1. : Implement a program in C++ that accepts 5 marks and prints out their corresponding grades. The program should calculate the average mark, find the greatest and also the least mark. The program uses a user defined function
grade ( ). you can use any previous concepts like Loop, Arrays, If else and Switch etc.
#include <iostream>
#include <string>
using namespace std;
string grade(int mark);
int main (){
int marks[5];
//accepts 5 marks
for(int i=0;i<5;i++){
marks[i]=-1;
while(marks[i]<0 || marks[i]>100){
cout<<"Enter mark "<<(i+1)<<" [0-100]: ";
cin>>marks[i];
}
}
int greatestMark=marks[0];
int leastMark=marks[0];
float averageMark=0;
float sumMarks=0;
cout<<"Mark\tGrade\n";
for(int i=0;i<5;i++){
//prints corresponding grades
cout<<marks[i]<<"\t"<<grade(marks[i])<<"\n";
sumMarks+=marks[i];
//find the greatest and also the least mark.
if(marks[i]>greatestMark){
greatestMark=marks[i];
}
if(marks[i]<greatestMark){
leastMark=marks[i];
}
}
//calculate the average mark
averageMark=sumMarks/5.0;
cout<<"\nThe average mark: "<<averageMark<<"\n";
cout<<"The least mark: "<<leastMark<<"\n";
cout<<"The greatest mark: "<<greatestMark<<"\n";
system("pause");
return 0;
}
string grade(int mark){
if(mark>=90){
return "A";
}
if(mark>=80){
return "B";
}
if(mark>=70){
return "C";
}
if(mark>=60){
return "D";
}
return "F";
}
Comments
Leave a comment