Code prints every number
Mar 16, 2016 at 9:55pm UTC
I have written this code that reads 10 integers from the user, stores them in an array, then calculates and prints the average. It is also supposed to print the numbers in the array that are greater than or equal to the average. Instead, my code prints out every number in the array. How can I fix this?
Also, if anyone has any tips on how to simplify this code, that would be much appreciated, as well.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
#include <iostream>
using namespace std;
int main()
{
const int ENTER_NUM = 10;
int integer[ENTER_NUM];
cout << "Enter " <<ENTER_NUM<<" numbers: " <<endl;
cin >> integer[0];
cin >> integer[1];
cin >> integer[2];
cin >> integer[3];
cin >> integer[4];
cin >> integer[5];
cin >> integer[6];
cin >> integer[7];
cin >> integer[8];
cin >> integer[9];
int sum;
sum = integer[0]+integer[1]+integer[2]+integer[3]+integer[4]+integer[5]+integer[6]+integer[$
int average;
average = sum/ENTER_NUM;
cout<<"Average is: " <<average<<endl;
for (int i=0; i<10; i++)
{
if (integer[i]>=average);
cout<<"Numbers in array greater than or equal to the average: " <<integer[i]<<endl;
}
return 0;
}
Mar 16, 2016 at 10:23pm UTC
What is at the end of line 32?
Edit: and online compiler says this:
32:41: warning: suggest braces around empty body in an 'if' statement [-Wempty-body]
It is good to make the compiler be as verbose as possible and then pay attention to its messages.
What do you think is "empty body"?
Last edited on Mar 16, 2016 at 10:25pm UTC
Mar 16, 2016 at 10:25pm UTC
If you meant 23,
+integer[7]+integer[8]+integer[9];
Mar 16, 2016 at 10:29pm UTC
I did not receive any compiling errors when running this code. It just did not execute what I was hoping it would.
Mar 17, 2016 at 1:46am UTC
Keskiverto didn't mean line 23, he mean't line 32. He even gave you a great hint!
Topic archived. No new replies allowed.