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 <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 average;
};
double calcAverage(int exams[]);
void display(student *students);
int main()
{
student *students = new student[NG];
for (int i = 0; i < 5; i++)
{
students[i].name = names[i];
for (int j = 0; j < 4; j++)
{
students[i].exams[i] = exams[i][j];
}
students[i].average = calcAverage(students[i].exams);
}
display(students);
return 0;
delete[] students;
students = NULL;
}
double calcAverage(int exams[])
{
double average = 0;
int total = 0;
for (int i = 0; i < 4; i++)
{
total += exams[i];
}
average = total / NG;
return average;
}
void display(student *students)
{
for (int i = 0; i < 5; i++)
{
cout << i + 1 << ". Name: " << students[i].name << " Avg: " << students[i].average << '\n';
}
cout << endl;
|