Calculating average using struct, vector, and for loop.

Hi as the title says I need to use a struct, vector, and for loop to calculate the average mark.
The program runs but it does not calculate the average correctly.

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
#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

struct Record{
	float mark;
};

vector<Record> a;
float ave(vector<Record> &a);

int main()
{
	Record b;
	int student;
	
cout << "how many students?: ";
cin >> student;

for(int i=0;i<student;i++)
{
cout << "enter mark - ";
cin >> b.mark;
a.push_back(b);
}

float ave(vector<Record> &a);

cout << ave;

}


float ave(vector<Record> &a){
	float sum;
	
	for(int i=0; i<a.size();i++)
	{
		sum += a[i].mark;
	}
	float avg = sum/a.size();
	return avg;
}
Firstly, Use meaningful variable names, A lot of problems are solved by an easy to read code.
IE your variables make more sense like:
vector<Record> StudentScores
int NumberOfStudents
Record StudentGrades

I believe You've declared your struct wrong.
You declared a struct with only float in it, which is pointless to have a struct containing one variable.
Then followed creating a vector of that struct, which is the same as just creating a vector of floats. Since your struct only contains float.
Basically useless when you could just do a vector of whatever type you wanted rather. Unless the teacher wants a struct containing one variable.

You're also calling your function incorrectly.
You are redeclaring it rather than calling it in main
1
2
3
4
float ave(vector<Record> &a);
// should be
ave(a);
//This might be why you arent printing what you want to. I imagine its not returning anything significant. 


(also you cant cout ave since its not declared properly, it should be cout << (ave(a)) if you wanted to cout average in main.)

Followed by your function not initializing sum. How can you add to sum if you dont know its value?

I would begin by fixing these issues then taking a second look. Writing an algorithm before you begin will benefit you.

Sorry for all the edits, Just kept trying to give more help lol
Last edited on
Attempt to fix your code before looking ;)
I did not use struct, But it is easily modifiable to include that.
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

#include <iostream>
#include <vector>
#include <cmath>
using namespace std;

int StudentAverage(vector<int> &StudentScores);

int main()
{
    
    int StudentGrade;
    vector<int> ListStudentScores;
    int NumberOfStudents;
	
    cout << "How many students in the class?: ";
    cin >> NumberOfStudents;

    for(int i = 0; i < NumberOfStudents; i++)
    {
        cout << "Enter Student # " << i + 1 << "'s Score: ";
        cin >> StudentGrade;
        ListStudentScores.push_back(StudentGrade);
    }

    cout << "Class Average Is: " << StudentAverage(ListStudentScores);

return 0;

}


int StudentAverage(vector<int> &ListStudentScores)
{
	int sum;
	sum = 0;
	for(int i=0; i<ListStudentScores.size();i++)
	{
		sum = sum + ListStudentScores[i];
	}
	int Average = sum/ListStudentScores.size();

	return Average;
}
Topic archived. No new replies allowed.