Something wrong with the program - Modify the program to allow the user to enter
ID: 3665605 • Letter: S
Question
Something wrong with the program - 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<string>
using namespace std;
// function
float Average(int *scores, int number);
void SortTestScores(int*scores, string *Names, int n);
void main()
{
//variable deslaration
int *testScores = new int[50];
string *Names = new string[50];
float averageScore;
int number, i;
cout << "Enter number of test Scores";
cin >> number;
for (i = 0; i < number; i++)
{
cout << "Enter the names";
cin >> Names[i];
cout << "Enter test Scores";
cin >> testScores[i];
}
SortTestScores(testScores, Names, number);
cout << "Name " << "TestScore" << endl;
for (i = 0; i < number; i++)
{
cout << Names[i] << " ";
cout << testScores[i] << endl;
}
averageScore = Average(testScores, number);
cout << "Average testScore is:" << averageScore << endl;
system("pause");
}
void SortTestScores(int *scores, string *Names, int number)
{
int temp;
string tempname;
int i, j;
for (i = 1; i < number; i++){
for (j = 0; j < number; j++)
{
if (scores[j]>scores[j + 1]){
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
tempname = Names[j];
Names[j] = Names[j + 1];
Names[j + 1] = tempname;
}
}
}
}
float Average(int*score, int n){
int i;
float avg = 0.0;
for (i = 1; i < n; i++){
avg += score[i];
}
avg = avg / n;
return avg;
}
Explanation / Answer
Bellow is the Modified code:
#include<iostream>
#include<string>
using namespace std;
// function
float Average(int *scores, int number);
void SortTestScores(int*scores, string *Names, int n);
int main()
{
//variable deslaration
int *testScores = new int[50];
string *Names = new string[50];
float averageScore;
int number, i;
cout << "Enter number of test Scores :";
cin >> number;
for (i = 0; i < number; i++)
{
cout << "Enter the names : ";
cin >> Names[i];
cout << "Enter test Scores : ";
cin >> testScores[i];
}
SortTestScores(testScores, Names, number);
cout << "Name " << "TestScore" << endl;
for (i = 0; i < number; i++)
{
cout << Names[i] << " ";
cout << testScores[i] << endl;
}
averageScore = Average(testScores, number);
cout << "Average testScore is:" << averageScore << endl;
system("pause");
}
void SortTestScores(int *scores, string *Names, int number)
{
int temp;
string tempname;
int i, j;
for (i = 1; i < number; i++){
for (j = 0; j < i; j++)
{
if (scores[j]>scores[j + 1]){
temp = scores[j];
scores[j] = scores[j + 1];
scores[j + 1] = temp;
tempname = Names[j];
Names[j] = Names[j + 1];
Names[j + 1] = tempname;
}
}
}
}
float Average(int*score, int n){
int i;
float avg = 0.0;
for (i = 0; i < n; i++){
avg += score[i];
}
avg = avg / n;
return avg;
}
Out Put:
Enter number of test Scores :4
Enter the names : Naidu
Enter test Scores : 10
Enter the names : Rajev
Enter test Scores : 30
Enter the names : chandu
Enter test Scores : 5
Enter the names : deva
Enter test Scores : 25
Name TestScore
chandu 5
Naidu 10
deva 25
Rajev 30
Average testScore is:17.5
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.