Related questions with answers
Recall the program that dynamically allocates an array large enough to hold a user-defined number of test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function was called that calculates the average score. The program displayed the sorted list of scores and averages with appropriate headings. It user pointer notation rather than array notation whenever possible.
Modify that program 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. Modify both the sorting and average-calculating functions so they take arrays of structures, with each structure containing the name and score of a single student. In traversing the arrays, use pointers rather than array indices.
Solutions
VerifiedIn this exercise we are going to write program that asks user to input size of the array, and then makes array of structures student. Structure student is structure defined with two properties - name (string) and score (double). Program asks user for input for each student (element of the array). Then, program prints out array of structures student sorted in ascending order with average score. Let’s start by importing libraries and defining namespace.
#include <iostream>
using namespace std;
Firstly, lets specify our program constraints,
- Arrays should be .
- such that no score is negative.
- both names, scores based on the scores.
- Use through the array rather than subscripts.
//contents of main.cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
//function prototypes
void sortArray(double *, string *, int);
double getAverage(double *, int);
int main()
{
//variable to store user input
//for number of tests
int numTests;
//prompt user to enter input then read
cout << "Please enter number of test scores ";
cout << "that will be stored:\n";
cin >> numTests;
//dynamically allocate array large enough
//for scores and student names
double *testScores = new double[numTests];
string *studentNames = new string[numTests];
//read values into array using for loop
for(int counter = 0; counter < numTests; counter++){
cout << "Student " << counter + 1 << " name: ";
//use pointer notation, don't forget
//to dereference first
cin >> *(studentNames + counter);
cout << "Test score: ";
cin >> *(testScores + counter);
//validate input
while(*(testScores + counter) < 0){
//print error message then prompt and read
cout << "ERROR! Negative values not allowed!";
cout << " Enter again!\n";
cin >> *(testScores + counter);
}
}
//sort arrays
sortArray(testScores, studentNames, numTests);
//format output
cout << fixed << setprecision(1);
//display contents of array along with names
cout << endl;
cout << "Sorted test scores are: \n";
cout << "Name\t\tScore\n";
for(int counter = 0; counter < numTests; counter++){
cout << *(studentNames + counter) << "\t\t";
cout << *(testScores + counter) << endl;
}
//print message for average
cout << "\n\nAverage of " << numTests << " tests is: ";
cout << getAverage(testScores, numTests);
//return 0 to mark successful termination
return 0;
}
//function to sort arrays
void sortArray(double *ptr, string *namesPtr, int arraySize){
//temporary variable to help in swapping
double temp;
string tempName;
//boolean variable that will control when
//array is done sorting
bool swapped = true;
while(swapped){
swapped = false;
//loop on all elements
for(int counter = 0; counter < arraySize - 1; counter++){
//check if following element is greater
//than current element
if(*(ptr+counter) > *(ptr+counter+1)){
//if so, perform swap
temp = *(ptr+counter+1);
tempName = *(namesPtr+counter+1);
*(ptr+counter+1) = *(ptr+counter);
*(namesPtr+counter+1) = *(namesPtr+counter);
*(ptr+counter) = temp;
*(namesPtr+counter) = tempName;
//and update boolean variable
swapped = true;
} //if ends here
} //for loop ends here
} //while loop ends here
}
//function to get average
double getAverage(double *ptr, int arraySize){
//accumulator variable
double total = 0.0;
//loop on all values of array, except from
//first one (the lowest score which will be dropped)
for(int counter = 0; counter < arraySize; counter++){
total += *(ptr + counter);
}
//use typecast to double to
//prevent integer division
return (double)total/arraySize;
}
Create a free account to view solutions
Create a free account to view solutions
Recommended textbook solutions

Starting Out with C++: Early Objects
8th Edition•ISBN: 9780133360929Godfrey Muganda, Judy Walters, Tony Gaddis
Fundamentals of Database Systems
7th Edition•ISBN: 9780133970777Ramez Elmasri, Shamkant B. Navathe
Introduction to Algorithms
3rd Edition•ISBN: 9780262033848Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. Cormen
Introduction to Algorithms
4th Edition•ISBN: 9780262046305Charles E. Leiserson, Clifford Stein, Ronald L. Rivest, Thomas H. CormenMore related questions
- computer science
- computer science
- computer science
- computer science
- computer science
1/4
- computer science
- computer science
- computer science
- computer science
- computer science
1/7