Design and implement a program that computes final averages for a set of grades. The program reads the grades from a user. (10)
The format of a grade line is:
N grades1, grades2, …………., grades5
Where N is total number of students and grades is the ith score. All scores must be between 0 and 100.
The program reads the grades from the user, calculate and display the average.
The weighted average is computed as:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main(){
int n;
string nStr;
int value;
int sumValues=0;
string line;
getline(cin,line);
stringstream ss(line);
getline(ss,nStr,',');
n=atoi(nStr.c_str());
for(int i=0;i<n;i++){
getline(ss,nStr,',');
value=atoi(nStr.c_str());
if(value>=0 && value<=100){
sumValues+=value;
}else{
n=-1;
break;
}
}
if(n!=-1){
double average=sumValues/(double)n;
cout<<"The weighted average is: "<<average<<"\n";
}else{
cout<<"Wrong data.\n";
}
system("pause");
return 0;
}
Comments
Leave a comment