NO

Author Topic: Need help for Greedy !  (Read 4290 times)

Marck

  • Guest
Need help for Greedy !
« on: December 03, 2012, 08:38:35 PM »
Hello !

Beginner, I need help for my code. It seems it's impossible to compil, but  I Don't understand why ???

Thanks !

Code: [Select]
#include <stdio.h>
#include<string.h>
 
int main(void)
{
int quart, /* coins of 0.25*/
dime,/* coins of 0.10 */
nickel,/* coins of 0.05 */
pennie,/* coins 0.01 */
nb25 = 0,
nb10 = 0,
nb05 = 0,
nb01 = 0,
 
money;/* recording the amount input by the user */
 
printf("Please enter the amount of money: ");
scanf("%d", &money);
 
quart = money/0.25;
money = money%25;
dime = money/0.10;
money = money%10;
nickel = money/0.05;
money = money%5;
pennie = money/0.01;
money = money%1;
 
while (money > 0)
{

/* Number of coins : 0.25 */

nb25 = (money/0.25); /* Calculating the number of pieces of € 0.25 */

if (money >= nb25)


/* Number of coins : 0.10 */

nb10 = (money/0.10);

if (money >= nb10)


/* Number of coins : 0.05 */

nb05 = (money/0.05);

if (money >= nb05)


/* Number of coins : 0.01 */

nb01 = (money/0.01);

}

printf("\n",nb25, nb10, nb05, nb01);

 
return 0;
}

CommonTater

  • Guest
Re: Need help for Greedy !
« Reply #1 on: December 03, 2012, 11:25:58 PM »
First... learn how to set up your source so you can see the way it works... indentation and both vertical and horizontal whitespace go a long way to making sense of a program... 

This is also your code...
Code: [Select]

 
#include <stdio.h>
#include<string.h>
 
int main(void)
  {
    int quart,                            /* coins of 0.25*/
        dime,                            /* coins of 0.10 */
        nickel,                           /* coins of 0.05 */
        pennie,                          /* coins 0.01 */
        nb25 = 0,
        nb10 = 0,
        nb05 = 0,
        nb01 = 0,
        money;                       /* recording the amount input by the user */
 
    printf("Please enter the amount of money: ");
    scanf("%d", &money);
 
    quart = money / 0.25;
    money = money % 25;
    dime = money / 0.10;
    money = money % 10;
    nickel = money / 0.05;
    money = money % 5;
    pennie = money / 0.01;
    money = money % 1;
 
    while (money > 0)
      {
        /* Number of coins : 0.25 */
       nb25 = (money/0.25);
 
       /* Number of coins : 0.10 */
       if (money >= nb25)
         nb10 = (money/0.10);

       /* Number of coins : 0.05 */
       if (money >= nb10)
         nb05 = (money/0.05);

       /* Number of coins : 0.01 */
       if (money >= nb05)
         nb01 = (money / 0.01);

      }

    printf("\n",nb25, nb10, nb05, nb01);
 
    return 0;
  }

Now that you can actually see your code... it's probably that last printf() statement that's messing you up.
 
In POIDE put your text cursor on printf then press F1 on your keyboard... what you will get is a full explaination of how printf works... (This also works for any other colour highlighted function)
 
If you fix that it should compile... It's going to give you wildly wrong answers... but it should compile.
 
 

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Need help for Greedy !
« Reply #2 on: December 04, 2012, 03:52:51 AM »
Well, in addition to what CommonTater said about formatting your code (which helps you to understand the flow of the code for yourself!, not only for others), you might need to be a bit more specific as to what your problem (error messages) with the code are...

I just copy & pasted it into a test project and it compiles just fine, so I don't see what your problem with "impossible to compile" would be...

Also, please be aware that as a general rule, we don't do homework assignments for you. If you ask specific questions, you will get specific answers...

Ralf

Marck

  • Guest
Re: Need help for Greedy !
« Reply #3 on: December 04, 2012, 09:23:01 PM »
Thank you for your help !
I follow your counsels and... It compil !  :D

But...It doesn't work  :-[

My new code :

Code: [Select]
#include <stdio.h>
#include<string.h>
 
