Thread: 2Darray in 1Darray and sum elements

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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

  3. #3
    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]

  4. #4
    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

  5. #5
    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