What is wrong with this? Array issue.

The assignment is to truncate the decimal for each number, which I did by assigning an integer value to the array. The next part of the assignment is to reduce each integer to the next lowest multiple of ten, such as reducing 55 to 50, 44 to 40, and so on. I don't understand why my code doesn't work because logically it makes sense. When I do it like I have it, it sets every number in the array to 50. Any suggestions?

for (i=0; i <= n-1; i++)
x[i] = (int)x[i];

if (50<x[i]<60)
else if (40<x[i]<50)
else if (30<x[i]<40)
else if (20<x[i]<30)
else if (10<x[i]<20)
else if (0 <x[i]<10)
Last edited on
first of all you can't chain those operators like that.

You must put if( 50 < x && x < 60 ) that format for them all.

Also why not use the modulus operator?

for( int i = 0; i < SIZE; ++i )
    x[i] -= x[i] % 10;
I just tried to use the modulus operator like you have, but it says "invalid operands of type 'double' and 'int' to binary 'operator%'". Any ideas?
Last edited on
I didn't realize x was a double I guess I should have figured with your 3rd line. Just cast it the c-style like you did or put
x[i] = static_cast<int>( x[i] ); Though it would make more sense if x[i] was an integer anyways if you are truncating it. So I would just go and change its type from double to integer.
That fixed it! I want to ask one more question if you don't mind. The next part is to calculate the count of all array values that are >= 40.0. Then, use that count to calculate the percentage of number >= 40.0 in the array. I've done that, and it works fantastically, but my program doesn't automatically end when this is finished. Is this normal? Is there a way to make it end?

int count = 0;
for (i=0; i <= n-1; i++)
if (x[i]>=40.0)

percentage = (count/(double)i) * 100;

cout << "Percentage of values >= 40.0 = " << fixed << setprecision(2) << setw(5) << right << percentage << "%" << endl;
cin >> percentage;

return 0;
} // end main function
I should have mentioned this earlier but instead of <= n -1 you should put < n.

The reason yours is not ending right away is because you have this line
cin >> percentage; A way to fix it would be to remove that line that is not needed or to add a return statement before it but I would just erase the line.
Wow, I can't believe I didn't notice that. You're a life saver, man. Thanks so much for all your help.
Topic archived. No new replies allowed.