# for loop problem What's Wrong???

This is a discussion on for loop problem What's Wrong??? within the C++ Programming forums, part of the General Programming Boards category; So the first half of my code to find the average works: Code: int main() { const int SIZE = ...

1. ## for loop problem What's Wrong???

So the first half of my code to find the average works:
Code:
```int main()
{
const int SIZE = 5; //max array size
double x[SIZE] = {1,5,6,8,10};
int num_points = 0; //array index

double sum = 0;
double avg;

for(int i=0;i<SIZE;i++) //for loop to find the sum of array
{
sum = sum + x[num_points];
num_points++;
}

avg = sum/SIZE;
cout<<"the average of the values in the array is:"<<endl;
cout<<avg<<endl;```
but the second half to see if each value in the array is >= or < the average does not work...
can anyone tell me why?:
Code:
```//for loop to find if each value is >= or < the array avg.
for(int k=0;k<SIZE;k++)
{
if(x[k]<avg)
cout<<setw(5)<<x[k]<<" is less than the average"<<endl;
else
cout<<setw(5)<<x[k]<<" is greater than or equal to the average"<<endl;
}

return 0;
}```
THANKS

2. I havent seen the "setw()" function yet so I dident know what header to include. I took it out and ran the prog which seemed to work fine for me ??? The rest of the code seems correct too.

3. setw() is in the header file iomanip

4. Can't see anything wrong in there. Do you do anything in between ? And what exactly "does not work" ?
(It's time you post your whole code)

5. Thanks for your help guys...
I for got to add the #include<iomanip> - duh!

6. Stick to the C++ section. This is C++ code, not C.

7. Agreed - moved.