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
|
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int NG = 4;
string names[] = { "Amy Adams", "Bob Barr", "Carla Carr", "Dan Dobbs", "Elena Evans" };
int exams[][NG] = { { 98, 87, 93, 88 },
{ 78, 86, 82, 91 },
{ 66, 71, 85, 94 },
{ 72, 63, 77, 69 },
{ 91, 83, 76, 60 } };
struct student
{
string name;
int exams[NG];
double avg;
};
float findavg(int exams[]);
void display(student *students);
int main()
{
student *students = new student[NG];
for (int i = 0; i < NG; i++)
{
students[i].name = names[i];
for (int j = 0; j < NG; j++)
{
students[i].exams[i] = exams[i][j];
}
students[i].avg = findavg(students[i].exams);
}
display(students);
}//main
float findavg(int exams[])
{
float avg;
float sum = 0;
for (int i = 0; i < NG; i++)
{
sum += exams[i];
avg = sum / NG;
}
return avg;
}//findavg
void display(student *students)
{
cout << "SN " << setw(7) << "Name " << setw(12) << "Avg " << endl;
for (int i = 0; i < NG; i++)
{
cout << i + 1 << setw(4) << " " << students[i].name << setw(6) << " " << students[i].avg << endl;
}
}//display
|