C++/percentage
Expert: Eddie - 11/15/2004
QuestionI've written a program for averaging lists of exam grades before but throwing in a percentage is throwing me for a loop. I don't know how to handle this information. Any help will be greatly appreciated.
//Write a program that asks the user to enter a student's midterm
//and final exam scores. The program should display the midterm and
//final exam scores and the student's final grade. Compute the final
//grade by taking the sum of 40% of the midterm exam score and 60% of
//the final exam score.
#include <iostream.h>
#include<iomanip.h>
//using namespace std;
int main()
{
int. midterm,midterm;
final;final;
double 40% mid;
60% final;
cout<< set precision()
<<setiosflags(.40);
<<setiosflags(.60);
//obtain input data
cout<<"\nEnter midterm";
cin>>midterm exam;
cout<<"\nEnter final";
cin>>;final exam;
//Do the calculations
final grade = double (midterm + final);
cout<<"\n;????
return 0;
}
AnswerHello Joanne, thanks for the question.
I don't know if you formatted the code like that to make it more readable, but a lot of your syntax is off. For one thing, a variable name cannot start with a number in C++. Perhaps you were overthinking this problem some, because it's really not that difficult. The following code should help point you in the right direction:
#include<iostream>
using namespace std;
int main()
{
float midterm, final, average;
cout << "Enter a midterm grade:";
cin >> midterm;
cout << "Enter a final grade:";
cin >> final;
average = (.4f * midterm) + (.6f * final);
cout << "The average is: " << average << '\n';
return 0;
}
As you can see, it sums up the average correctly by adding 40 percent of the midterm with 60 percent of the final, as per the commented directions.
I hope this information was helpful.
- Eddie