Modify the program to allow the user to enter name-score pairs. For each student
ID: 3665602 • Letter: M
Question
Modify the program to allow the user to enter name-score pairs. For each student taking a test, the user types the student's name followed by the student's integer test score. Modify the sorting function so it takes an array holding the student names and an array holding the student test scores. When the sorted list of scores is displayed, each student's name should be displayed along with his or her socre. In stepping through the arrays, use pointers rather than array subscripts.
________________________________________________________________________________
#include <iostream>
#include <iomanip>
using namespace std;
//Function prototypes
void arrSelectSort(double *, int);
double arrAvgScore(double *, int);
int main()
{
//Define variables
double *TestScores,
total = 0.0,
average;
int numTest,
count;
//Get the number of test scores you wish to average and put in order
cout << "How many test scores do you wish to enter?" << endl;
cin >> numTest;
//Dynamically allocate an array large enough to hold that many scores
TestScores = new double[numTest];
//Get the test scores
cout << "Enter the test scores below. ";
for (count = 0; count < numTest; count++)
{
//Display score
cout << "Test Score " << (count + 1) << ": ";
cin >> TestScores[count];
// Input validation. Only numbers between 0-100
while (TestScores[count]<0 || TestScores[count]>99)
{
cout << "You must enter a scores that non-negative" << endl;
cout << "Please enter again: ";
cin >> TestScores[count];
}
}
//Dsiplay the results
arrSelectSort(TestScores, numTest);
average = arrAvgScore(TestScores, numTest);
cout << fixed << showpoint << setprecision(2);
cout << "The test scores, sorted in ascending order, are: ";
for (count = 0; count < numTest; count++)
cout << TestScores[count] << " ";
cout << endl;
cout << "The average of all the test score is " << average << endl;
//Free dynamically allocated memory
delete[] TestScores;
TestScores = 0; //make TestScores point to null
//Display the Test Scores in ascending order
system("pause");
return 0;
}
//Ascending order selection sort
void arrSelectSort(double *arr, int size)
{
int startScan;
int minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = arr[startScan];
for (int index = startScan; index < size; index++)
{
if (arr[index] < minElem)
{
minElem = arr[index];
minIndex = index;
}
}
if (minIndex != startScan)
{
double temp = arr[minIndex];
arr[minIndex] = arr[startScan];
arr[startScan] = temp;
}
}
}
double arrAvgScore(double *arr, int size)
{
double total = 0, average;
int numTest;
for (int count = 0; count < size; count++)
{
total += arr[count];
}
average = total / size;
return average;
}
Explanation / Answer
Please find below the code to achive the requirement mentioned by you :
#include “stdafx.h”
#include<iostream>
#include<string>
using namespace std;
void sortScore(int *,int,string* );
int main()
{
int numberOfStudents;
cout<<“Enter the number of students: “;
cin>>numberOfStudents; //Input the no. of students//
int* score=new int[numberOfStudents];
string* nameOfStudent=new string[numberOfStudents];
for(int i=0;i<numberOfStudents;i++)
{
cout<<“ Enter the name of “<<i+1<<” student: “;
cin>>nameOfStudent[i];
cout<<“ Enter the score of the student: “;
cin>>score[i];
}
sortScore(score, numberOfStudents,nameOfStudent);
cout<<“ List of Scores in ascending order”;
for(int i=0;i<numberOfStudents;i++)
{
cout<<“ ”<<nameOfStudent[i]<<“ ”<<score[i];
}
cout<<endl;
delete [] score;
score=0;
system(“pause”);
return 0;
}
void sortScore(int *score,int size,string* name)
{
int startScan,minIndex;
int minValue;
string minName;
for(startScan=0;startScan<(size-1);startScan++)
{
minIndex=startScan;
minValue=score[startScan];
for(int index=startScan+1;index<size;index++)
{
if(score[index]<minValue)
{
minValue=score[index];
minIndex=index;
minName=name[index];
}
}
score[minIndex]=score[startScan];
score[startScan]=minValue;
name[minIndex]=name[startScan];
name[startScan]=minName;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.