In: Computer Science
C++
Task 1
Write a program that allocates an array large enough to hold 5 student test scores. Once all the scores are entered, the array should be passed to a function that sorts them in ascending order. Another function should be called that calculates the average score. The program should display the sorted list of scores and averages with appropriate headings.
Input Validation: Do not accept negative numbers for test scores.
Task 2
Modify the program so the lowest test score is dropped. This score should not be included in the calculation of the average.
Task 3
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 score.
C++
Task 1:
#include <iostream>
#include<iomanip>
using namespace std;
void arrSelectSort(double *, int);
double showAverage(double *, int);
int main()
{
double *scores,total = 0.0, average;
int numScores;
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many test scores
scores = new double[numScores];
if (scores == NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}
//sort the elements of the array pointers
arrSelectSort(scores, numScores);
cout << "The test scores in ascending order are: \n";
average=showAverage(scores, numScores);
for (int count = 0; count< numScores; count++)
cout << scores[count] << " ";
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Average Score: " << average << endl;
return 0;
}
void arrSelectSort(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
double showAverage(double *scores, int numScores)
{
double average,total;
for (int count = 0; count < numScores; count++)
{
total += scores[count];
}
//Calculate the average
average = total / numScores;
return average;
}
OUTPUT:-
How many test scores would you like to process?5
Enter the test scores below.
Test score #1: 78
Test score #2: 52
Test score #3: 63
Test score #4: 25
Test score #5: 84
The test scores in ascending order are:
25 52 63 78 84
Average Score: 60.40
Task2:-
#include <iostream>
#include<iomanip>
using namespace std;
void arrSelectSort(double *, int);
double showAverage(double *, int);
int main()
{
double *scores,total = 0.0, average;
int numScores;
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many test scores
scores = new double[numScores];
if (scores == NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}
//sort the elements of the array pointers
arrSelectSort(scores, numScores);
cout << "The test scores in ascending order are: \n";
average=showAverage(scores, numScores);
for (int count = 0; count< numScores; count++)
cout << scores[count] << " ";
cout << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Average Score (Where lowest test score not included): " << average << endl;
return 0;
}
void arrSelectSort(double *array, int size)
{
int startScan, minIndex;
double minElem;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
array[startScan] = minElem;
}
}
double showAverage(double *scores, int numScores)
{
double average,total;
for (int count = 1; count < numScores; count++) //loweset test score not included
{
total += scores[count];
}
//Calculate the average
average = total / (numScores-1);
return average;
}
OUTPUT:-
How many test scores would you like to process?5
Enter the test scores below.
Test score #1: 52
Test score #2: 41
Test score #3: 85
Test score #4: 63
Test score #5: 62
The test scores in ascending order are:
41 52 62 63 85
Average Score (Where lowest test score not included):
65.50
Task3:-
#include <iostream>
#include<iomanip>
using namespace std;
void arrSelectSort(double *,string *, int);
int main()
{
double *scores,total = 0.0, average;
string *student;
int numScores;
cout << "How many test scores would you like to process?";
cin >> numScores;
//Dynamically allocate an array large enough to hold that many test scores
scores = new double[numScores];
student=new string[numScores];
if (scores == NULL)
return 0;
//Get the test score for each test
cout << "Enter the test scores below.\n";
for (int count = 0; count < numScores; count++)
{
cout << "Student name #" << (count + 1) << ": ";
cin>>student[count];
cout << "Test score #" << (count + 1) << ": ";
cin >> scores[count];
while (scores[count] <= 0)
{
cout << "Zero or negative numbers not accepted.\n";
cout << "Test Score #" << (count + 1) << ": ";
cin >> scores[count];
}
}
//sort the elements of the array pointers
arrSelectSort(scores,student, numScores);
cout << "The test scores in ascending order are: \n";
//average=showAverage(scores, numScores);
for (int count = 0; count< numScores; count++)
cout << "Student name: "<<student[count]<<" Student score: "<<scores[count] << " \n";
cout << endl;
return 0;
}
void arrSelectSort(double *array,string student[], int size)
{
int startScan, minIndex;
double minElem;
string s;
for (startScan = 0; startScan < (size - 1); startScan++)
{
minIndex = startScan;
minElem = array[startScan];
s=student[startScan];
for (int index = startScan + 1; index < size; index++)
{
if (array[index] < minElem)
{
minElem = array[index];
s=student[index];
minIndex = index;
}
}
array[minIndex] = array[startScan];
student[minIndex]=student[startScan];
array[startScan] = minElem;
student[startScan]=s;
}
}
OUTPUT:-
How many test scores would you like to process?5
Enter the test scores below.
Student name #1: ram
Test score #1: 42
Student name #2: sam
Test score #2: 45
Student name #3: jadu
Test score #3: 86
Student name #4: tapas
Test score #4: 54
Student name #5: amit
Test score #5: 63
The test scores in ascending order are:
Student name: ram Student score: 42
Student name: sam Student score: 45
Student name: tapas Student score: 54
Student name: amit Student score: 63
Student name: jadu Student score: 86