First declare an array that is large enough to hold at most 25 floating point values.
You are to ask the user to enter a value in the range of 5 to 25. The program should continue to ask the user to enter a value until he successfully enters a value in this range. You're free to use the function we had in the notes to actually get the proper value.
You are then to get the indicated number of values from the user and store them in the previously-defined array.
After you have gotten all the numbers from the user, you are to find and display the sum and the average of all the values the user entered.
You are then to display the average of all the values the user entered that are greater than 100.0.
Finally, you are to display all the numbers the user entered, but only display three per line.
This is what I have to show.
How many values are you going to enter (5-25)? 30
Too many values; try again.
How many values are you going to enter (5-25)? 2
Too few values; try again.
How many values are you going to enter (5-25)? 8
Please enter value 1: 12.2
Please enter value 2: 7.6
Please enter value 3: -200.3
Please enter value 4: 140.7
Please enter value 5: 890.23
Please enter value 6: 67.78
Please enter value 7: 99.9
Please enter value 8: 100.1
The num of the values is 1118.21
The average is 139.776
The average of the values > 100 is 377.01
12.2 7.6 -200.3
140.7 890.23 67.78
99.9 100.1
This is what I did
#include <iostream>
using namespace std;
int main () {
float A[25];
double num, sum1, sum2, n, p;
int a, i;
do{
cout << "How many values will you enter (5-25)? ";
cin >> num ;
if (num < 5)
cout << "Too few values; try again."<< endl;
else if ( num > 25)
cout << "Too many values; try again." << endl;
}
while (num < 5 || num > 25);
cout << endl;
for (i=1; i<=num; i++)
cout << endl << "Please enter values" << i << " : ";
for (a=0; a<=num; a++)
cin >> A[a];
sum1 = 0.0;
sum2 = 0.0;
for (a=0; a<=num; a++)
{
sum1 += A[a];
}
cout << "The num of the values is " << sum1 << endl;
n = 0.0;
p = 0.0;
cout << "The average is "<< sum1/num << endl;
if (A[i]>100){
sum2+=A[i];
cout << "The average of the values > 100 is: " << sum2/num << endl << endl;
}
else
{
cout << "There were no values entered > 100" << endl;
}
for(int i=0; i<a; i++)
{
if(i%3==0)
cout<<endl;
cout << A[i] << ' ';
}
return 0;
}
I can't seem to fix the average of the values greater than 100. And the values entered one in each line with the statement " Enter value 1: value 1", "Enter value 2: value2]", ..... and so on.
and at the end there is a weired value I get
How can I fix this problem?
This is what I get
Output produced (User input is displayed in bold and underlined)
How many values will you enter (5-25)? 30
Too many values; try again.
How many values will you enter (5-25)? 2
Too few values; try again.
How many values will you enter (5-25)? 8
Please enter values1 :
Please enter values2 :
Please enter values3 :
Please enter values4 :
Please enter values5 :
Please enter values6 :
Please enter values7 :
Please enter values8 : 12.2
7.6
-200.3
140.7
890.23
67.78
99.9
100.1
The num of the values is 1118.21
The average is 139.776
There were no values entered > 100
I can't seem to fix the average of the values greater than 100.
I don't understand what your problem is. You have correctly computed the average of the numbers you gave it. What does 100 have to do with it?
Oh, I see. I'll get back to that.
And the values entered one in each line...
You have two loops. One loop prints "Please enter valueN :" N times. The next loop gets input N times.
Shouldn't you have just one loop?
and at the end there is a [weird] value I get
Arrays of N elements are indexed at 0, 1, ..., N-2, N-1.
The problem is that you are checking to see if a single number is greater than 100, but that number is not in your list, since it is indexing element N, which is one-past the end of your list of numbers.
You need a loop. And, to count the average of elements greater than 100, you also need to count the number of them.