0

I want to add the elements of a 2D array row wise and then the summation I get want to store in another array. Like:: An array of 3 rows and 3 columns.. {1,2,3}=>6,{2,3,4}=>9,{5,6,7}=>18 and now I wanna store the values(6,9,18) in another array.What should I do?I could only able to sum up row wise.Should I use malloc? Please do help.

#include<stdio.h>
void main(){
    int i,j,k,sum=0;
    int array[3][3]={
        {1,2,4},
        {4,5,6},
        {7,8,9}
    };
    int array2[k];
    for(i=0;i<=2;i++){
        for(j=0;j<=2;j++){
            sum=sum+array[i][j];
        }
        printf("sum is %d in row %d in array1 \n \n",sum,i);

        sum=0;
    }
}   
RedRidingHood
  • 568
  • 3
  • 16

4 Answers4

0

int array2[3];

array2[i]=sum after the inner loop

You can extract the 3 to something like:

#define SIZE 3

and use it wherever needed.

chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
ItayB
  • 10,377
  • 9
  • 50
  • 77
0

If you know, that your array contains exactly 3 rows, you don't need to use any malloc. Just declare the resulting array like this: int array2[3] and then, store your sums in this array like this: array2[i] = sum; (after inner loop, where j is the iterator). To make your code more predictable, set sum to zero before the inner loop, when you count the sum.

RedRidingHood
  • 568
  • 3
  • 16
  • Can you please suggest how can I use malloc to deal with that same prob? Your answer was really helpful –  Sep 16 '16 at 20:40
  • @SUVAMROY Thank you =) Say, if you know, that your `array` contains `k` rows, you need to allocate memory for `array2` like this: `array2 = (int*) malloc( sizeof(int) * k )`. And you will need to make your outer loop iterate from 0 through k-1: `for(i = 0; i < k; i++)` In the end of the program, don't forget to free the memory like this: `free( array2 );` – RedRidingHood Sep 16 '16 at 20:44
  • Rather than `array2 = (int*) malloc( sizeof(int) * k )`, consider `array2 = malloc( sizeof *array2 * k);` The cast is not needed and no need to coordinate the type, simple use the size of the referenced data. – chux - Reinstate Monica Sep 16 '16 at 20:48
  • @SUVAMROY You can access the elements of `array2`, allocated with `malloc`, like elements of any other array: `array2[i]`, as an example. – RedRidingHood Sep 16 '16 at 20:48
  • @chux Since the code is in `C`, it would be better to use case, because `malloc` returns `void*` – RedRidingHood Sep 16 '16 at 20:51
  • Many would disagree [Do I cast the result of malloc?](http://stackoverflow.com/a/605856/2410359) – chux - Reinstate Monica Sep 16 '16 at 21:14
  • @chux Ok, it's only up to you. – RedRidingHood Sep 16 '16 at 22:46
0

The source array is a fixed size, so the result array can be as well.

int array2[3];
for(i=0;i<=2;i++){
    array2[i]=0;
    for(j=0;j<=2;j++){
        array2[i]+=array[i][j];
    }
    printf("sum is %d in row %d in array1 \n \n",array[i],i);
}
dbush
  • 205,898
  • 23
  • 218
  • 273
0

you have to keep in mind that you must verify the size of the array as a constant or by using malloc , than you can use this code .

void main(){


int i,j,k,sum=0;
int array[3][3]={
{1,2,4},
{4,5,6},
{7,8,9}
};
int array2[3];
for(i=0;i<=2;i++){
for(j=0;j<=2;j++){
    sum=sum+array[i][j];
}
array2[i] = sum ; 
 sum = 0 ;


sum=0;

  }
 for(i=0;i<=2;i++){
   printf("sum is %d in row %d in array1 \n \n",array2[i],i);
   }

   }