Write a program which: 1. Prompts the user for 5 test scores as integers greater
ID: 3818228 • Letter: W
Question
Write a program which:
1. Prompts the user for 5 test scores as integers greater than or equal to zero and less than or equal to 100. 2. Validates that the user entries are valid (integers greater than or equal to zero and less than or equal to 100). 3. Stores the 5 tests scores in an array. 4. Uses the bubble sort to sort the scores in ascending order. 5. Calculates the letter grade for each test 6. Displays the letter grade of each score as well as the overall average (the average of the 5 tests).
SPECIFIC REQUIREMENTS
1. You must include a method called calcAverage to compute the average of the 5 test scores; it takes an array of type int as a parameter and returns the average. 2. You must include a method called validateUserInput to ensure user entries are integers greater than or equal to zero and less than or equal to 100. 3. You must include a method determineGrade to compute the average of the 5 test scores; it takes an int array with the test scores and a char array to store the corresponding letter grades. The letter grade is arrived at using the table below:
score Letter Grade >=90 A >=80 and <90 B >=70 and <80 C >=60 and <70 D >=0 and <60 F 4. You must include a method called bubbleSort which sorts the values (test scores) stored in the array in ascending order. 5. You must include a method called displayTestScores which displays each test score and its corresponding letter, as well as the overall average.
1. No infinite loops, examples include: a. for(;;) b. while(1) c. while(true) d. do{//code}while(1); 2. No break statements to exit loops
Explanation / Answer
#include <iostream>
using namespace std;
int validateUserInput(int temp)
{
if(temp<0 || temp>100)
{
return 1;//invalid
}
return 0;//valid
}
void bubbleSort(int scores[])
{
int temp1;
for(int i=0;i<5;i++)
{
for(int j=0;j<5-1-i;j++)
{
if(scores[j]>scores[j+1])
{
temp1 = scores[j];
scores[j] = scores[j+1];
scores[j+1] = temp1;
}
}
}
for(int i=0;i<5;i++)
{
cout<<scores[i]<<" ";
}
}
float calcAverage(int scores[])
{
int sum=0;
for(int i=0;i<5;i++)
{
sum+=scores[i];
}
return sum/5;
}
void calcGrades(int scores[], char grades[])
{
for(int i=0;i<5;i++)
{
if(scores[i]>=90)
grades[i] = 'A';
else if(scores[i]>=80 && scores[i]<90)
grades[i] = 'B';
else if(scores[i]>=70 && scores[i]<80)
grades[i] = 'C';
else if(scores[i]>=60 && scores[i]<70)
grades[i] = 'D';
else
grades[i] ='F';
}
}
displayTestScores(int scores[], char grades[])
{
for(int i=0;i<5;i++)
{
cout<<" subject "<<i+1<<" score: "<<scores[i]<<" grade: "<<grades[i];
}
}
int main ()
{
int scores[5]; char grades[5];
int temp; int temp1;
int flag=0;
for(int i=0;i<5;i++)
{
cout<<" enter marks in subject "<<i+1<<" :";
cin>>temp;
flag = validateUserInput(temp);
if(flag)
{
i--;
cout<<"last entered score was invalid,enter again. ";
}
else
{
scores[i]=temp;
flag= 0;
}
}
cout<<" scores in ascending order: ";
bubbleSort(scores);
cout<<" average score :"<<calcAverage(scores);
calcGrades(scores,grades);
cout<<" scores and corresponding grades:";
displayTestScores(scores,grades);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.