2Darray in 1Darray and sum elements

This is a discussion on 2Darray in 1Darray and sum elements within the C Programming forums, part of the General Programming Boards category; Hello and congratulations for the forum! My problem: we have a 2D array[i][j] nxn . We put serial the rows ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    63

    2Darray in 1Darray and sum elements

    Hello and congratulations for the forum!
    My problem: we have a 2D array[i][j] nxn . We put serial the rows of the 2D array in one 1D array[t].Next we have a function with this prototype:
    PHP Code:
    int myfunc(array[t] , n); 
    The int that the function returns is 1 or 0.
    1(one) if the sum of each row and column(like as we have a 2D array) in 1D array is the same returns 1,otherwise returns 0;So i am very confused with the loops that i must do.Can anyone give me an idea?
    My thougt, as example we have a 3X3 array:
    [img=http://img17.imagevenue.com/loc1116/th_11174_array_122_1116lo.jpg](the numbers are random)

    for the sum of the the rows in array[t] we must make 3 loops and for each loop i must calcuate the sum each t/n=3 elements (from 0-2, 3-5, 6-8 )
    for the rows i must sum example the array[0] + array[t/n] + array[2t/n]
    Am i wrong? How can i do the loops?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    For a 2D or higher dimension array, use nested for loops. Something like this.
    Code:
    int x, y, sum;
    
    for(x = 0; x < rows; x ++) {
        for(y = 0; y < cols; y ++) {
            sum += array[x][y];
        }
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Yes i know that, but we have a 1D array and we should use it like it was a 2D array.If you can't give me some code i want to give me some advices such as : should i calculate the sum of each column and row separately and after compare them or must i calculate the sum of the first row and the sum for the first column, compare them and after continue with the others?Thanks!

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    With a 1D array that uses the same amount of memory as a 2D array, a2Darray[i][j] is equal to a1Darray[i * maxi + j], where a2Darray is declared as a2Darray[maxi][maxj] and a1Darray is declared as a1Darray[maxi * maxj]. In code:
    Code:
    const int maxi = 2, maxj = 3;
    int a2Darray[maxi][maxj];
    int a1Darray[maxi * maxj];
    int x, y;
    
    for(x = 0; x < maxi; x ++) {
        for(y = 0; y < maxj; y ++) {
            a2Darray[x][y] = x * maxi + y;
            a1Darray[x * maxi + y] = x * maxi + y;
        }
    }
    
    /* a1Darray[] can be initialized in the same way with:
    
    for(x = 0; x < maxi * maxj; x ++) {
        a1Darray[x] = x;
    }*/
    
    /* So, a2Darray[][] contains:
        0, 1,
        2, 3,
        4, 5
    
    while a1Darray contains:
        0, 1, 2, 3, 4, 5
    */
    That may have confused you more than it helped, but oh well . . .

    should i calculate the sum of each column and row separately and after compare them or must i calculate the sum of the first row and the sum for the first column, compare them and after continue with the others?
    It depends on what you are trying to do. I was under the impression that you wanted to sum the entire 1D array, and compare that sum with the sum of the entire 2D array; in which case, summing each row and each column at the same time would make sence.
    Last edited by dwks; 08-29-2007 at 01:09 PM.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    so, you have an array like this:

    Code:
    int arr[9];
    but you want it to appear like:
    Code:
    int arr[3][3];
    And then you call the function with myfunc(arr, 3).
    Code:
    int myfunc(int arr[], int n)
    {
        int x, y;
        for(y = 0; y < n; y++)
        {
            for(x = 0; x < n; x++) 
            {
                 int e = arr[y * n + y];
                 // ...  use e to calculate your sums. 
            }
        }
    }
    --
    Mats

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    I realize that that's just an example; however, the OP never said that the elements were equal in size. In fact, [s]he said:
    My problem: we have a 2D array[i][j] nxn . We put serial the rows of the 2D array in one 1D array[t].
    Presumably t=i*j . . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by dwks View Post
    I realize that that's just an example; however, the OP never said that the elements were equal in size. In fact, [s]he said:

    Presumably t=i*j . . . .
    I took
    My problem: we have a 2D array[i][j] nxn
    the n x n to mean that it's a square layout. Otherwise the function will require an X and Y size of course.

    --
    Mats

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by matsp View Post
    I took the n x n to mean that it's a square layout. Otherwise the function will require an X and Y size of course.
    You're probably right. I didn't get the "nxn" myself.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,249
    To step around in such flat representations it's useful to precompute the step size along each dimension. Moving in a given dimensions means adding or subtracting some constant value from the current position.

    In general, suppose you have a flat array which is conceptually layed out as:

    array[Dim1][Dim2]...[DimN-1][DimN]

    In other words, an N-dimensional array. The total size of the flat array is the produce of the dimensions Dim1*Dim2*...*DimN. You probably already understand this part.

    To find the "step" to move along dimension N, you simply take the product of the dimension sizes of all dimensions which follow it in the layout. So take your example:

    array[Dim1][Dim2].

    Suppose you want to move along Dim2. The product of all the dimensions FOLLOWING Dim2 is 1 -- nothing follows Dim2. It's as if there was a silent [1] sitting there. So the step size along Dim2 is 1. Now consider moving along Dim1. The product of the following dimensions is Dim2, so the step size along Dim1 is Dim2.

    To compute an absolute position in the array for a point <x1, x2, ..., xN>, you would find the sum x1*Step1 + x2*Step2 + ... + xN*StepN.

  10. #10
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Sorry my mistake.There is no 2D array .I just describe how the only and only one 1D array that we have work.More clearly we have one 1D array[t] and we must find the sum of each row and each column(like as the array was 2D) and compare them.If all are the same(example sum_first_column=10 and sum_first_row=10, sum_sec_column_=12 and sum_sec_column=10 etc. return 1,else 0)

    If you don't understand see and the 1st picture that i upload.We have only array[t]
    http://img17.imagevenue.com/img.php?...122_1116lo.jpg
    example from the picture.I want a loop in array[t] to add 5 6 7 after 5 4 10 and then compare them, after add 4 1 2 after 6 1 5 etc
    Last edited by alzar; 08-29-2007 at 01:51 PM.

  11. #11
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Yes, so my solution describes how to get to a certain element in that array.

    You will have to write the code to figure out what column sums are and such.

    --
    Mats

  12. #12
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Quote Originally Posted by matsp View Post
    Yes, so my solution describes how to get to a certain element in that array.

    You will have to write the code to figure out what column sums are and such.

    --
    Mats
    No i don't think so or i didn't understand.If we have array[i][j] we say
    PHP Code:
    for i=0i<n
       
    for j=0 j<
    So in 1D array[t] i have only t for loop and thought that must do these(hope are correct)
    for the sum of the the rows in array[t] we must make 3 loops and for each loop i must calcuate the sum each t/n=3 elements (from 0-2, 3-5, 6-8 )
    for the rows i must sum example the array[0] + array[t/n] + array[2t/n]

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    So, if we take a 9-element array and "make it into a 2D array", it would have the following indices:
    Code:
    [0] = [0][0]   [1] = [0][1]   [2]=[0][2]
    [3] = [1][0]   [4] = [1][1]   [5]=[1][2]
    [6] = [2][0]   [7] = [2][1]   [8]=[2][2]
    So to get to element [y][x], we use [y *n + x] - for example, x = 1, y = 1 -> 1*3 + 1 -> 4; x = 0, y = 2 -> 2*3 + 0 -> 6.

    Does this make sense to you?

    --
    Mats

  14. #14
    Registered User
    Join Date
    Aug 2007
    Posts
    63
    Yes thank you very much.Now i understand what you mean.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21