In: Computer Science
1. Enter user name. 2. Enter test scores. 3. Display average. 4. Display summary. 5. Quit. Selection:
c++ please
for while do while
NOTE: If you have any query regarding the solution, please ask in comment section! HAPPY LEARNING!!
There will not be too much difference in the code for language c and c++, but still I have provided the code in both language for better understanding, and the code is done in the same way as in problem statement explained.
CODE (C++):
#include<iostream>
using namespace std;
void display_average(float scores[]){ // FUNCTION FOR DISPLAY AVERAGE
float sum = 0.0;
for(int i=0;i<3;i++)
sum += scores[i]; // ADDED ALL THE SCORES AND DIVIDED BY NUMBER OF SCORES TO CALCULATE AVERAGE
float average = sum/3;
cout<<"Your average score is "<<average<<endl; // PRINTED THE AVERAGE
}
void display_summary(char name[],float scores[]){ // FUNCTION FOR DISPLAY SUMMARY
float sum = 0.0;
for(int i=0;i<3;i++)
sum += scores[i];
float average = sum/3; // CALCULATED THE AVERAGE IN THE SAME WAY AS ABOVE
char grade;
if (average>=90) // CALCULATED THE GRADE ACCORDING TO THE AVERAGE CALCULATED
grade = 'A';
else if (average>=80)
grade = 'B';
else if (average>=70)
grade = 'C';
else if (average>=60)
grade = 'D';
else
grade = 'F';
// PRINTED THE SUMMARY AS SPECIFIED IN THE PROBELEM DESCRIPTION
cout<<"Hello "<<name<<", your test scores were "<<scores[0]<<", "<<scores[1]<<" and "<<scores[2]<<". Your average is "<<average<<" with letter grade: "<<grade<<"."<<endl;
}
int main(){ // MAIN FUNCTION
char name[20];
float test_scores[3]; // TEST SCORES ARRAY OF SIZE 3 TO STORE THE SCORES OF 3 SUBJECTS
bool name_entered =false, scores_entered = false; // BOOLEAN VARIABLES FOR THE PROGRAM TO KNOW IF USER HAS ENTERED THE NAME AND SCORES OR NOT
int choice;
while (1){
cout<<"\n1. "<<"Enter User Name."<<endl; // DISPLAY THE MENU
cout<<"2. "<<"Enter Test Scores."<<endl;
cout<<"3. "<<"Display Average."<<endl;
cout<<"4. "<<"Display Summary."<<endl;
cout<<"5. "<<"Quit."<<endl;
cout<<"\nEnter your choice: ";
cin>>choice;
switch(choice){ // GO TO THE CASE BY SWITCH AS THE CHOICE ENTERED
case 1:
cin>>name; // TAKE THE NAME INPUT FROM USER AND ASSIGN TRUE TO THE BOOLEAN VARIABLE NAME_ENTERED
name_entered = true;
break;
case 2:
for(int i=0;i<3;i++)
cin>>test_scores[i]; // TAKE THE SCORES OF THE THREE SUBJECTS AND ASSIGN TRUE TO THE BOOLEAN VARIABLE SCORES ENTERED
scores_entered = true;
break;
case 3:
if (!scores_entered) // IF SCORES NOT ENTERED DISPLAY ERROR ELSE CALL THE DISPLAY AVERAGE FUNCTION
cout<<"Please use the menu to enter test scores first"<<endl;
else
display_average(test_scores);
break;
case 4:
if(!name_entered)
cout<<"Please use the menu to enter your name first"<<endl; // IF NAME OR SCORES OR BOTH NOT ENTERED DISPLAY THE ERROR ELSE CALL THE DISPLAY SUMMARY FUNCTION
if(!scores_entered)
cout<<"Please use the menu to enter test scores first"<<endl;
if(name_entered && scores_entered)
display_summary(name,test_scores);
break;
case 5:
exit(0);
default:
cout<<"Invalid choice!!! please try again..."<<endl; // IF CHOICE IS INVALID DISPLAY ERROR
}
}
return 0;
}
CODE (C):
#include<stdio.h>
#include<stdlib.h>
void display_average(float scores[]){
float sum = 0.0,average;
int i;
for(i=0;i<3;i++)
sum += scores[i];
average = sum/3;
printf("Your average score is %.2f\n",average);
}
void display_summary(char name[],float scores[]){
float sum = 0.0,average;
int i;
char grade;
for(i=0;i<3;i++)
sum += scores[i];
average = sum/3;
if (average>=90)
grade = 'A';
else if (average>=80)
grade = 'B';
else if (average>=70)
grade = 'C';
else if (average>=60)
grade = 'D';
else
grade = 'F';
printf("Hello %s, your test scores were %.1f, %.1f and %.1f. Your average is %.2f with letter grade: %c\n",name,scores[0],scores[1],scores[2],average,grade);
}
int main(){
char name[20];
float test_scores[3];
int name_entered = 0, scores_entered = 0; // IN C LANGUAGE BOOLEAN IS NOT DEFINED SO TOOK INTEGER VALUE 0 FOR FALSE AND 1 FOR TRUE
int choice,i;
while (1){
printf("\n1. Enter User Name.");
printf("\n2. Enter Test Scores.");
printf("\n3. Display Average.");
printf("\n4. Display Summary.");
printf("\n5. Quit.\n");
printf("\nEnter your choice: ");
scanf("%d",&choice);
switch(choice){
case 1:
scanf("%s",name);
name_entered = 1;
break;
case 2:
for(i=0;i<3;i++)
scanf("%f",&test_scores[i]);
scores_entered = 1;
break;
case 3:
if (!scores_entered)
printf("\nPlease use the menu to enter test scores first\n");
else
display_average(test_scores);
break;
case 4:
if(!name_entered)
printf("\nPlease use the menu to enter your name first");
if(!scores_entered)
printf("\nPlease use the menu to enter test scores first\n");
if(name_entered && scores_entered)
display_summary(name,test_scores);
break;
case 5:
exit(0);
default:
printf("\nInvalid choice!!! please try again...\n");
}
}
return 0;
}
OUTPUT SCREENSHOT( INPUT INCLUDED) :