DIGIT-ARC read-only archive · snapshot: July 23, 2026

Array addition

7 posts · originally from https://geek.digit.in/community/threads/array-addition.148281/

S
subhransu123 Original post #1
Nov 2, 2011 at 3:14 PM
I have 4 by 4 array,how can i perform the following operation(using c)
col1=col1+col2
col1=col1+col3
col1=col1+col4
col1=col1+col2+col3
col1=col1+col2+col4
col1=col1+col3+col4
col1=col1+col2+col3+col4
please help......
doomgiver
Nov 2, 2011 at 4:02 PM
use nested loops and inside the nested loop, force the column number to remain same and change the row number for the columns you want to add.

if you cant figure it out, say so and i'll put some code
S
Nov 2, 2011 at 4:30 PM
thanks for help i cant figured actually......please give some code.........
because here sometimes add two col,sometimes add three col and if there are 10 by 10 mtx it add will be large.so it could be psbl in one loop???????pls help....
Zangetsu
Nov 2, 2011 at 4:36 PM
^whats your main objective here?
S
Nov 2, 2011 at 7:52 PM
1. Please dont ask people to post the code for you (in this forum at least). First show what you have tried and others will help you to fix any problems in it.
2. If you want to write separate functions for each summation, it is very easy with nested loop. If you want a single function that can do any of the above, you need to create a coeff array with 0 or 1 depending on whether you want to add a column entry or not. For example, col1 = col1+col2 will have coeff array (1,1,0,0), col1=col1+col3+col4 will have coeff array (1,0,1,1). Then, all you need to do is create a function that takes in the array col, the array coeff and the col number of result c, run a loop through all rows and nest in loop through all columns setting in each row col[c]= sum of coeffxcol... col1=coeff1xcol1+coeff2xcol2+coeff3xcol3+coeff4xcol4.

Hope that helps...
Arun
S
Nov 17, 2011 at 2:51 PM
include <iostream>

int main()
{

const int ARRAY_SIZE = 5;

int sum = 0;
int array[ARRAY_SIZE] = {1, 2, 3, 4, 5}; //or whatever you want the array to be
//change ARRAY_SIZE accordingly

for(int i=0; i < ARRAY_SIZE; i++)
sum += array; //add up array

std::cout<<sum; //print out sum
std::cin.ignore(100,'\n'); //used to pause program before it closes
}

HOPE THIS WILL HELP YOU!!
P
Nov 27, 2011 at 5:45 PM
I have 4 by 4 array,how can i perform the following operation(using c)
col1=col1+col2
col1=col1+col3
col1=col1+col4
col1=col1+col2+col3
col1=col1+col2+col4
col1=col1+col3+col4
col1=col1+col2+col3+col4
please help......

very simple

int a[4][4]
int sum=0;
//for col1 + col2

for(int i=0;i<4;i++)
{
for(int k=0; k<2;k++)
{
sum=a[k];
}

a[0]=sum;
}


hope you have got the idea. have fun!