C++ Programming, Visual Studio - Please help write program which: Prompts the us
ID: 3876881 • Letter: C
Question
C++ Programming, Visual Studio - Please help write program which:
Prompts the user for 5 test scores as integers greater than or equal to zero and less than or equal to 100.
Ensures that the user entries are valid (integers greater than or equal to zero and less than or equal to 100).
VALIDATE INPUT: Do not accept alphabetical/special characters or Enter key - "invalid entry" should appear.
VALIDATE INPUT: Do not accept integers greater than 100 - "Enter only integers greater than or equal to zero and less than or equal to 100" should appear
Stores the 5 tests scores in an array.
Uses the bubble sort to sort the scores in ascending order.
Calculates the letter grade for each test
Displays the letter grade of each score as well as the overall average (the average of the 5 tests).
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.
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.
You must include a method called bubbleSort which sorts the values (test scores) stored in the array in ascending order.
You must include a method called displayTestScores which displays each test score and its corresponding letter, as well as the overall average.
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:
RESTRICTIONS:
No global variables
No labels or go-to statements
No infinite loops, examples include: for(;;), while(1), while(true), do{//code}while(1);
No break statements
No pointers
score Letter Grade >=90 80 and =70 and =60 and =0 andExplanation / Answer
#include <iostream>
using namespace std;
//To calculate average of the test scores.
int calcAverage(int a[])
{
int sum=0;
for(int i=0;i<5;i++)
{
sum+=a[i];
}
return sum/5;
}
//to validate user's inpur for two conditions mentioned.
bool validateUserInput(int a)
{
if(cin.fail())
{
cout<<"invalid entry";
cin.clear();
std::cin.ignore(256,' ');
return false;
}
if(a>=0 && a<=100)
return true;
else
{
cout<<"Enter only integers greater than or equal to zero and less than or equal to 100";
return false;
}
}
//to swap two numbers
void swap(int *a, int *b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//to sort the scores in ascending order.
void bubbleSort(int a[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
for (j = 0; j < n-i-1; j++)
if (a[j] > a[j+1])
swap(&a[j], &a[j+1]);
}
//to display test scores.
void displayTestScores(int test_scores[],char grades[])
{
for(int i=0;i<5;i++)
{
cout<<"Test Score: "<<test_scores[i]<<" "<<"Grade :"<<grades[i]<<endl;
}
cout<<"Average of test scores :" <<calcAverage(test_scores)<<endl;
}
//to calculate grades repective to the test score.
void determineGrade(int test_scores[],char grades[])
{
for(int i=0;i<5;i++)
{
if(test_scores[i]>=90)
grades[i]='A';
else if(test_scores[i]>=80 && test_scores[i]<90)
grades[i]='B';
else if(test_scores[i]>=70 && test_scores[i]<80)
grades[i]='C';
else if(test_scores[i]>=60 && test_scores[i]<70)
grades[i]='D';
else if(test_scores[i]>=0 && test_scores[i]<60)
grades[i]='F';
}
}
int main()
{
int a,i=0;
int test_scores[5];
cout << "Hello world!" << endl;
cout<<"Enter five test scores greater than or equal to zero and less than or equal to 100."<<endl;
for(int i=0;i<5;i++){
}
//We take each input, validate it, and then put it in the test_scores array.
while(i<5)
{
cin.clear();
cin>>a;
if(validateUserInput(a))
{
test_scores[i]=a;
i++;
}
}
char grades[5];
bubbleSort(test_scores,5);
determineGrade(test_scores,grades);
displayTestScores(test_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.