i need a help on how to add the value of an array to another value of an array?

i dont need the whole program.. i just want to know what will i do in order to add the 4 values of an array using the for loop statement.

for example :

array[4]={12,5,2,1};


if i do it manually i guess i just write this..

sum= array[0]+array[1]+array[2]+array[3];

the output will be:

20

and i guess that's not the best idea to add those values of an array..

Recommended Answers

All 10 Replies

i need a help on how to add the value of an array to another value of an array?

Value of an array? I don't know what that means. You are going to have to elaborate, I think.

Here u go..

int a[2]={1,2};
int b[2];
for(size_t i=0;i<2;i++)
   b[i]=a[i];

Here u go..

int a[2]={1,2};
int b[2];
for(size_t i=0;i<2;i++)
   b[i]=a[i];

I don't see any addition occurring. :confused: You are doing a deep copy above, not addition, which is what the OP wants (see below).

sum= array[0]+array[1]+array[2]+array[3];

To the OP, initialize a variable to 0, then loop through your array, adding that element of the array to the sum each time. From your original code, try to generalize. Change it from this:

sum= array[0]+array[1]+array[2]+array[3];

to

sum= 0;
sum += array[0];
sum += array[1];
sum += array[2];
sum += array[3];

Set up a loop, and replace the array indexes with the loop control variable.

sum = 0;
for (int i = ?; ?; ?)
     sum += array[i];

hi!

thanks for your help..
but when i run the program the output is 00.

heres how i write the program:

#include<iostream.h>

main()
{
 int a[2]={1,2};
int b[2];
int counter;

for(counter-0;counter<2;counter++)
 {
   b[counter]=a[counter];
}
cout<< a[counter];
getch();
return 0;
}

ahh, ok now i know thanks man!

My bad..Thank u very much..
Back to the owner of this problem..

counter=0, not counter-0

yay! sorry i forgot! ^_^..

oopss i still have a problem!!.. What's wrong with my code?

main()
{

int a[4];
int b[4];
int counter,sum=0;

cout<<"JANUARY";
for(counter=0;counter<4;counter++)
 {
   cout<<"Numbers of Rainfall:";
   cin>>a[counter];
}

for(counter=0;counter<4;counter++)
 {
   sum+=a[counter];
 }

cout<<"The numbers of rainfall in the first month"<<sum<<endl;

cout<<FEBRUARY;
for(counter=0;counter<4;counter++)
 {
   cout<<"Numbers of Rainfall";
   cin>>b[counter];
 }

for(counter=0;counter<4;counter++)
 {
   sum+=b[counter];
 }

cout<<"The numbers of rainfall in the second month"<<sum<<endl;

getch();
return 0;
}

now my aim is to add all the values of first month and then print it. and same as the second month.(add all the values of second month and then print it). And when i run the program and then i put a values in the first month and its working fine but my problem is in second month because after i put a values in second month, the output of 1st month will add to the values of second month.

ex.
JANUARY
Numbers of Rainfall:4
Numbers of Rainfall:3
Numbers of Rainfall:2
Numbers of Rainfall:1

The numbers of rainfall in the first month 10

FEBRUARY
Numbers of Rainfall:5
Numbers of Rainfall:4
Numbers of Rainfall:3
Numbers of Rainfall:2

The numbers of Rainfall in Second month 24

now i need help on the second month on how to print the second month without adding the first month..?

Sorry i'm a newbie programmer but i'm studying hard. thanks guys..

You shouldn't post the same question in two different threads.

sorry im a newbie here.. it won't be happen again..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.