Trying not to write two FOR loops

So this array has a set of values already assign to it.
Is there a way to print out the array that was assigned to it and also add them all together in one FOR loop?


Line 16 FOR loop shows that I do know how to add the arrays. I am just wondering if I can do it all in just one.

Thank you
ps This is not homework, it is from C++ without fear exercise. Just trying to learn extra stuff over spring break.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <cmath>

using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
	int array[6]={10,22,13,99,4,5};
	for (int i=0; i<6; i++){
		cout<<array[i]<<endl;
		array[i]=array[i]+array[i+1];
		cout<<array[i]<<endl;
		cout<<"\nThe Value of i: "<<i<<endl;
	}

//for (int i=1; i<6; i++){
//		array[i]+=array[i-1];
//		cout<<array[i]<<endl;
//	}
	

	return 0;
}
Is there a way to print out the array that was assigned to it and also add them all together in one FOR loop?


It seems like you are already doing that in your loop from 9-14? I'm not sure what you are asking.
- Try this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14


int total;

int aaaa[]={1,2,3,4,5,6};
total=0;
for(int i=0;i<6;i++)
{

total= aaaa[i] + total; // total variable
cout<<aaaa[i]<<endl; // display array


}


- Basically, you keep a total variable or a running total.

- Hopefully , i understood your question correctly and this has helped you.

The adding of the arrays is not right. It gives me 32 for the (10+22) then 35 which is for the (22+13). I am looking the sum of all the arrays.
- Then keep a running total as i mentioned above.
Thank you so much. But now I feel like an idiot, that was simple. Thank you again.
- Don't worry about it, just programming and having fun.
Topic archived. No new replies allowed.