0

The output is not being rounded off: It doesn't count the cents value if you input a dollar in decimal. if it's $45.67, it only counts change for $45. Also, output comes back 0 if the input is negative, while I want the question to be repeated if the input is negative..

e.g., input = 0.67, output = 0 (expected 6).... input = 7.36, output = 3 (not counting cents).... input = 4.20, output = 4 (expected 18).... Also, input 23 gives output 5 instead of 93.... And it rejects negative input like -2, whereas I want it to loop back to the question.

Here is my code:

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

    void greedyAlgorithm(int n); //Define the function

    int main(void)
    {
        float dollars=get_float("Change owed:"); // Ask for change
        int n=round(dollars*100);
        greedyAlgorithm(dollars); // Run the greedy algorithm
        return 0;
    }

    void greedyAlgorithm(int n)

    {
        int A=0, B=0, C=0, D=0, count=A+B+C+D;

    //total number of quarters ($0.25)
        while(n>=25)
        {
            count++, A++;
            n=n-25;
        }
        ..........................
        //repeat with each value of coins

        printf("%d\n", count);
    }
2
  • Could you rephrase your question? I don't understand what you're asking. Also, your code currently only counts the number of quarters. And you can create a loop until the user gives a positive input.
    – TLH
    Mar 30 '20 at 5:11
  • I have created other similar loops like quarters that also count dimes, nickels, and pennies. I didn't want to include the repetitive code here. My problem is that it doesn't give the desired output to the above-mentioned input. It doesn't count the cents value if you input a dollar in decimal. if it's $45.67, it only counts change for $45. Also, output comes back 0 if I input any negative, while I want the output to repeat the question. Mar 31 '20 at 19:13
0

The program is sending users input value dollars to the function here greedyAlgorithm(dollars); // Run the greedy algorithm. Is that what you meant? Probably not, since the previous line is int n=round(dollars*100);, you probably meant to send n to the function.

Review the lecture notes (or the lecture) to see how to use do ... while to do "something first, then checks a condition, and repeats until the condition is no longer true.". It needs a do/while loop. Did you do mario? If so, use the same strategy. If not, review the lecture or the lecture notes, there is an example in "positive.c".

2
  • I meant that the inputted number is not being rounded off. It doesn't count the cents value if you input a dollar in decimal. if it's $11.10, it only counts change for $11. Also, output comes back 0 if the input negative, while I want the output to repeat the question if the input is negative. Mar 31 '20 at 19:20
  • more info added to answer Mar 31 '20 at 20:47

You must log in to answer this question.

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