I'm just trying to do a simple program to find the sum and average. It seems right to me, but apparently I've got an error with my identifiers. Here is my program:
int main()
{
int first = 2;
int second = 4;
int third = 6;
int fourth = 8;
int sum, // Sum of all values.
int average, // Average of sum of all values.
// Calculate the sum of all values.
sum = first + second + third + fourth;
// Calculate the average of all values.
average = sum / 4;
// Display the average.
cout << "The average is " << average << endl;
You will still get a wrong answer if you change first to 3, for example. The answer should be 5.25, but you wil still just get 5. You need to use a floating point variable (usually a double) to hold decimals. (You would also have to change 4 to 4.0 when calculating the average, to force the left-hand-side to be evaluated as in floating point.)