Below is a section of code that calculates the average of the input that is put into an array. Any number that is in the array is less than average, the code couts those numbers. But what if no number entered is less than average? How would this be written into the code? If there are no number less than average, it's suppose to cout "There aren't any!". I'm sure it's something easy and I always end up slapping myself on the forehead when I see it but can't figure this out.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
average = sum/index;
cout << "The total was " << sum << endl;
cout << "The average is " << average << endl;
cout << "Here are all the numbers less than the average: " << endl;
int i = 0;
while (i < index) {
if (array[i] >= average) {
cout << "There aren't any!";
}
if (array[i] < average) {
cout << array[i] << endl;
}
i++;
}
Thanks for the reply. I'm in my first year of school I have a hard time visualizing the code and how it should look. I added something in, it seems to produced the needed verbiage but it repeats "There aren't any!" 5X.
Thanks. I know I didn't give the entire code but I added these lines in this way and now it works.
average = sum/index;
cout << "The total was " << sum << endl;
cout << "The average is " << average << endl;
cout << "Here are all the numbers less than the average: " << endl;
int i = 0;
int count = 0;
while (i < index) {
if (array[i] < average) {
count++;
cout << array[i] << endl;
}
i++;
}
if (count == 0) {
cout << "There aren't any!" << endl;
}