Arrays help

Hello!
I have just started with arrays and I have a question. I have been asigned to make a program that asks the user to write in numbers in an array.

I have got far enough to make it happen and the program works perfectly, just a small thing - I need help making it smaller (if possible).

Because right now it looks like a mess. It has to be possible to make it smaller.

1
2
3
4
5
6
7
8
9
10
11
 cin >> number[0];
    cin >> number[1];
	cin >> number[2];
	cin >> number[3];
	cin >> number[4];
	cin >> number[5];
	cin >> number[6];
	cin >> number[7];


  int add = number[0]+number[1]+number[2]+number[3]+number[4]+number[5]+number[6]+number[7]


Is it possible to make it smaller?
Last edited on
closed account (48T7M4Gy)
It sure is. A for loop is ideal.
kemort hit the nail on the head. The beauty of arrays (and why they are used in the first place) is that they are indexed, so you can loop through them with ease. For example, if you wanted to assign the number one to every element in an array:

1
2
3
4
5
int number[8];

for (int i = 0; i < 8; i++) {
    number[i] = 1;
}


Now try to do the same for the what you have to do. ;)
Topic archived. No new replies allowed.