• C

Nasty Nested Loops for Array Manipulation

Given an array declared as follows:

    int My_Array[16][4] = { {2,3,5,7},{6,2,1,4},{5,3,8,6},{3,3,4,5},
      {2,1,5,7},{1,3,5,7},{2,4,6,8},{1,2,3,4},
      {4,3,2,1},{3,1,2,4},{6,7,5,3},{2,4,6,3},
      {1,2,7,8},{2,4,4,2},{2,5,7,5},{3,7,8,6} };

    int *NewArray;

somewhere in the program a user specifies a value ( n = 1, 2, 4 or 8). Below is my expected output:

if n=1, contents of NewArray = {45,54,78,80} - sum of cols 0-3

if n = 2, contents of NewArray = {22,21,37,48,24,21,34,42} - split My_Array into 2, get column sums of 1st 8 row and col sum of 2nd 8 rows.

if n = 4, contents of NewArray =  {16,11,18,22,16,9,18,      22,11,10,22,25,8,11,20,27} ie split My_Array into 4 and store the sums of every 4 rows into NewArray.

Hope u get the idea. I am lost in the nested loops. Hope u can give some pointers to proceed.

baharbh
baharbhAsked:
Who is Participating?
 
ozoCommented:
    int *NewArray1 = malloc(4*n*sizeof(int)) ;
     int i,j ;
         for( i = 0; i < 4*n; i++ ){
              for( NewArray1[i] = 0, j = i/4*16/n; j < (i/4+1)*16/n; j++ ){
                    NewArray1[i] += My_Array[j][i%4];
               }
         }
   
0
 
brettmjohnsonCommented:
Start with the simplest case:

case 1:
{
      int NewArray1[4] ;
      int i,j ;
      for (i = 0; i < 4; i++)
            for (NewArray1[i] = 0, j =0; j < 16; j++)
                  NewArray1[i] += My_Array[j][i];
      break;
}

Now the next one:

case 2:
{
      int NewArray2[8] ;
      int i,j ;
      for (i = 0; i < 4; i++)
            for (NewArray2[i] = 0, j =0; j < 8; j++)
                  NewArray2[i] += My_Array[j][i];
      for (i = 4; i < 8; i++)
            for (NewArray2[i] = 0, j =8; j < 16; j++)
                  NewArray2[i] += My_Array[j][i];

      break;
}

The pattern seems clear at this point.

0
 
imladrisCommented:
I'm not following along.

My_Array consists of 16 rows.
If n==1 then the first element of NewArray is the sum of the first elements of all the rows (2,6,5,3,2,1,2,1,4,3,6,2,1,2,2,3)=45; the second element of NewArray is the sum of the second elements of all the rows (3,2,3,3,1,3,4,2,3,1,7,4,2,4,5,7)=54. So far so good. The next two will also presumably work out.

If n==2,  the the first element of NewArray is the sum of the first elements of the first 8 rows(2,6,5,3,2,1,2,1)=22; the second element of NewArray is the sum of the second elements of the first 8 rows (3,2,3,3,1,3,4,2)=21. The third and fourth elements analogously. But the fifth element of NewArray should then be the sum of the first elements of the *last* 8 rows (4,3,6,2,1,2,2,3)=23!?!? The 6th element would be the sum of the second elements of the last 8 rows (3,1,7,4,2,4,5,7)=33??

Similarly for n==4 the first set seems to work, but nothing after that. What am I misunderstanding?
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
baharbhAuthor Commented:
Sorry about that. Yes Imladris ur right.

If n == 1 contents of NewArray
   = {45,54,78,80}

If n == 2 contents of NewArray
  = {22,21,37,48,23,33,41,32}

If n == 4 contents of NewArray
 = {16,11,18,22,6,10,19,26,15,15,15,11,8,18,26,21}

I got my formula mixed up while using MS Excel to get the sums.

If possible I wd like to have a gen-purpose solution ie without the need for cases 1,2,4,8 etc.
0
 
ageraldnaveenCommented:
Here is a generic solution which would give the expected result for any array and for any number..

int My_Array[16][4] = { {2,3,5,7},{6,2,1,4},{5,3,8,6},{3,3,4,5},
     {2,1,5,7},{1,3,5,7},{2,4,6,8},{1,2,3,4},
     {4,3,2,1},{3,1,2,4},{6,7,5,3},{2,4,6,3},
     {1,2,7,8},{2,4,4,2},{2,5,7,5},{3,7,8,6} };

#define ARR_ROWS 16 // to generalize
#define ARR_COLS 4 // to generalize

void process(int n)
{
      int *NewArray = new int[n* sizeof(int) * ARR_COLS];
      memset(NewArray,0,n*sizeof(int) * ARR_COLS); // initialize to zero
      int i;
      int nUnitSize = ARR_ROWS / n;
      int nRowBias = 0;
      for(i=0; i < n; i++)
      {
            int j;
            int startRow = i * nUnitSize;
            for(j= startRow; j < startRow + nUnitSize; j++)
            {
                  int nCol;
                  for(nCol=0;nCol<ARR_COLS;nCol++)
                        NewArray[nRowBias+nCol] += My_Array[j][nCol];

            }
            nRowBias += ARR_COLS; // use this to store the sum splitted as per n
      }
}


hope that helps.
0
 
ageraldnaveenCommented:
NewArray will contain the expected answer.. you may return it from process if required.

int* process(int n)
{
.....
return NewArray;
}
0
 
baharbhAuthor Commented:
Liked both answers from ozo and ageraldnaveen hence splitting points. Thanks for your help.

baharbh
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.