Solved

Nasty Nested Loops for Array Manipulation

Posted on 2004-08-24
7
242 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:baharbh
7 Comments
 
LVL 23

Expert Comment

by:brettmjohnson
ID: 11885728
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
 
LVL 16

Expert Comment

by:imladris
ID: 11885756
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
 

Author Comment

by:baharbh
ID: 11890620
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 1

Assisted Solution

by:ageraldnaveen
ageraldnaveen earned 150 total points
ID: 11890865
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
 
LVL 1

Expert Comment

by:ageraldnaveen
ID: 11890873
NewArray will contain the expected answer.. you may return it from process if required.

int* process(int n)
{
.....
return NewArray;
}
0
 
LVL 84

Accepted Solution

by:
ozo earned 150 total points
ID: 11890881
    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
 

Author Comment

by:baharbh
ID: 11891982
Liked both answers from ozo and ageraldnaveen hence splitting points. Thanks for your help.

baharbh
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now