-3

Well, suppose that a cashier owes a customer some change and in that cashier’s drawer are quarters (25¢), dimes (10¢), nickels (5¢), and pennies (1¢). The problem to be solved is to decide which coins and how many of each to hand to the customer

if some customer is owed 41¢, the biggest first bite that can be taken is 25¢ 41 - 25 = 16 another 25¢ bite would be too big cashier would move on to a bite of size 10¢, leaving him or her with a 6¢ problem At that point the cashier calls for one 5¢ bite followed by one 1¢ bite

#include <cs50.h>
#include <stdio.h>

int main(void)
{   
    float f = get_float("Enter Cash: ");

    int q;
    int d;
    int n;
    int p;

    float quarter = 0.25;
    float dimes = 0.10;
    float nickels = 0.05;
    float pennies = 0.01;

    while ( f != 0)
    {

        if (f >= quarter){

            f = f - quarter;
            q = q + 1;
        } else if (f >= dimes && f < quarter) {

            f = f - dimes;
            d = d + 1;
        } else if (f >= nickels && f < dimes) {

            f = f - nickels;
            n = n + 1;        
        } else {
            f = f - pennies;
            p = p + 1;
        }
        printf ("Quarter: %d \n Dimes %d \n Nickels %d \n Pennies %d \n", q, 
        d, n, p);
    }
}

$ ./cash
Enter Cash: 6
Quarter: 32768 
 Dimes -1230737968 
 Nickels 0 
 Pennies 4205168 
Quarter: 32769 
 Dimes -1230737968 
 Nickels 0 
 Pennies 4205168 
Quarter: 32770 
 Dimes -1230737968 
 Nickels 0 
 Pennies 4205168 
Quarter: 32771 
 Dimes -1230737968 
 Nickels 0 
 Pennies 4205168 
Quarter: 32772
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278

1 Answers1

2

you need to initialize your variables

int q = 0;
int d = 0;
int n = 0;
int p = 0;

otherwise they have unpredictable values

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Thank you guys, but even after initializing them still dosn't work int q = 0; int d = 0; int n = 0; int p = 0; i'm reaciving command not found when i enter any number – kinan hamwi May 20 '19 at 13:20