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++
Code for task 1:
#include <iostream>
using namespace std;
int average(double arr1[5]){
double
scoreAverage=(arr1[0]+arr1[1]+arr1[2]+arr1[3]+arr1[4])/5;
cout<<"Average: "<<scoreAverage<<endl;
}
int sort(double arr[5]){
int i,j,tmp;
for(i=0; i<4; i++)
{
for(j=i+1; j<5; j++)
{
if(arr[j] <arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
cout<<"Sorted Array: "<<arr[0]<<"
"<<arr[1]<<" "<<arr[2]<<"
"<<arr[3]<<" "<<arr[4]<<endl;
average(arr);
}
int main()
{
double testScores[5];
for(int i=0;i<5;i++){
do{ cout<<"Enter the score of student "<<i+1<<":
";
cin>>testScores[i];}while(testScores[i]<0);
}
sort(testScores);
return 0;
}
Code for task 2:
#include <iostream>
using namespace std;
int average(double arr1[5]){
double scoreAverage=(arr1[1]+arr1[2]+arr1[3]+arr1[4])/4;
cout<<"Average without the least score:
"<<scoreAverage<<endl;
}
int sort(double arr[5]){
int i,j,tmp;
for(i=0; i<4; i++)
{
for(j=i+1; j<5; j++)
{
if(arr[j] <arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
}
}
}
cout<<"Sorted Array: "<<arr[0]<<"
"<<arr[1]<<" "<<arr[2]<<"
"<<arr[3]<<" "<<arr[4]<<endl;
average(arr);
}
int main()
{
double testScores[5];
for(int i=0;i<5;i++){
do{ cout<<"Enter the score of student "<<i+1<<":
";
cin>>testScores[i];}while(testScores[i]<0);
}
sort(testScores);
return 0;
}
Code for Task 3:
#include <iostream>
#include<string>
using namespace std;
int average(double arr1[5]){
double scoreAverage=(arr1[1]+arr1[2]+arr1[3]+arr1[4])/4;
cout<<"Average without the least score:
"<<scoreAverage<<endl;
}
int sort(double arr[5],string str[5]){
int i,j,tmp;
string tmp2;
for(i=0; i<4; i++)
{
for(j=i+1; j<5; j++)
{
if(arr[j] <arr[i])
{
tmp = arr[i];
arr[i] = arr[j];
arr[j] = tmp;
tmp2 = str[i];
str[i] = str[j];
str[j] = tmp2;
}
}
}
cout<<"Sorted Array: "<<endl;
cout<<"Score of "<<str[0]<<" is
"<<arr[0]<<endl;
cout<<"Score of "<<str[1]<<" is
"<<arr[1]<<endl;
cout<<"Score of "<<str[2]<<" is
"<<arr[2]<<endl;
cout<<"Score of "<<str[3]<<" is
"<<arr[3]<<endl;
cout<<"Score of "<<str[4]<<" is
"<<arr[4]<<endl;
average(arr);
}
int main()
{
string studentName[5];
double testScores[5];
for(int i=0;i<5;i++){
cout<<"Enter the student's name: ";
cin>>studentName[i];
do{ cout<<"Enter the score of
"<<studentName[i]<<": ";
cin>>testScores[i];}while(testScores[i]<0);
}
sort(testScores,studentName);
return 0;
}