Code-A-Day : changeMaker
This is a rewrite of an assignment I had in my first computer science class. Originally written in Java, this is a from-scratch rewrite in C. I had some trouble with the random number generator, but I finally figured out how to get it to behave. Also, I broke the change-making part into it’s own separate function, which was something I didn’t know how to do the first time I wrote this program.
/* Change Maker
** 2013-06-05
**
** Basic Change maker program to the specifications listed for
** assignment at
** http://faculty.kirkwood.edu/pdf/uploaded/262/cs1/sp13/sp13p1-makeChange.pdf
** Original assignment written in Java.
** This is a self-taught rewrite in C
**
** kiyote23@gmail.com
*/
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#define NICKEL 5
#define DIME 10
#define QUARTER 25
int getChange(int price) {
int change, quarters, dimes, nickels, remainder;
change = 100 - price;
remainder = change;
quarters = remainder/QUARTER;
remainder = remainder%QUARTER;
dimes = remainder/DIME;
remainder = remainder %DIME;
nickels = remainder/NICKEL;
printf("Your change is %d cents: %d quarter(s), %d dime(s), %d nickel(s)\n", change, quarters, dimes, nickels);
return 0;
}
int main() {
int price;
int r;
srand(time(NULL));
r = ((rand() % 20)+1)*5;
printf ("Enter the price of the item to be bought, in cents: ");
scanf("%d", &price);
//printf("You entered %d.\n", price);
getChange(price);
printf("Random price: %d\n", r);
getChange(r);
return 0;
}