subhransu123

Journeyman
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

Warframe
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
 
OP
S

subhransu123

Journeyman
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....
 
Last edited:

sakumar79

Technomancer
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
 

sandradavis

Right off the assembly line
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!!
 

priyaranjan

Right off the assembly line
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!
 
Top Bottom