My program is as follows
Code:
#include <stdio.h>
int countchange(int q, int d, int n, int p) ;
/* Don't change the main() function! */
int main () {
int cents ;
int quarters, dimes, nickels, pennies ;
printf("This program will compute the worth of your change.\n\n") ;
printf("Enter number of quarters you have: ") ;
scanf("%d", &quarters) ;
printf("Enter number of dimes you have: ") ;
scanf("%d", &dimes) ;
printf("Enter number of nickels you have: ") ;
scanf("%d", &nickels) ;
printf("Enter number of pennies you have: ") ;
scanf("%d", &pennies) ;
cents = countchange(quarters, dimes, nickels, pennies) ;
printf("Your %d quarters, %d dimes, %d nickels and %d pennies",
quarters, dimes, nickels, pennies) ;
printf(" are wort %d cents.\n", cents) ;
return 0 ;
}
/* end of main() function */
/* Function countchange
Computes the worth of the given number of quarters,
dimes, nickels and pennies.
*/
/* The function */
int countchange(int q, int d, int n, int p) {
int qu;
int di;
int ni;
int pe;
int cents;
qu = q * .25 ;
di = d * .1 ;
ni = n * .05 ;
pe = p * .01 ;
cents = qu + di + ni + pe ;
return ;
}
When I execute my program I know why I am getting the wrong results because the integer value counts everything that is less than 1 as 0 (basically it rounds) therefore skewing my results. I know double/float will accept decimals but when I try and make qu, di, ni, pe, and cents double/float I still get wrong results.
Any help would be great!