-1
/********************************************************************************************
 * greedy.c
 *
 * CS50 Problem Set 1
 *
 * since i live in UAE ill will use UAE notes because we dont deal
 * with coins like quarters pennies and dimes i will state the notes down
 *currency is called : dirham
 *5 dirhams, 10 dirhams,20 dirham, 50 dirham,100 dirham, 200 dirham, 500 dirham, 1000 dirham
 * this program will figure out the minimum number of notes necessary for change
 *
 * (Also want to show the type and number of each note needed for the minimum number of notes)
 *******************************************************************************************/

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


int
main( void)
{
    float change = 0, inputflag = 1;
    int one = 1, five = 5, ten = 10, twenty = 20, fifty = 50, onehundred = 100, twohundred = 200, fivehundred = 500, onethousand = 1000;
    int onec = 1, fivec = 5, tenc = 10, twentyc = 20, fiftyc = 50, onehundredc = 100, twohundredc = 200, fivehundredc = 500, onethousandc = 1000;


    printf( "This program shows the minimum number of notes you need to return for change.\n");
    printf( "\n");

    while (inputflag == 1)
    {
        printf( "How much is the change?  " );
        change = GetFloat();

        if (change == 0)
        {
            printf( "\nNo change needed!\n" );
            inputflag = 0;
        }
        else if (change > 0)
        {
            printf( "%.2f\n" , change);
            inputflag = 0;
        }
        else
        {
            printf( "%.2f put a positive number please.\n", change);
        }
    }

    if (change != 0) 
        {


            while (change >= one)
            {
                change = change - one;
                onec++;
            }
            while (change >= five)
            {
                change = change - five;
                fivec++;
            }

            while (change >= ten)
            {
                change = change - ten;
                tenc++;
            }

            while (change >= twenty)
            {
                change = change - twenty;
                twentyc++;
            }

            while (change >= fifty)
            {
                change = change - fifty;
                fiftyc++;
            }

            while (change >= onehundred)
            {
                change = change - fifty;
                onehundredc++;
            }

            while (change >= twohundred)
            {
                change = change - twohundred;
                twohundredc++;
            }

            while (change >= fivehundred)
            {
                change = change - fivehundred;
                fivehundredc++;
            }

            while (change >= onethousand)
            {
                change = change - onethousand;
                onethousandc++;
            }

            printf( "You owe the following number of notes as change:\n");
            printf( "one = %d\n" , onec);
            printf( "five    = %d\n" , fivec);
            printf( "ten  = %d\n" , tenc);
            printf( "twenty  = %d\n" , twentyc);
            printf( "fifty = %d\n" , fifty);
            printf( "onehundred   = %d\n" , onehundredc);
            printf( "twohundred  = %d\n" , twohundredc);
            printf( "fivehundred  = %d\n" , fivehundredc);
            printf( "onethousand = %d\n" , onethousandc);

            printf("The total number of notes needed for change: %d\n", onec + fivec + tenc + twentyc + fiftyc + onehundredc + twohundredc + fivehundredc + onethousandc);
            }

}
4
  • 1
    Most probably is because you don't initialize to zero the variables that keep count of the coins or notes..
    – wallek876
    Jul 26 '15 at 20:47
  • You should also start with the biggest coin value (i.e. 1000) and go downwards to 1. Not the other way around.
    – ChrisG
    Jul 26 '15 at 20:58
  • With respect to your location, you should know that your code will never pass the check50 grading program. CHECK50 expects very exact output from your program - exact results, exact formatting, even the exact number of spaces and line feeds! Changing to UAE currency is changing the assignment with the addition of currency that doesn't match U.S. coins ( in other words, anything that doesn't have a unit value of .01, .05, .10 and .25 as compared to the primary unit of currency, whether it be the dollar or something else.).
    – Cliff B
    Jul 26 '15 at 21:01
  • Also, since your currency is all in whole units, the dirham, it misses the point of the exercise - to use integers to deal with the inaccuracies of floating point storage. It would still be a good exercise and good practice to implement a version for your local currency, but these assignments are graded by a computer that won't accept any changes to the assignment.
    – Cliff B
    Jul 26 '15 at 21:01
0

you have to round,only use 1,5,10,25 you have to rewrite the whole code it would be easier,this time read the pset section and see the video you only have to print no.of coins used

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .