byrddw
March 14th, 2007, 02:38 PM
I am taking a C programming course. I cannot seem to understand what I am doing wrong on a homework problem. I am to write a program that will show the number of coins (quarters, dimes, nickels, and pennies) in a certain amount. I am also to use a pointer. The assignment seemed pretty straint foward, but I am running into an issue. Every time my program runs through, it is one penny short. I cannot get it to come with the correct number of pennies. I am one short every time. I am using Bloodsheds DevC compiler. I mention this because a freind of mine in the course is using the same compiler, and he is having the same problem. Our programs are completely different (but accomplishing the same functions), yet he is one penny short on every run also. I am attatching a copy of my program. Any help would be greatly appreciated. I've got the program close enough to get an 'A', but I am really curiouse why this is happening.
/*
Name: Written Assignment 3.B.1
Copyright: N/A
Author: Donald W. Byrd
Date: 12/03/07 15:01
Description: Total Number of Coins
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void change (float *); /* Prototype with pointer */
float value;
value = 1.88; /* Sets first run value */
change (&value); /* Calls change function */
value = 0.32; /* Sets second run value */
change (&value); /* Calls change function for second run */
printf ("\nPlease enter a value: "); /* Prompts user to enter a */
scanf ("%f", &value); /* number to run through. */
change (&value); /* Calls change function for user run */
system("PAUSE");
return 0;
}
void change (float *value)
{
float quarterValue, dimeValue, nickelValue, pennyValue, remainder;
int quarterNum, dimeNum, nickelNum, pennyNum;
printf ("TOTAL VALUE ENTERED: %5.2f\n\n", *value);
quarterNum = (*value / 0.25); /* pointer calling value from main () */
quarterValue = (quarterNum * .25);
remainder = (*value - quarterValue);
/* Determines number of quarters */
dimeNum = (remainder / 0.10);
dimeValue = (dimeNum * 0.10);
remainder = (remainder - dimeValue);
/* Determines number of quarters */
nickelNum = (remainder / 0.05);
nickelValue = (nickelNum * 0.05);
remainder = (remainder - nickelValue);
/* Determines number of quarters */
pennyNum = (remainder / 0.01);
/* Determines number of quarters */
printf ("The number of quarters is : %2.2i \n", quarterNum);
printf ("The number of dimes is : %5.2i \n", dimeNum);
printf ("The number of nickels is : %3.2i \n", nickelNum);
printf ("The number of pennies is : %3.2i \n\n\n", pennyNum);
}
/*
Name: Written Assignment 3.B.1
Copyright: N/A
Author: Donald W. Byrd
Date: 12/03/07 15:01
Description: Total Number of Coins
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
void change (float *); /* Prototype with pointer */
float value;
value = 1.88; /* Sets first run value */
change (&value); /* Calls change function */
value = 0.32; /* Sets second run value */
change (&value); /* Calls change function for second run */
printf ("\nPlease enter a value: "); /* Prompts user to enter a */
scanf ("%f", &value); /* number to run through. */
change (&value); /* Calls change function for user run */
system("PAUSE");
return 0;
}
void change (float *value)
{
float quarterValue, dimeValue, nickelValue, pennyValue, remainder;
int quarterNum, dimeNum, nickelNum, pennyNum;
printf ("TOTAL VALUE ENTERED: %5.2f\n\n", *value);
quarterNum = (*value / 0.25); /* pointer calling value from main () */
quarterValue = (quarterNum * .25);
remainder = (*value - quarterValue);
/* Determines number of quarters */
dimeNum = (remainder / 0.10);
dimeValue = (dimeNum * 0.10);
remainder = (remainder - dimeValue);
/* Determines number of quarters */
nickelNum = (remainder / 0.05);
nickelValue = (nickelNum * 0.05);
remainder = (remainder - nickelValue);
/* Determines number of quarters */
pennyNum = (remainder / 0.01);
/* Determines number of quarters */
printf ("The number of quarters is : %2.2i \n", quarterNum);
printf ("The number of dimes is : %5.2i \n", dimeNum);
printf ("The number of nickels is : %3.2i \n", nickelNum);
printf ("The number of pennies is : %3.2i \n\n\n", pennyNum);
}