strange array shift

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • MN

    strange array shift

    Hi,
    I was writing a program that must dynamically allocate memory to a 2-
    dimensions array, initialize it to 0, then place the result of
    multiplication of two 1-dimension arrays as described in the next code
    example.
    My code is:

    #include<stdio. h>
    #include<stdlib .h>

    int main ()
    {
    int A[7] = {1,1,1,1,1,1,1} ;
    int B[4] = {2,2,2,2};

    int i = 0;
    int ii = 0;

    int j = 0;
    int jj = 0;

    int k = 0;

    int deg_a = sizeof(A)/sizeof(A[0]) - 1;
    int deg_b = sizeof(B)/sizeof(B[0]) - 1;

    int C_dimension = deg_a+deg_b+1;

    int** C_1 = NULL;


    if (C_1 != NULL)
    free(C_1);

    C_1 = (int**)malloc(s izeof(int**) * (deg_a+1));

    for (i = 0; i< (deg_a+1); i++)
    C_1[i]= malloc(C_dimens ion);

    // Initialize all values of C_1 to 0
    printf("C_1 is initialized to 0:\n");

    for(j = 0; j < (deg_a+1); j++)
    {
    for (k = 0; k < C_dimension; k++)
    {
    C_1[j][k] = 0;
    // For verification
    printf("%3d ", C_1[j][k]);
    }
    printf("\n");
    }

    // Do multiplication of A[deg_a+1] and B[deg_b+1]
    for (ii = 0; ii <(deg_a+1); ii++)
    {
    for(jj = 0; jj <(deg_b+1) ; jj++)
    {
    C_1[ii][ii+jj] = A[ii]*B[jj];
    }
    }

    printf("\n");
    // For verification
    printf("After multiplication of A[%d] and B[%d], C_1 is:\n", deg_a
    +1, deg_b+1);
    for(j = 0; j < (deg_a+1); j++)
    {
    for (k = 0; k < C_dimension; k++)
    {
    printf("%3d ", C_1[j][k]);
    }
    printf("\n");
    }
    return 0;

    }

    After executing I get:

    ../test_array

    C_1 is initialized to 0:
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0
    0 0 0 0 0 0 0 0 0 0

    After multiplication of A[7] and B[4], C_1 is:
    2 2 2 2 0 0 0 0 0 2
    0 2 2 2 2 0 0 0 0 0
    0 0 2 2 2 2 0 0 0 0
    0 0 0 2 2 2 2 0 0 0
    0 0 0 0 2 2 2 2 0 0
    0 0 0 0 0 2 2 2 2 0
    2 0 0 0 0 0 2 2 2 2

    what I want as result is:

    2 2 2 2 0 0 0 0 0 0
    0 2 2 2 2 0 0 0 0 0
    0 0 2 2 2 2 0 0 0 0
    0 0 0 2 2 2 2 0 0 0
    0 0 0 0 2 2 2 2 0 0
    0 0 0 0 0 2 2 2 2 0
    0 0 0 0 0 0 2 2 2 2

    After debugging I noticed that C_1[1][0] and C_1[0][9] get the same
    value at the same time, although I didn't change the value of C_1[0]
    [9].
    So why this occurs?
    Thanks for your help.

  • Richard Heathfield

    #2
    Re: strange array shift

    MN said:
    Hi,
    I was writing a program that must dynamically allocate memory to a 2-
    dimensions array, initialize it to 0, then place the result of
    multiplication of two 1-dimension arrays as described in the next code
    example.
    My code is:
    >
    #include<stdio. h>
    #include<stdlib .h>
    >
    int main ()
    {
    int A[7] = {1,1,1,1,1,1,1} ;
    int B[4] = {2,2,2,2};
    Okay, we have an array of seven ints, and an array of four ints. For matrix
    multiplication, the number of columns in A has to match the number of rows
    in B. So there are only two ways for this to make sense. One of them is:

    1
    1
    1
    [ 1 ] * [ 2 2 2 2 ]
    1
    1
    1

    which multiplies out to

    2 2 2 2
    2 2 2 2
    2 2 2 2
    [ 2 2 2 2 ]
    2 2 2 2
    2 2 2 2
    2 2 2 2

    and the other is the same deal only rotated 90 degrees.

    >
    int i = 0;
    int ii = 0;
    >
    int j = 0;
    int jj = 0;
    Indices, presumably.
    >
    int k = 0;
    >
    int deg_a = sizeof(A)/sizeof(A[0]) - 1;
    int deg_b = sizeof(B)/sizeof(B[0]) - 1;
    deg_a is 6, and deg_b is 4. These look like "max index" values to me.
    int C_dimension = deg_a+deg_b+1;
    It is no longer obvious to me what you are actually trying to do.
    >
    int** C_1 = NULL;
    >
    >
    if (C_1 != NULL)
    free(C_1);
    How can it not be NULL? You only just set it to NULL.
    C_1 = (int**)malloc(s izeof(int**) * (deg_a+1));
    That's wrong. C_1 has type int **, so you're using it to point at a bunch
    of int *, so you need space for a bunch of int *, not a bunch of int **.
    You can get this right every time using the following template:

    p = malloc(n * sizeof *p);

    Thus, in your case:

    C_1 = malloc((deg_a + 1) * sizeof *C_1);


    Okay, so what do you do with this memory?
    for (i = 0; i< (deg_a+1); i++)
    C_1[i]= malloc(C_dimens ion);
    Here's your problem right here. Well, /a/ problem. You're not allocating
    enough storage (unless ints are only 1 byte wide on your system, which can
    happen, but isn't typical on desktop machines).
    >
    // Initialize all values of C_1 to 0
    printf("C_1 is initialized to 0:\n");
    >
    for(j = 0; j < (deg_a+1); j++)
    {
    for (k = 0; k < C_dimension; k++)
    {
    C_1[j][k] = 0;
    // For verification
    printf("%3d ", C_1[j][k]);
    }
    printf("\n");
    }
    Well, okay, you could simplify this a lot while you're fixing the bug:

    if(C_1 == NULL)
    {
    take evasive action
    }
    else
    {
    for(i = 0; i < deg_a + 1; i++)
    {
    C_1[i] = calloc(C_dimens ion, sizeof *C_1[i]);
    if(C_1[i] == NULL)
    {
    take evasive action


    That's probably the issue, anyway - insufficient storage for your array.
    But since it's not obvious to me what you're trying to do, it may well be
    that there are other issues too that I didn't spot.

    --
    Richard Heathfield <http://www.cpax.org.uk >
    Email: -http://www. +rjh@
    Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
    "Usenet is a strange place" - dmr 29 July 1999

    Comment

    • MN

      #3
      Re: strange array shift

      >
      That's probably the issue, anyway - insufficient storage for your array.
      But since it's not obvious to me what you're trying to do, it may well be
      that there are other issues too that I didn't spot.
      What I want to get is a program that can do a polynomial
      multiplication, so I supposed that each polynomial can be represeteb
      by an array where:
      array[0] represents the coeffcient of x^0
      array[1] represents the coeffcient of x^1
      array[2] represents the coeffcient of x^2
      ..... and so on.

      After I can multiply each coefficient of the first array with all
      coefficients of the second array and put the result in a new row of an
      new array (in this case called C_1). At the end I can easily do the
      sum of all elements that are in the column in C_1 and this will give
      the final result of polynomial multiplication.
      As I think (may be I'm wrong!) this is the simple approach to code
      polynomial multiplication in c language. Perhaps there is another and
      simple way to do this.

      Comment

      • gw7rib@aol.com

        #4
        Re: strange array shift

        On 19 Sep, 12:05, MN <mazouz.nezh... @gmail.comwrote :
        That's probably the issue, anyway - insufficient storage for your array.
        But since it's not obvious to me what you're trying to do, it may well be
        that there are other issues too that I didn't spot.
        >
        What I want to get is a program that can do a polynomial
        multiplication, so I supposed that each polynomial can be represeteb
        by an array where:
        array[0] represents the coeffcient of x^0
        array[1] represents the coeffcient of x^1
        array[2] represents the coeffcient of x^2
        .... and so on.
        >
        After I can multiply each coefficient of the first array with all
        coefficients of the second array and put the result in a new row of an
        new array (in this case called C_1). At the end I can easily do the
        sum of all elements that are in the column in C_1 and this will give
        the final result of polynomial multiplication.
        As I think (may be I'm wrong!) this is the simple approach to code
        polynomial multiplication in c language. Perhaps there is another and
        simple way to do this.
        If that's what you're trying to do, you don't need a two-dimensional
        array at all. Given the difficulties of two-dimensional arrays in C,
        an approach which does not use them should be simpler. What's wrong
        with:

        (allocate C to the right size, and initialise all the values to zero)

        for (ii = 0; ii <(deg_a+1); ii++)
        {
        for(jj = 0; jj <(deg_b+1) ; jj++)
        {
        C[ii+jj] += A[ii]*B[jj];
        }
        }

        Hope that helps.
        Paul.

        Comment

        • MN

          #5
          Re: strange array shift


          Paul,
          Thanks for help!! program is working !


          Comment

          • Keith Thompson

            #6
            Re: strange array shift

            MN <mazouz.nezhate @gmail.comwrite s:
            [...]
            My code is:
            >
            #include<stdio. h>
            #include<stdlib .h>
            >
            int main ()
            {
            int A[7] = {1,1,1,1,1,1,1} ;
            int B[4] = {2,2,2,2};
            >
            int i = 0;
            int ii = 0;
            >
            int j = 0;
            int jj = 0;
            >
            int k = 0;
            >
            int deg_a = sizeof(A)/sizeof(A[0]) - 1;
            int deg_b = sizeof(B)/sizeof(B[0]) - 1;
            >
            int C_dimension = deg_a+deg_b+1;
            >
            int** C_1 = NULL;
            >
            >
            if (C_1 != NULL)
            free(C_1);
            >
            C_1 = (int**)malloc(s izeof(int**) * (deg_a+1));
            >
            for (i = 0; i< (deg_a+1); i++)
            C_1[i]= malloc(C_dimens ion);
            >
            // Initialize all values of C_1 to 0
            printf("C_1 is initialized to 0:\n");
            >
            for(j = 0; j < (deg_a+1); j++)
            {
            for (k = 0; k < C_dimension; k++)
            {
            C_1[j][k] = 0;
            // For verification
            printf("%3d ", C_1[j][k]);
            }
            printf("\n");
            }
            >
            // Do multiplication of A[deg_a+1] and B[deg_b+1]
            for (ii = 0; ii <(deg_a+1); ii++)
            {
            for(jj = 0; jj <(deg_b+1) ; jj++)
            {
            C_1[ii][ii+jj] = A[ii]*B[jj];
            }
            }
            >
            printf("\n");
            // For verification
            printf("After multiplication of A[%d] and B[%d], C_1 is:\n", deg_a
            +1, deg_b+1);
            for(j = 0; j < (deg_a+1); j++)
            {
            for (k = 0; k < C_dimension; k++)
            {
            printf("%3d ", C_1[j][k]);
            }
            printf("\n");
            }
            return 0;
            >
            }
            [...]

            Among other oddities in your code, you compute deg_a and deg_b as 1
            less than the lengths of the A and B arrays, but then you almost
            always refer to deg_a+1 and deg_b+1, which are simply the lengths of A
            and B. Why not just compute and use their lengths?

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            Working...