-
arrays
were winding down the semester and just running through some simple (i think) arrays. the description of the assignment is:
Write a program that prompts the user for test scores (doubles). The user enters -1 to stop the entry. After all of the test scores have been entered, calculate the average, the highest and the lowest test score. Use the code below as a template. Make sure you respond appropriately when no test scores are entered.
the code that we were provided is:
Code:
#include <iostream>
using namespace std;
int main()
{
double scores[75];
int counter = -1;
do
{
counter++;
cout << "Please enter a score (enter -1 to stop): ";
cin >> scores[counter];
} while (scores[counter] >= 0);
// CALCULATE AND DISPLAY THE AVERAGE
// THE HIGHEST AND LOWEST TEST SCORE
// BELOW HERE -- DO NOT MODIFY THE REST OF
// PROGRAM EXCEPT TO PUT YOUR NAME ETC.
// AT THE TOP.
}
what i have so far, which i cant get working is:
Code:
double total = 0, average = 0, min = 101, max = 0;
for (int x = 0; x >= 0; x++)
{
total += scores[x];
average = total/counter;
if (scores[counter] > max)
max=scores[counter];
if (scores[counter] < min)
min=scores[counter];
}
cout << total << average << min << max;
the program compiles fine, and runs ok until i type in -1. once i do that, the error message i get says something along the lines of 'memory could not be "read."'
im not even sure if i am using this for loop correctly or not. i thought that was the right way to calculate the average, but im not too positive on the min/max ones.
thanks for any help fellas.
-
i modified my program and it is almost there:
Code:
double total = 0, average = 0, min = 101, max = 0;
for (int x = 0; x < counter; x++)
{
total += scores[x];
if (scores[counter] > max)
max=scores[counter];
if (scores[counter] < min)
min=scores[counter];
}
average = total/counter;
cout << "Average is " << average << endl;
cout << "Highest is " << max << endl;
cout << "Lowest is " << min << endl;
the average works fine, but im still having trouble with the min/max. the lowest is showing -1 every time (because thats the value needed to be put in to end the program), and the max is always reading 0. i think it has something to do with how i initialized them, since max is set at 0, but i thought with the if statements that would do the trick.
thanks again.
-
here is the correct version:
Code:
double total = 0, average = 0, min = 101, max = 0;
for (int x = 0; x < counter; x++)
{
total += scores[x];
if (scores[x] > max)
max=scores[x];
if (scores[x] < min)
min=scores[x];
}
average = total/counter;
cout << "Average is " << average << endl;
cout << "Highest is " << max << endl;
cout << "Lowest is " << min << endl;
but the only thing now is that if the user enters -1 on their first entry, the program is supposed to stop. instead this tries to calculate the average/min/max.
i was thinking i needed to include an if statement --- if (counter ==0) break;
but, im not sure where i need to put it, or if i need to uses scores[counter] or scores[x], or something else.
-
You could add a condition to check if scores[counter] is -1, after reading the first number.
There's also lots of other things you could improve. Function int main () should always return 0; your algorithms for max and min don't work for all cases. You can either use the max and min functions from <algorithm> or at least initialize max and min with the first value you read.
Also, you can calculate max and min while reading the numbers instead of adding another loop after knowing all the numbers.