Hi I have to write program that accepts 10 integers from the user. The program has to list the numbers the user entered, list them in reverse order, display the highest/lowest/average numbers, and output the number of integers that are above average.
I have debugged most of the problems but there are a few errors I am having trouble figuring out. I keep getting:
Line 34: error: expected unqualified-id before '=' token
Line 34: error: name lookup of `i' changed for new ISO `for' scoping
Line 25: error: using obsolete binding at `i'
I already went through the program and checked to make sure that there weren't any errors related to {}. If someone could tell me what I am doing wrong, I would really appreciate it.
#include <iostream>
usingnamespace std;
//=============================================================================
// Function prototypes
void printResults();
//=============================================================================
// Main function executes all other functions
int main()
{
int grade[10];
int sum;
// For loop asks user to enter the grades one at a time
for (int i=0; i<10; i++)
{
cout << "Enter a grade (no decimals, whole numbers only): ";
cin >> grade[i];
}
// For loop finds the highest grade in the array
int high = grade[0];
for (int i=1; i<10; i++)
{
if (grade[i] > high)
high=grade[i];
}
cout << "The highest grade is " << high << endl;
// For loop find the lowest grade in the array
int low = grade[0];
for (int=10; i>10; i--)
{
if (grade[i] < low)
low=grade[i];
}
cout << "The lowest grade is " << low << endl;
// For loop finds the average grade in the array
for (int i=0; i<10; i++)
{
sum+=grade[i];
}
int average;
average = sum/10;
cout << "The average grade is " << average << endl;
// For loop finds the number of grades above average
int counter =0;
for (int i=0; i<10; i++)
{
if (grade[i]>average)
counter ++;
}
cout << "There are " << counter << " grades above average." << endl;
}
// Displays output of information
// void printResults();
That line is from the for loop I made to try and locate the lowest number. I basically took the previous for loop (to locate the highest number) and did the opposite. The declared variable for that line is int low
Line 34: Several problems.
- What Moschops was trying to tell you is that you didn't name a variable (i) to initialize.
- Why are you using a descending loop here?
- The first iteration of the loop is going to cause an out of bounds reference. i.e. grade[10] is not valid.
- i is never going to be greater than 10.
Use a conventional for loop here.
for (inti=1; i<10; i++)
Line 14: sum is an uninitialized variable (garbage).