#include<iostream>
using std::cout;
using std::cin;
using std::endl;
int main()
{
int gradeCounter,grade,average,total;
total=0;
gradeCounter=1;
while (gradeCounter<=10)
{
cout<<"Enter grade subject:";
cin>>grade;
total=grade+total;
gradeCounter=gradeCounter+1;
}
average=total/10;
cout<<"Class average is"<<average<<endl;
return 0;
}
The program is working successfully. How good the program is supposed to be? Do you mean in term of the simplicity or is it about upgrading the program?
If simplicity is the main concern, using the for loop would make the program simpler,i.e:
for(gradeCounter=0;gradeCounter<10;gradeCounter++)
Assuming that the program should work exactly like what you posted, I suggest:
- declare variables when needed
- define named constants instead of putting literal numbers in the code
- simplify mainline logic using functions (a function is useful if you reduce the number of variables needed in the calling function, e.g. main)
- put a space between output text and values
#include<iostream>
using std::cout;
using std::cin;
using std::endl;
using std::istream;
constint NUMBER_OF_GRADES = 10;
int readGrade(istream& stream)
{
int grade;
stream >> grade;
return grade;
}
int main()
{
int total=0;
for (int c = 0; c < NUMBER_OF_GRADES; c++) {
cout << "Enter grade subject: ";
total += readGrade(cin);
}
int average = total / NUMBER_OF_GRADES;
cout << "Class average is " << average << endl;
return 0; // this is optional
}
tnX ropez .....are you a filipino.....?
do you remember recently...
Im the one who posted .....tht you said that using .h is not nescessity your absolutely right....
a shorcut instead of using all of
using std::cout;
using std::cin;
using std::endl;
using std::istream;
these is just in the header put
using namespace std;
Putting 'using namespace std;' in the file would put all 'names' from the std namespace into a more global name space (depending on what namespace you put the call in). It would be better to only put the names that you require in the namespace, as Ropez and takemewithyou have done, to reduce the possibility of name clashing from other sources. It would be even better to not use the ‘using …’ method and qualify your names as they are used (but that is more typing).
#include<iostream>
constint NUMBER_OF_GRADES = 10;
int readGrade(std::istream& stream)
{
int grade;
stream >> grade;
return grade;
}
int main()
{
int total=0;
for (int c = 0; c < NUMBER_OF_GRADES; c++) {
std::cout << "Enter grade subject: ";
total += readGrade(std::cin);
}
int average = total / NUMBER_OF_GRADES;
std::cout << "Class average is " << average << std::endl;
return 0; // this is optional
}