In: Computer Science
In C++
Modify the program #1 to allow the user to enter name-score pairs. For each student taking a test, the user types a string representing the name of the student, followed by an integer representing the student's score. (use a structure) Modify the average-calculating function so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers notation rather than array indices. (myArray->name or *(myArray).name) Make it a class. You will need a destructor because you are using dynamic arrays.
I need your help! This is fundamental II. Thank you
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//function declaration
void array_sort(double *, string *, int);
double calculate_avg(double *, int);
int main()
{
//variable to store number of student test
int no_of_test;
cout << "Enter Number of test : ";
cin >> no_of_test;
//dynamic array to store student name and their test score
double *test_score = new double[no_of_test];
string *student_name = new string[no_of_test];
//fetch values into array using loop
for(int counter = 0; counter < no_of_test; counter++)
{
cout << "Student " << counter + 1 << " name:
";
//use of pointer notation
cin >> *(student_name + counter);
cout << "Test score: ";
cin >> *(test_score + counter);
//check input is valid or not
while(*(test_score + counter) < 0)
{
cout << "ERROR! Negative values not allowed!";
cout << " Enter again!\n";
cin >> *(test_score + counter);
}
}
//call array_sort() for sorting array
array_sort(test_score, student_name, no_of_test);
//format output
cout << fixed << setprecision(1);
//display data of arrays
cout << "\n";
cout << "Sorted test scores are: \n";
cout << "Name\t\tScore\n";
for(int counter = 0; counter < no_of_test; counter++)
{
cout << *(student_name + counter) << "\t\t";
cout << *(test_score + counter) << endl;
}
//for average
cout << "\n\nAverage of " << no_of_test << "
tests is: ";
//call calculate_avg() to calculate average
cout << calculate_avg(test_score, no_of_test);
//return 0 to mark successful termination
return 0;
}
//sorting array by array_sort()
void array_sort(double *ptr, string *name_ptr, int
array_size)
{
//temporary variable to help in swapping
double temp;
string temp_name;
//boolean variable that will control when array is done
sorting
bool swap = true;
while(swap)
{
swap = false;
//loop on all elements
for(int counter = 0; counter < array_size - 1; counter++)
{
//check if element is > current element or not and if yes then
perform swap
if(*(ptr+counter) > *(ptr+counter+1))
{
temp = *(ptr+counter+1);
temp_name = *(name_ptr+counter+1);
*(ptr+counter+1) = *(ptr+counter);
*(name_ptr+counter+1) = *(name_ptr+counter);
*(ptr+counter) = temp;
*(name_ptr+counter) = temp_name;
//update boolean variable after done
swap = true;
}
}
}
}
//calulate average
double calculate_avg(double *ptr, int array_size)
{
double total = 0.0;
//loop on values of array
for(int counter = 0; counter < array_size; counter++)
{
total =total + *(ptr + counter);
}
//typecast
return (double)total/array_size;
}