Help summing values within an Array

Hello There! I've been having trouble getting the sum of values from an array. The array is filled with random numbers from a Data File. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void f2()
{
	
		const int ARRAY_SIZE = 50;
		int numbers[ARRAY_SIZE];

		int count = 0;
		ifstream inputFile;

		inputFile.open("filename.txt");

		while (count < ARRAY_SIZE && inputFile >> numbers[count])
			count++;

		inputFile.close();

		for (int index = 0; index < count; index++)
			cout << numbers[index] << endl;

		int total = 0;
		for (int count = 0; count < ARRAY_SIZE; count++)
			total += numbers[count];

		cout << total << endl;
	
	system("pause");
	return;

}
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void f2()
{
	
		const int ARRAY_SIZE = 50;
		int numbers[ARRAY_SIZE] ={0}; // zero array elements

		int count = 0;
		ifstream inputFile;

		inputFile.open("filename.txt");

		while ( (count < ARRAY_SIZE) && (inputFile >> numbers[count]) )
			count++;

		inputFile.close();

		for (int index = 0; index < count; index++)
			cout << numbers[index] << endl;

		int total = 0;
		for (int c= 0; c < count; c++)
			total += numbers[c];

		cout << total << endl;
	
	system("pause");
	return;

}



Last edited on
Topic archived. No new replies allowed.