Originally Posted by
Salem
No idea. I get 2.
If you're getting 1, post your test case.
Code:
$ cat foo.c
#include<stdio.h>
int main(void)
{
int y = 420;
int b = (y % 25) / 10;
printf("b=%d\n",b);
return 0;
}
$ gcc foo.c
$ ./a.out
b=2
my output wasn't exactly what I wanted so I have narrowed the problem down to 'b' so changed the code so that only 'b' would be printed rather than the actual answer to the problem. I just need to know why I'm getting 1 in my programme for 'b' rather than 2. Thank you for your help.
Code:
#include <cs50.h>
#include <stdio.h>
float get_positive_float(string prompt);
int main(void)
{
float change_required = get_positive_float("How much change? ");
int y = change_required * 100;
int a, b, c, d, e;
a = y / 25;
b = (y % 25) / 10;
c = (y - (a * 25) - (b * 10)) / 5;
d = y - (a * 25) - (b * 10) - (c * 5);
e = a + b + c + d;
if (y > 0)
{
printf("%d\n", b);
}
}
float get_positive_float(string prompt)
{
float change_required;
do
{
change_required = get_float("%s", prompt);
}
while (change_required <= 0);
return change_required;
}