Commit 791d2511 authored by Alan Marchiori's avatar Alan Marchiori
Browse files

added lec04

parent 7817b51f
#include <stdio.h>
#include <stdlib.h>
struct change{
int quarters;
int dimes;
int nickels;
int pennies;
};
struct change *make_change(float amount)
{
int cents = amount * 100;
struct change *c = malloc(sizeof(*c));
c->quarters = 0;
c->dimes = 0;
c->nickels = 0;
c->pennies = 0;
while (cents > 0){
if (cents > 25){
c->quarters ++;
cents -= 25;
} else if (cents > 10){
c->dimes ++;
cents -= 10;
} else if (cents > 5){
c->nickels ++;
cents -= 5;
} else{
c->pennies = cents;
cents = 0;
}
}
return c;
}
void print_change(struct change* c)
{
printf("%d quarters, %d dimes, %d nickels, %d pennies\n",
c->quarters, c->dimes, c->nickels, c->pennies);
}
int main(void){
struct change *c;
float amount;
printf("amount: $");
scanf("%f", &amount);
c = make_change(amount);
print_change(c);
free(c);
c = NULL; // don't leave dangling pointer
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment