Computing Average

// Hello, I need some help computing averages after reading from a file. The parameters for my computeAvg function has an error.

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
89
90
91
92
93
94
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

struct person
{
    string name;
    int age;
    float gpa;
};

void copyData(string fName, person x[]);
void displayData(person x[]);
void displayTeens(person x[]);
void computeAvg(person x[], float& ageAvg, float& gpaAvg);
void displayAvg(float& ageAvg, float& gpaAvg);

int main()
{
    person a[5];

    cout << "This is the content of array a: " << endl;
    copyData("data.txt", a);

    displayData(a);

    cout << "This is the list of teenagers: " << endl;
    displayTeens(a);

    cout << "The average age is: ";
	displayAvg(ageAvg, gpaAvg);

    system("pause");
    return 0;
}

void copyData(string fName, person x[])
{
    fstream f;

    f.open("data.txt", ios::in);

    for(int i = 0; i < 5; i++)
    {
        f >> x[i].name >> x[i].age >> x[i].gpa;
    }

    f.close();

}

void displayData(person x[])
{
    for (int i = 0; i < 5; i++)
    {
        cout << x[i].name << " " << x[i].age << " " << x[i].gpa << endl;
    }
}

void displayTeens(person x[])
{
    for(int i = 0; i < 5; i++)
    {
        if((x[i].age > 12) && (x[i].age < 20))
        {
            cout << x[i].name << endl;
        }
    }
}

void computeAvg(person x[], float& ageAvg, float& gpaAvg)
{
    float ageTotal = 0;
    float gpaTotal = 0;

    for(int i = 0; i < 5; i++)
    {
        ageTotal += x[i].age;
        gpaTotal += x[i].gpa;
    }

    ageAvg = ageTotal / 5;
    gpaAvg = gpaTotal / 5;
}

void displayAvg(float& ageAvg, float& gpaAvg)

{
	cout << ageAvg;
	cout << gpaAvg;
}


Last edited on
Please, before anything else, edit your post and put the text [code] and [/code] around your code so that it is formatted.
e.g.
1
2
3
4
5
#include <iostream>
int main()
{
    std::cout << "Proper formatting & indenting!\n";
}


Also note that you are only giving your person array a size of 5, but you are attempting to iterate over 6 people. That will go out of bounds of your array.
Last edited on
Okay, thank you. I fixed the array size, but I still need help computing the averages.
Last edited on
You'd get help much faster if you told us what the error actually was, so that we would know what to look for.

 In function 'int main()':
32:13: error: 'ageAvg' was not declared in this scope
32:21: error: 'gpaAvg' was not declared in this scope
 

You never declare ageAvg or gpaAvg as variables.

Need to do something like:
1
2
3
4
5
    cout << "The average age is: ";
    float ageAvg;
    float gpaAvg;
    computeAvg(a, ageAvg, gpaAvg); //edited, I previously had written something wrong here
    displayAvg(ageAvg, gpaAvg);
Last edited on
Student1904. First you need to call the computeAvg function, which computes the average. Second your computeAvg takes 3 arguments (Array struct, float, float).
 
computeAvg(a, ageAvg, gpaAvg); 


You also need to create a local variable ageAvg and gpaAvg.
1
2
float ageAvg; 
float gpaAvg; 


Your int main should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{
    person a[5];
    float ageAvg; 
    float gpaAvg;

    cout << "This is the content of array a: " << endl;
    copyData("data.txt", a);

    displayData(a);

    cout << "This is the list of teenagers: " << endl;
    displayTeens(a);

    cout << "The average age is: ";
    computeAvg(a, ageAvg, gpaAvg);
    displayAvg(ageAvg, gpaAvg);

    system("pause");
    return 0;
}


- By the way, I believe that computeAvg and displayAvg are redundant. Due to ageAvg and gpaAvg being local variable which you can simply print.
Topic archived. No new replies allowed.