+ 1
HELP ME!!! It's the ques. from c++ exercise, what's wrong with it??.....
#include <iostream> using namespace std; int main(){ int ages[5]; int i, x, cost; for (i = 0; i < 5; ++i){ cin >> ages[i]; } int min = ages[0]; for (x = 0; x < 5; x++){ if (ages[x] < min){ min = ages[x]; } } cost = 50-(50*(min/100)); cout << cost; return 0; }
6 Answers
+ 2
Remember, I was talking about the division. If you change 50 to float, the division ( min / 100 ) will still be integer division. It would make the most sense to change 100 to float, i.e. ( min / 100.0 ), since not only will this become floating point arithmetic, but also all later operations on the result.
However, keep in mind your "cost" variable is an integer right now, meaning the decimal part would be truncated at the end, which would lead to an incorrect result. You can either print the result directly, without storing it in a variable inbetween, or you need to change the data type of "cost" as well.
+ 4
If you divide an integer by another integer, the result will be an integer itself, i.e. the decimal part is cut off. Example:
10 / 4 = 2
4 / 10 = 0
If you want the result as a floating point type, then at least one of the operands needs to be a floating point value. Example:
10 / 4.0 = 2.5
4.0 / 10 = 0.4
Since the task expects you to output a floating point number, you need to change the calculation of the total cost a little to accomodate for this behaviour.
+ 1
Can u plzz send full ans.... I'm still getting only 50 as answer.
+ 1
What did you try to change?
+ 1
50 -> int to float
+ 1
Oh... thanks...it really worked...