Type your question here Currently student grade records are stored parallel arra
ID: 3534975 • Letter: T
Question
Type your question here
Currently student grade records are stored parallel arrays.<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
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}
};
We want to convert to an array of structures that is
allocated on the Heap with additional exam average field.
Your tasks are:
1. define a structure for student grade records.
2. declare an array of records and allocate on the Heap.
3. populate from the parallel arrays.
4. define a function to compute average and
populate average field.
5. define a function to display name and average.
6. demonstrate your functions and program
Explanation / Answer
#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];
float 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);
return 0;
}
float findavg(int exams[]) {
float avg = 0;
for (int i = 0; i < NG; i++) {
avg += exams[i];
}
return avg/NG;
}
void display(student *students) {
for (int i = 0; i < NG; i++) {
cout<<i+1<<". Name: " << students[i].name << " Avg: " << students[i].avg << endl;
}
}
output:
1. Name: Amy Adams Avg: 22
2. Name: Bob Barr Avg: 22.75
3. Name: Carla Carr Avg: 23.5
4. Name: Dan Dobbs Avg: 17.25
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.