I have to write a code, where a user enters a number of students and their surnames (then they will get 5 different grades). Their averages have to be calculated and displayed. Next, the overall average (of the students' averages) has to be calculated. I've been trying to get it but I always get weird numers... Could anyone have a look at it and help me? And I've got another question. What should I do to enter a surname and get the person's data displayed?
#include <iostream>
#include <iomanip>
using namespace std;
struct STUDENT
{
string surname;
double grades[5];
double average;
};
void random(STUDENT &a);
void display(STUDENT a);
void random(STUDENT &a){
srand(time(NULL));
cin >> a.surname;
for(int i = 0; i < 5; i++) {
for (int i = 0; i < 5; ++i) {
double locen[7]={2, 2.5, 3, 3.5, 4, 4.5, 5};
int wyboc = rand()%(7);
a.grades[i] = locen[wyboc];
}
}
}
void display(STUDENT a){
double sum = 0;
cout << a.surname << endl;
for(int i=0; i <5; i++){
cout<< "Grades " << i + 1 << ": " << setprecision(2) << a.grades[i] << endl;
sum += a.grades[i];
a.average = sum/5;
}
cout << "Average is: " << a.average;
}
double average(STUDENT a, int numberOfStudents){
double sum = 0;
double sumAverage = 0;
for (int i = 0; i < numberOfStudents; i++){
sum += a.grades[i];
}
sumAverage = a.average / numberOfStudents;
cout << "All students' average: " << sumAverage << endl;
}
int main(){
int numberOfStudents;
cout << "Enter number of students: ";
cin >> numberOfStudents;
if (numberOfStudents <= 0){
cout << "You cannot enter 0" << endl;
return -1;
}
STUDENT tab[numberOfStudents];
for (int i = 0; i < numberOfStudents; i++){
cout << "Enter surname " << (i + 1) << ": ";
random(tab[i]);
}
for(int i=0;i<numberOfStudents;i++){
cout<<"\nStudent "<<i+1<<": ";
display(tab[i]);
}
average(a, numberOfStudents);
}