Im currently learn c++ and for my assignment im asked to write a program that calculates the average of test scores(0-100). The program stores grades until the user enters -1. Once the user enters -1 then the program prints out the average of the grades prior to the -1. In addition the program also prints out the highest grade that the user input. Please help I dont quite know what im suppose to do!
#include <iostream>
usingnamespace std;
int main() {
int x = 0;
int number;
int total;
while(x != -1)
{
cin >> number;
total = total + number;
x++;
if(number == -1)
{
cout << "The average for all grades is: " <<((total + 1) / x) << endl;
}
}
return 0;
}
"hw4.cpp" 33L, 328C written
[pejo9770@venus hw]$ g++ hw4.cpp
[pejo9770@venus hw]$ ./a.out hw4.cpp
51
45
80
-1
The average for all grades is: 1049156
50
50
100
-1
The average for all grades is: 524602
I prefer to use a few extra variables just for clarity sake.
Giving your variables meaningful names instead of generic names like "x" is helpful
Initializing your variables and a few conditional funtions can work wonders.
#include <iostream>
usingnamespace std;
int main()
{
int number, total =0; // Got rid of x
int highgrade, avg, counter = 0; // Added three variables
while(number > -1)
{
cout << "Please enter grade ";
cin >> number;
if (number > -1)
{
counter++;
}
total = total + number;
avg = total / counter; // averages grades every time a new grade is entered
if (number>highgrade)
{
highgrade=number; // checks for the highest number
}
}
cout << "\nYou entered " << counter << " grades." << endl;
// Used to make sure 'counter' was accurate
cout << "Your grades total " << total << endl;
// Used to ensure grades were totalled correctly
cout << "\nThe highest grade entered is: " << highgrade << endl;
cout << "\nThe average for all grades is: " << avg << endl;
return 0;
}
Thank you guys, So much great insight and information. Specifically Pearlyman, I dont quite understand what effect does initializing the variable have on the programs. Im sorry if this is a newbie question but I really want to understand the concepts taught to me, not just ask for help anytime i encounter a problem. Thank you again!
When you write a code, sometimes it can get extremely large, spread out over many files.
When a function routine goes stupid on ya, you want to find your variables quickly.
If the Monster I call "Orc" is showing up with green eyes, and I think they should be black... It's easier to look for Orc.eyes than try to remember which variable x, y, and z controls...
Orc.hair_color or x
Orc.eyes or y
Orc.disposition or z
By using meaningful names, You can tell right away what that variable is used for.
Initializing a variable gives the variable a starting point. A beginning value. In your computer memory, somewhere in the dark corners of your CPU resides the variable a. It's a scary place until you are a very experience programmer (which I am not). In that memory location where 'a' lives is a value. It could be left over from Windows starting, or a game you were playing earlier. No one knows. It's likely to be different on your computer than it is on mine. Declaring it, reserves the right for you to use it, but it does not clear the value. That's your job. So we initialize our variable to give them a starting value. This is how we control the outcome of our code.
int a; is not the same as
int a; // this declares it
a=0; // this initializes it