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
Solved

Nasty Nested Loops for Array Manipulation

Posted on 2004-08-24
7
250 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Connect further...control easier

With the ATEN CE624, you can now enjoy a high-quality visual experience powered by HDBaseT technology and the convenience of a single Cat6 cable to transmit uncompressed video with zero latency and multi-streaming for dual-view applications where remote access is required.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Trouble linking program with -lcrypt 3 155
C#, VS15, StructLayout 1 135
List out all word 7 316
Windows Server 2012  Backup - HyperV host 18 54
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

789 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