Using arrays

Create a file containing 20 exams scores ( ranging from 0-100 ). Write a program to read the file, calculate and output the average exam score to the screen. Use a 1 dimensional array to hold the exam scores.

I need help coming up with a program with this because this is my first time ever using arrays and it just does not make any sense to me at all. Please! Can someone help me ??
closed account (48T7M4Gy)
What you do is create 20 exam scores and file them.

Hint: the array will need 20 elements and the average is the total of all the 20 divided by 20. Use floats because integer division will give you bad/misleading average.

arrays? Read this if your class notes and textbook fail:
http://www.cplusplus.com/doc/tutorial/arrays/

Come back with some code is best way to get more help:)


When I put it in the Visio Studio its shows an error. accumulate is underlined ?
In the same way that you loop through the array to populate it, loop through it again to sum up the contents. Initialize a variable to 0 and add the contents of each array element to it.

Edit: This would be instead of using accumulate
Last edited on
Okay so how would I get the average

would it be average = "exams.txt." / 20 ??
I recommend:

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

int main()
{
  double aValue[20];
  double total = 0.0;

  std::ifstream ifs ("exams.txt", std::ifstream::in);

  for (int i (0) ; i < 20 ; ++i)
  {
      ifs >> aValue[i];
      total += aValue[i];
  }
  total /= 20.0;
  ifs.close();

  std::cout << total << std::endl;

  return 0;
}
OMG !! thank you so much it displaced the average !
Topic archived. No new replies allowed.