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)
x[i]=50;
else if (40<x[i]<50)
x[i]=40;
else if (30<x[i]<40)
x[i]=30;
else if (20<x[i]<30)
x[i]=20;
else if (10<x[i]<20)
x[i]=10;
else if (0 <x[i]<10)
x[i]=0;
}
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)
count++;
}
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.