Hello. I am writing a program which collects data from the user and once all the data is entered, calculates the mean, and the highest and lowest numbers entered.
I got the program to calculate the mean and figure out the highest number but I cannot figure out the lowest number. Help, please?
Also, the program stops running when the user enters anything less than 0 however the mean/highest and lowest numbers COUTs still appear. How can I make the program completely break if the user enters anything less than 0?
*/
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the
average of all the test scores.
*/
#include <iostream>
usingnamespace 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);
//The above section must be included in the program, left untouched.
//This section finding the mean of the entered numbers works.
double total=0;
for (int x = 0; x < counter; x++)
{
total += scores[x];
}
cout << "Average is " << (total/(counter)) << endl;
double maxval=0;
for (int x = 0; x < counter; x++)
{
maxval = scores[x];
}
cout << "Highest is " << maxval << endl;
// The minimum value is constantly being returned as 0.
double minval=0;
for (int x=0; x>counter; x--)
{
minval = scores[x];
}
cout << "Lowest is " << minval <<endl;
}
And as for your setting maxval to the score if the score is greater than the current maxval, I have to have the program figure out the max/min values after all the numbers have been entered, not as they are being entered.
The if() statements are the right thing to do. But why have you put them in a do{}while() loop? What's that for? Your original for(;;) loop was correct.
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the
average of all the test scores.
*/
#include <iostream>
usingnamespace std;
int main()
{
double scores[75];
int counter;
double total = 0;
for(counter = 0 ; counter < 75 ; counter++) {
cout << "Please enter a score (enter -1 to stop): ";
cin >> scores[counter];
if (scores[counter] < 0) {
break;
} else {
total += scores[counter];
}
}
//The above section must be included in the program, left untouched.
//This section finding the mean of the entered numbers works.
cout << "Average is " << (total/counter) << endl;
double maxval=0;
for (int x = 0; x < counter; x++)
{
if (scores[x] >= maxval) {
maxval = scores[x];
}
}
cout << "Highest is " << maxval << endl;
// The minimum value is constantly being returned as 0.
double minval = scores[0];
for (int x=0; x < counter; x++)
{
if (scores[x] <= minval) {
minval = scores[x];
}
}
cout << "Lowest is " << minval <<endl;
}
That does work but for my assignment, I'm not allowed to change this code segment:
Any other ideas?
1 2 3 4 5 6 7 8 9 10 11 12
#include <iostream>
usingnamespace 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);
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the
average of all the test scores.
*/
#include <iostream>
usingnamespace 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);
//The above section must be included in the program, left untouched.
//This section finding the mean of the entered numbers works.
double total=0;
for (int x = 0; x < counter; x++)
{
total += scores[x];
}
cout << "Average is " << (total/(counter)) << endl;
double maxval=0;
for (int x = 0; x < counter; x++)
{
maxval = scores[x];
}
cout << "Highest is " << maxval << endl;
// The minimum value is constantly being returned as 0.
double minval=0;
for (int x=counter; x>=0; x--)
{
minval = scores[x];
}
cout << "Lowest is " << minval <<endl;
}
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the
average of all the test scores.
*/
#include <iostream>
usingnamespace 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);
//The above section must be included in the program, left untouched.
//This section finding the mean of the entered numbers works.
double total=0;
for (int x = 0; x < counter; x++)
{
total += scores[x];
}
cout << "Average is " << (total/(counter)) << endl;
double maxval=scores[0];
for (int x = 0; x < counter; x++)
{
if (scores[x] >= maxval) {
maxval = scores[x];
}
}
cout << "Highest is " << maxval << endl;
// The minimum value is constantly being returned as 0.
double minval=scores[0];
for (int x=counter-1; x>=0; x--)
{
if (scores[x] <= minval) {
minval = scores[x];
}
}
cout << "Lowest is " << minval <<endl;
}
Thanks so much! The main program works now. There's just one thing that isn't working. When the only thing entered is a negative number (for example, a -1 to stop the loop), the loop still attempts to figure out the mean, lowest and highest number. When a -1 is entered, I'd like the program to immediately print "Press any key...." and exit.
Any tips for that?
/* After the user enters scores (entering -1 will stop the loop),
calculate and display the highest and lowest test scores and the
average of all the test scores.
*/
#include <iostream>
usingnamespace 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);
//The above section must be included in the program, left untouched.
if (counter > 0) {
//This section finding the mean of the entered numbers works.
double total=0;
for (int x = 0; x < counter; x++)
{
total += scores[x];
}
cout << "Average is " << (total/(counter)) << endl;
double maxval=scores[0];
for (int x = 0; x < counter; x++)
{
if (scores[x] >= maxval) {
maxval = scores[x];
}
}
cout << "Highest is " << maxval << endl;
// The minimum value is constantly being returned as 0.
double minval=scores[0];
for (int x=counter-1; x>=0; x--)
{
if (scores[x] <= minval) {
minval = scores[x];
}
}
cout << "Lowest is " << minval <<endl;
}
}