int main(void)
  {
    int quart,                            /* coins of 0.25*/
        dime,                            /* coins of 0.10 */
        nickel,                           /* coins of 0.05 */
        pennie,                          /* coins 0.01 */
        nb25 = 0,
        nb10 = 0,
        nb05 = 0,
        nb01 = 0,
        money;                       /* recording the amount input by the user */
 
    printf("Please enter the amount of money: ");
    scanf("%d", &money);
 
    quart = money / 0.25;
    money = money % 25;
    dime = money / 0.10;
    money = money % 10;
    nickel = money / 0.05;
    money = money % 5;
    pennie = money / 0.01;
    money = money % 1;
 
    while (money > 0)
      {
        /* Number of coins : 0.25 */
       nb25 = (money/0.25);
 
       /* Number of coins : 0.10 */
       if (money >= nb25)
         nb10 = (money/0.10);

       /* Number of coins : 0.05 */
       if (money >= nb10)
         nb05 = (money/0.05);

       /* Number of coins : 0.01 */
       if (money >= nb05)
         nb01 = (money / 0.01);

      }

    printf("%d",nb25 + nb10 + nb05 + nb01);
 
    return 0;
  }

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Need help for Greedy !
« Reply #4 on: December 04, 2012, 10:10:11 PM »
But...It doesn't work  :-[
"doesn't work" is not a proper problem description. And as mentioned before, we don't do homework assignments for you on the forum...

Tell us more specific as to what you expect and what you see happening and you will get an answer to your specific problem...

As a hint, write down on a piece of paper the math for each step of the program and then follow the program step by step in the debugger and compare what you did on paper with what the program does. There are several logical errors in your program, but with a little bit of thinking about what you are doing they are easy fixed.

Another hint, you are "loosing track of your money"...  ;)

Ralf

CommonTater

  • Guest
Re: Need help for Greedy !
« Reply #5 on: December 05, 2012, 09:05:13 AM »
Thank you for your help !
I follow your counsels and... It compil !  :D

But...It doesn't work  :-[

My new friend this is hard lesson #1 in the programming world ... "Compiles" does not mean "Works". 

It is rare indeed that anyone writes a program bigger than "Hello world" that compiles first try.  There's always a missing semicolon or mismatched bracket someplace... it's just the way it is. It is even rarer (to the point of near impossibility) that a program is going to work perfectly right out of the gate... Even the most cautious and experienced programmer is going to make minor errors in logic that will have to be fixed after the first pass.

Programming was once described as: "A bunch of strange people training a complete idiot to do brilliant things."

Now... take note of the reformatted code I put up for you ... *I did not fix the problem* ... that is your job.  So there is lesson #2... "Scoop and Poop" programming causes more problems than it solves.  Copying other people's code, especially while learning, isn't going to fix anything (and you won't learn anything from it)... as I pointed out in my first reply the most likely problem is that last printf statement, and you should follow my instructions about fixing it.

Marck

  • Guest
Re: Need help for Greedy !
« Reply #6 on: December 05, 2012, 01:37:23 PM »
Lol, tkank you for this lessons ;-) It reassures me that everyone can not do it the first time ! (it's not juste me...)

I followed your advice to printf, which allowed me to compile (the problem for compilation).

Now the program compiled, but I can not make it display the sum.

I will follow your advice and those of Ralf (problem of reasoning etc.) and see if I can.

@ Ralf: I do not want anyone to make the excercie for me. I complete my marketing school, by computer. The hardest for me is to learn the reasoning (close to mathematics and very different from marketing). I have no pressure to succeed
Thank you for your help !!
« Last Edit: December 05, 2012, 01:40:11 PM by Marck »

Offline Bitbeisser

  • Global Moderator
  • Member
  • *****
  • Posts: 772
Re: Need help for Greedy !
« Reply #7 on: December 05, 2012, 07:36:17 PM »
Lol, tkank you for this lessons ;-) It reassures me that everyone can not do it the first time ! (it's not juste me...)
It's something that you have to learn, some people just pick up certain things faster/easier than others. Like with pretty much everything in life...  ;)
Quote
I followed your advice to printf, which allowed me to compile (the problem for compilation).
Well, that's strange, as I copy&pasted your code without any modification and it compiled just fine for me...  ???
Quote
Now the program compiled, but I can not make it display the sum.
Well, the program does exactly what you tell it to do. Though that might not be what you intended...
Quote
I will follow your advice and those of Ralf (problem of reasoning etc.) and see if I can.
Seriously, sit down with a piece of paper and a pen and do one example step by step on paper, making a column on the left for the instruction, and several columns for the various variables you use on the right. I think you will find your issue pretty quickly this way, and the overall program is small enough to do this easily...
Quote
@ Ralf: I do not want anyone to make the excercie for me. I complete my marketing school, by computer. The hardest for me is to learn the reasoning (close to mathematics and very different from marketing). I have no pressure to succeed
Well, we can't "see" what a persons intentions are and this is a typical home assignment program, hence my comments. And we have frequently people coming in here with similar questions that in fact are homework assignments...
But even in your cause (or even more so), it doesn't help you to get a finished solution, as you would likely not learn anything out of it, and that's after all the whole purpose of it, right?  ;)
Quote
Thank you for your help !!
You're welcome... 8)

Ralf