Revision: 65129
at October 27, 2013 13:04 by bhart

Updated Code
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
// ichangeD is equal to the change in dollars
float ichangeD;

//ichangeC is equal to the change in cents
int ichangeC;

do
{
// ask for user the change and covert it to cents
printf("What is the amount of spare change owed (in dollars)?\n");
ichangeD = GetFloat();
ichangeC = round(ichangeD * 100);

}
while(ichangeD < 0);

printf("So %.2f dollars or %.0d cents are needed!\n", ichangeD, ichangeC);

// declare variables of different possible monetary values
int DaCount = 0;
int ldollars = 0;
int gdollars = 0;
int hdollars = 0;
int jdollars = 0;
int quarters = 0;
int dimes = 0;
int nickles = 0;
int pennies = 0;

while(ichangeC % 2000 != ichangeC)
{
jdollars++;
ichangeC = ichangeC - 2000;
}

printf("%d twenty dollar bills needed\n", jdollars);

// since remainder of something less that a certain number is that number that you
// trying to divide, use a while loop to do this until something happens
// then move on to the next monetary value while keeping track of count of money
while(ichangeC % 1000 != ichangeC)
{
hdollars++;
ichangeC = ichangeC - 1000;
}

printf("%d ten dollar bills needed\n", hdollars);

while(ichangeC % 500 != ichangeC)
{
ldollars++;
ichangeC = ichangeC - 500;
}

printf("%d five dollar bills needed\n", ldollars);

while(ichangeC % 100 != ichangeC)
{
gdollars++;
ichangeC = ichangeC - 100;
}

printf("%d one dollar bills needed\n", gdollars);

printf("%d bills in total needed\n", DaDCount);

while(ichangeC % 25 != ichangeC)
{
quarters ++;
DaCount++;
ichangeC = ichangeC - 25;
}

printf("%d quarters needed\n", quarters);

while(ichangeC % 10 != ichangeC)
{
dimes++;
DaCount++;
ichangeC = ichangeC - 10;
}

printf("%d dimes needed\n", dimes);

while(ichangeC % 5 != ichangeC)
{
nickles++;
DaCount++;
ichangeC = ichangeC - 5;
}

printf("%d nickles needed\n", nickles);

while(ichangeC % 1 != ichangeC)
{
pennies++;
DaCount++;
ichangeC = ichangeC - 1;
}

printf("%d pennies needed\n", pennies);

printf("%d coins in total are needed!\n", DaCount);

}

Revision: 65128
at October 27, 2013 12:58 by bhart

Initial Code
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
// ichangeD is equal to the change in dollars
float ichangeD;

//ichangeC is equal to the change in cents
int ichangeC;

do
{
printf("What is the amount of spare change owed (in dollars)?\n");
ichangeD = GetFloat();
ichangeC = round(ichangeD * 100);

}
while(ichangeD < 0);

printf("So %.2f dollars or %.0d cents are needed!\n", ichangeD, ichangeC);

int DaCount = 0;
int ldollars = 0;
int gdollars = 0;
int hdollars = 0;
int jdollars = 0;
int quarters = 0;
int dimes = 0;
int nickles = 0;
int pennies = 0;

while(ichangeC % 2000 != ichangeC)
{
jdollars++;
ichangeC = ichangeC - 2000;
}

printf("%d twenty dollar bills needed\n", jdollars);

while(ichangeC % 1000 != ichangeC)
{
hdollars++;
ichangeC = ichangeC - 1000;
}

printf("%d ten dollar bills needed\n", hdollars);

while(ichangeC % 500 != ichangeC)
{
ldollars++;
ichangeC = ichangeC - 500;
}

printf("%d five dollar bills needed\n", ldollars);

while(ichangeC % 100 != ichangeC)
{
gdollars++;
ichangeC = ichangeC - 100;
}

printf("%d one dollar bills needed\n", gdollars);

printf("%d bills in total needed\n", DaDCount);

while(ichangeC % 25 != ichangeC)
{
quarters ++;
DaCount++;
ichangeC = ichangeC - 25;
}

printf("%d quarters needed\n", quarters);

while(ichangeC % 10 != ichangeC)
{
dimes++;
DaCount++;
ichangeC = ichangeC - 10;
}

printf("%d dimes needed\n", dimes);

while(ichangeC % 5 != ichangeC)
{
nickles++;
DaCount++;
ichangeC = ichangeC - 5;
}

printf("%d nickles needed\n", nickles);

while(ichangeC % 1 != ichangeC)
{
pennies++;
DaCount++;
ichangeC = ichangeC - 1;
}

printf("%d pennies needed\n", pennies);

printf("%d coins in total are needed!\n", DaCount);

}

Initial URL

Initial Description
see the change

Initial Title
Advanced Greedy Algorithm for Change Machine

Initial Tags

Initial Language
C