I study CS50 course at prometheus.org.ua. In task "Greedy", during the test of code, I get an error https://sandbox.cs50.net/checks/2eee4ed453ee4c948f41256da48ee0a9 Here is my code:

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

int main(void)
float input;
    printf("How much change is owed?");
    input = GetFloat();
while (input<=0);
int m = (int) roundf(input*100); 
int coin25 = 25;
int coin10 = 10;
int coin5 = 5;
int coin1 = 1;
int sum_coin25, sum_coin10, sum_coin5, sum_coin1;
if (m>=coin25)
    sum_coin25 = m/coin25;
    if (sum_coin25*coin25>m)
            sum_coin25 = sum_coin25 - 1;
    m = m - sum_coin25*coin25;
if (m>=coin10)
    sum_coin10 = m/coin10;
    if (sum_coin10*coin10>m)
            sum_coin10 = sum_coin10 - 1;
    m = m - sum_coin10*coin10;
if (m>=coin5)
    sum_coin5 = m/coin5;
    if (sum_coin5*coin5>m)
            sum_coin5 = sum_coin5 - 1;
    m = m - sum_coin5*coin5;
if (m>=coin1)
    sum_coin1 = m/coin1;
    printf("%d\n", sum_coin25+sum_coin10+sum_coin5+sum_coin1 );

At the same time, used the site http://ideone.com/ without the functions cs50.h program runs without errors. What am I doing wrong?

P.S. Sorry for my english. I just started study it.

2 Answers 2


Let's debug your program by hand.

  • We are given an input of 1.
  • m is assigned the value 100.
  • since m >= 25, sum_coin25 = 100 / 25 so sum_coin25 == 4

By the way, all of those ifs after the divisions are unnecessary since the integer division will always yield a number less or equal than the normal division. So no need to check for if (sum_coin25*coin25 > m). It's always false.

  • now m = m - sum_coin25 * coin25 so m == 0 and all the other if statements are false.
  • at the last line where you print the result, you have
   printf("%d\n", sum_coin25 + sum_coin10 + sum_coin5 + sum_coin1 );

but what are the values for sum_coin10, sum_coin5 and sum_coin1? You haven't initialized them, that means they don't have a value, and they contain garbage values. That's why you get the wrong output.

To fix your problem, change this:

int sum_coin25, sum_coin10, sum_coin5, sum_coin1;

to this:

int sum_coin25 = 0, sum_coin10 = 0, sum_coin5 = 0, sum_coin1 = 0;
  • Very Great Thanks))) I thought that enough to declare a variable without assigning a value to it. And I thought, then what value will be recorded in the calculations to verify the conditions. But, how the test passed by condition "input of 0.41 yields output of 4"? And, how my code worked at the ideone.com ?
    – Teo
    Commented Aug 11, 2016 at 14:14
  • The behavior and contents of an uninitialized variable are undefined, that is they are unpredictable. It those two cases it just happened that the garbage value assigned to the variables was probably 0. But you can't predict it will happen every time.
    – ChrisG
    Commented Aug 12, 2016 at 5:37

don't use if use when(condition) { //do }

this will be like a loop,u are just doing and think u didided the no is it right


You must log in to answer this question.

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