Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Reply
 
LinkBack Thread Tools Display Modes
Old 02-11-2011, 03:14 PM   #1 (permalink)
Apprentice
 
Join Date: Mar 2008
Posts: 78
Default Array addition


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......
subhransu123 is offline   Reply With Quote
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 02-11-2011, 04:02 PM   #2 (permalink)
Mmmph!!!
 
doomgiver's Avatar
 
Join Date: Nov 2010
Location: Mmmphhmph Mmphph
Posts: 1,408
Default Re: Array addition

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
__________________
Mmmphh-mphhhh-mmphh mhh!!!

Steam : doomgiver
doomgiver is offline   Reply With Quote
Old 02-11-2011, 04:30 PM   #3 (permalink)
Apprentice
 
Join Date: Mar 2008
Posts: 78
Default Re: Array addition

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 by subhransu123; 02-11-2011 at 04:36 PM.
subhransu123 is offline   Reply With Quote
Old 02-11-2011, 04:36 PM   #4 (permalink)
Fullbring
 
Zangetsu's Avatar
 
Join Date: Jan 2008
Location: Soul Society
Posts: 5,523
Default Re: Array addition

^whats your main objective here?
__________________
I'm the One you've been Waiting for...
Zangetsu is offline   Reply With Quote
Old 02-11-2011, 07:52 PM   #5 (permalink)
Human Spambot
 
Join Date: Nov 2004
Location: Madurai
Posts: 2,349
Default Re: Array addition

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 coeff[i]xcol[i]... col1=coeff1xcol1+coeff2xcol2+coeff3xcol3+coeff4xco l4.

Hope that helps...
Arun
sakumar79 is offline   Reply With Quote
Old 17-11-2011, 02:51 PM   #6 (permalink)
Right Off the Assembly Line
 
Join Date: Nov 2011
Location: Hawaii
Posts: 1
Default Re: Array addition

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[i]; //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!!
sandradavis is offline   Reply With Quote
Old 27-11-2011, 05:45 PM   #7 (permalink)
Right Off the Assembly Line
 
Join Date: Mar 2009
Location: hydrabad
Posts: 8
Default Re: Array addition

Quote:
Originally Posted by subhransu123 View Post
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[i][k];
}

a[i][0]=sum;
}


hope you have got the idea. have fun!
priyaranjan is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:36 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2