Answer to Question #193430 in C++ for isaac

Question #193430

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:


1
Expert's answer
2021-05-14T11:09:03-0400
#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;
}




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog