Thread: problem with a simple program

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    problem with a simple program

    I am new to C++/C programming and i was doing a little practice with "for" loops.
    The program asks the user to enter a sum of money and prints all possible ways to pay the sum with the following cents : 5, 10, 20, 50
    for example if you enter a sum of 20, it says:

    How many cents ? > 20
    0 50-cent 1 20-cent 0 10-cent 0 5-cent
    0 50-cent 0 20-cent 2 10-cent 0 5-cent
    0 50-cent 0 20-cent 1 10-cent 2 5-cent
    0 50-cent 0 20-cent 0 10-cent 4 5-cent



    The problem:

    How many cents ? > 30
    0 50-cent 1 20-cent 1 10-cent -1 5-cent
    0 50-cent 1 20-cent 0 10-cent 2 5-cent
    0 50-cent 0 20-cent 3 10-cent -1 5-cent
    0 50-cent 0 20-cent 2 10-cent 2 5-cent
    0 50-cent 0 20-cent 1 10-cent 4 5-cent
    0 50-cent 0 20-cent 0 10-cent 6 5-cent


    So it sometimes prints -1 instead of 0 . Some help please.

    The code:
    Code:
    #include <stdio.h>
    #include <conio.h>
    
    int main(void) 
    { 
    int a=0, i=0, j=0, k=0, sum1=0, sum2=0, sum3=0, sum4=0;
    int cent, temp1, temp2, temp3, temp4;
    printf("How many cents ? >");
    scanf("%d", &cent);
    temp1=((int)(cent/50+0.0001));
    temp2=((int)(cent/20+0.0001));
    temp3=((int)(cent/10+0.0001));
    temp4=((int)(cent/5+0.0001));
    
      for(a=temp1;a>=0;a--){ //50 - cent
      sum1=a*50;
      if(sum1==cent){printf("%d 50-cent, %d 20-cent, %d 10-cent, %d 5-cent \n",a, i, j, k);continue;} 
         for(i=temp2;i>=0;i--){ // 20 - cent
               sum2=sum1+(i*20);
               if(sum2==cent){printf("%d 50-cent, %d 20-cent, %d 10-cent, %d 5-cent \n",a, i, j, k);continue;} 
               for(j=temp3;j>=0;j--){ // 10 - cent
                     sum3=sum2+(j*10);
                     if(sum3==cent){printf("%d 50-cent, %d 20-cent, %d 10-cent, %d 5-cent \n",a, i, j, k);continue;}
                     for(k=temp4;k>=0;k--){ // 5 - cent
                           sum4=sum3+(k*5);
                           if(sum4==cent){printf("%d 50-cent, %d 20-cent, %d 10-cent, %d 5-cent \n", a, i, j, k);continue;}
    }
    }
    }
    }
        printf("\nPress any key...");
        getch(); 
        return 0;
    }
    Thanks in advance

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    1. this is C - so should be moved to the correct forum
    2. cent/50 is int so doing (int)(cent/50+0.0001) is pointless

    3. for(k=temp4;k>=0;k--)
    what will be a value of k when you exit this loop?
    what will be value of k when you enter the next iteration of outher loop?
    what will be printed in case where the sum1 will match the cent?

    if your sum contains only 50-cent monyes why do you need to print values of i,j,k?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    progress

    i was able to solve the problem of 5-cent "-1" by using for(k=temp4;k>0;k--). However, i cant do that with other loops. For example
    How many cents ? >100
    prints a "-1" on 10-cent coin when 100 is summed by 5x20-cent.
    At the same time the loop can not be for(j=temp3;j>0;j--) to work properly.

    This problem is solved when i dont print the value of j and k (in the 100 cent example), but i dont like that very much.
    There must be a more elegant jet simple way...

  4. #4
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    sum2 is buld of a 50-cents and i 20cents, so correct printf will be

    printf("%d 50-cent, %d 20-cent, 0 10-cent, 0 5-cent \n",a, i);

    could you fix same way other outputs?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    3

    el fin

    jes, the problem is solved.
    Thanks, vart

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  2. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  3. Running Program Problem
    By warfang in forum C++ Programming
    Replies: 10
    Last Post: 03-28-2007, 02:02 PM
  4. simple login program problem
    By suckss in forum C Programming
    Replies: 11
    Last Post: 11-11-2006, 05:02 PM
  5. Problem with a simple program
    By Salgat in forum C Programming
    Replies: 10
    Last Post: 06-15-2006, 05:57 PM