Solved

Nasty Nested Loops for Array Manipulation

Posted on 2004-08-24
7
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
mixing C++ and C code elegantly 10 160
Problem to save 10 174
change colour of repeater control in asp.net c# 7 138
A  &&  B  || (C||D) how it works in c 7 153
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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

730 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