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..

Dani AI

Generated

Short version: the simplest, robust approach is to keep a running total for each group of inputs and make sure that total is initialized (or reset) before you sum that group. In this thread was doing the right thing with a loop but later saw the second-month total include the first-month numbers because the running total was never cleared. showed array copying (useful, but different), and pointed toward using a loop + += rather than listing every index by hand.

A clean, modern C++ pattern (safer than raw C arrays) is to use a small container and sum it — for example with std::vector and std::accumulate. This also avoids off-by-one mistakes and makes it obvious when one total is computed and another is computed separately:

#include <iostream>
#include <vector>
#include <numeric>

int main() {
    std::vector<int> jan(4), feb(4);
    for (int i = 0; i < 4; ++i) std::cin >> jan[i];
    int sumJan = std::accumulate(jan.begin(), jan.end(), 0);
    std::cout << "January total: " << sumJan << '\n';

    for (int i = 0; i < 4; ++i) std::cin >> feb[i];
    int sumFeb = std::accumulate(feb.begin(), feb.end(), 0);
    std::cout << "February total: " << sumFeb << '\n';
    return 0;
}

Troubleshooting checklist (things that caused the behavior seen in the thread):

  • Always initialize the accumulator (sum = 0) before each new sum, or use separate variables (sumJan, sumFeb).
  • Make sure loop indices are initialized and bounded correctly (no counter-0 typos, and print the sum variable — not an array index after the loop).
  • Prefer <iostream> and int main() with return 0; and avoid non-standard helpers like getch() in portable code.
  • For larger datasets or potential overflow, use a wider type (e.g., long long) and validate input with std::cin.

Using a small helper function or std::accumulate makes the intention obvious and prevents the very bug that caused the second-month total to include the first.

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.