how do calculating the avg?

How do i calculate the avg i got and divide by the number of students that read from a file? without asking the user how many students? also lining data same column!

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

//function prototpes.
void ProcessOverall(string, int, int, int, int& min, int& max, float& avg);


int main ()

{
  //variable
  string name;
  int test1, test2, test3;
  float avg;
  int min, max;
  double overall;

 //read file from data.txt
  ifstream inputFile;
  inputFile.open("data.txt");


  cout << fixed << setprecision(2);
  cout << "Name     \tScore 1     \tScore 2     \tScore 3     \tLow     \tHigh     \tAverage" << endl;
  cout << "--------------------------------------------------------------------------------------------------------" << endl;

  while (inputFile >> name >> test1 >> test2 >> test3)
    {
     ProcessOverall(name, test1, test2, test3, min, max, avg);


    }

  cout << "__________________________________________________________________________________________________________" << endl;
  cout << "Overall average of the class is: " << overall << endl;


  //close
  inputFile.close();


  return 0;
}
void ProcessOverall(string name, int test1, int test2, int test3, int& min,int&  max, float& avg)
{

  double overall;


  min = test1;
  max = test1;
  if (test2 > max)
    max = test2;
  if (test2 < min)
    min = test2;
  if (test3 > max)
    max = test3;
  if (test3 < min)
    min = test3;

  avg = ((test1 + test2 + test3) / 3.0);
  overall = (avg / 6.0);

  avg = ((test1 + test2 + test3) / 3.0);
  overall = (avg / 6.0); //are there a way that i dont need to put 6.0 here?


  cout <<  name
       << setw(15) << test1
       << setw(16) << test2
       << setw(16) << test3
       << setw(16) << min
       << setw(16) << max
       << setw(19) << avg << endl;


  //calculating overall average.


}




//outcome i got
Name Score 1 Score 2 Score 3 Low High Average
--------------------------------------------------------------------------------------------------------
Bob 78 92 88 78 92 86.00
Sue 89 94 78 78 94 87.00
Fred 100 80 92 80 100 90.67
George 90 90 85 85 90 88.33
Mary 95 70 99 70 99 88.00
Jane 100 92 87 87 100 93.00
__________________________________________________________________________________________________________
Overall average of the class is: 0.00

Last edited on
Add this here (additions in bold):
17
18
    int test1, test2, test3, count = 0;
    ...


And this here:
31
32
33
34
35
    while (inputFile >> name >> test1 >> test2 >> test3)
    {
        ProcessOverall(name, test1, test2, test3, min, max, avg);
        count++;
    }


And then you should be able to do
65
66
        avg = ((test1 + test2 + test3) / 3.0);
        overall = (avg / count);


And you should be all set!

Hope this helped,
VX
Last edited on
@VX0726
i added those kind but it still says 0.00 for overall and sometime it said undef
@gunam12

I assume that 'overall' should be the average for the class, whilst avg is the average for one person.

On that basis, avg is computed correctly in line 65, but there is no need for you to repeat the same calculation (line 69).

'overall' should be computed in main(), not in your ProcessOverall() function (which isn't very aptly named). At present you are trying to compute its value in ProcessOverall() and print out its value in main() - the latter knows nothing about its value, so that is probably why you get an 'undef' or 0, depending on what your implementation decides to do with unitialised variables.

If you want an overall class average then you will have to accumulate both (e.g.) sumAvge and count variables within your while loop, then do the division of the two to get the average after the while loop has finished.
Last edited on
@lastchance


yup the 'overall' is the avg of the whole class that i'm trying to do! thank you for the input lets me see what i can do! and i'll input the code back!
hey I wanted to send you a message but it didnt allow me.
The question u first posted
"Use the Concept of vector and string to do the following question"

can u please explain that program to me ? I am new to CS as well and This was introduced to us today and we were given that exact assignment.
Topic archived. No new replies allowed.