Please Help

This is a discussion on Please Help within the C++ Programming forums, part of the General Programming Boards category; I have a project due in a programming class at the college today. I have know idea how to begin. ...

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    5

    Please Help

    I have a project due in a programming class at the college today.
    I have know idea how to begin. The project is in C++, and I have a compiler that I borrowed from the school and loaded onto my computer. The project says that I have to come up with some amount of money that is less than 100$. Then it has to loop until it gets to zero. When it gets to zero the project must then terminate. This is basic programming but I have no clue how to do this could anyone please help.

    Thanks,
    Kyle

  2. #2
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Considering that it's an assignment, I'm sure the professor didn't spring this on you suddenly. You might have wanted to start sooner. Further, you didn't reguritate enough of the assignment to even know where to start. Since it's a class, I'm sure the professor believes that he's taught enough for you to be able to do the assignment. Good luck with that.

    A good place to start would be an int main()
    Code:
    int main()
    {
       //do stuff
       return 0;
    }
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  3. #3
    Registered User
    Join Date
    Feb 2005
    Posts
    5
    No he didnt spring this on me suddenly but shen I loaded the compiler onto my computer my hard drive died so we had to fix that. I have to figure out a way to come up with that amount using the least amount of bills and coins. But any help would be appreciated.

  4. #4
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Google is your friend. If it's the algorithm you're having difficulty with, look there.

    If it's programming that you're having difficulty with, post what you've got, and we'll try to help.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Straight-forward (but not necesarily ideal, if the coin values are trickily chosen) method is to have the coin values in an array, sorted from greatest to smallest value. Then you walk through the array, substracting the coin value from the total until it won't fit anymore, tracking the count. Then you step to the next coin value and repeat.
    Pseudocode:
    Code:
    int coin_values[] = { 50, 20, 10, 5, 2, 1 };
    int money = random(between 50 and 100);
    int coin_usage[coin_values.length] = {0};
    
    for(i = 0; i < coin_values.length; ++i) {
      while(money > coin_values[i]) {
        ++coin_usage[i];
        money -= coin_values[i];
    }
    
    print("Coins used:\n");
    for(i = 0; i < coin_values.length; ++i) {
      print(coin_values[i] + ": " + coin_usage[i] + "\n");
    }
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  6. #6
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Ha. That's hardly pseudo-code. After just a few adjustments, that's working code. Anyway, can you explain the part you mentioned about "if the coin values are trickily chosen"? What values would be tricky?
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Can't remember exactly, but I believe when both the 3 and 2 coins exist, it's possible not to have the ideal amount.

    But perhaps this is only when there's no 1 coin.

    E.g. Coin values are 1, 5, 20, 30 and 50. Money is 60. Using my algorithm, you need 3 coins (50 + 5 + 5), but the ideal way uses 2 (30 + 30).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by CornedBee
    Pseudocode:
    Code:
    int coin_values[] = { 50, 20, 10, 5, 2, 1 };
    int money = random(between 50 and 100);
    int coin_usage[coin_values.length] = {0};
    
    for(i = 0; i < coin_values.length; ++i) {
      while(money > coin_values[i]) {
        ++coin_usage[i];
        money -= coin_values[i];
    }
    
    print("Coins used:\n");
    for(i = 0; i < coin_values.length; ++i) {
      print(coin_values[i] + ": " + coin_usage[i] + "\n");
    }
    Pseudocode is a generic way of describing an algorithm without use of any specific programming language syntax.
    http://en.wikipedia.org/wiki/Pseudocode
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can't have code, even pseudo-code, without intentionally or unintentionally following at least a bit the syntax of some programming language.

    This said, my pseudo-code always looks extremely C-like - it's what I'm most comfortable with.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #10
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Pseudocode V2.1

    create an array for coin values
    create and initialize an array with the same amount of elements as the array of coin values to count how many times each coin was used

    retrieve the amount of money (random number 50..100?)

    loop through the array of coin values
    for each coin value, while the money is greater than the current coin value, add one to the coin usage array index that represents that coin value and subract the amount from the total amount of money

    output the values array next to the usage array and you're done
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  11. #11
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    That's an algorithm description, not pseudocode.

    And never mind that the Wikipedia says that's exactly what pseudocode is...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  12. #12
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by CornedBee
    That's an algorithm description, not pseudocode.
    okay then...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  13. #13
    Whats gonna happen now! himanch's Avatar
    Join Date
    Feb 2005
    Location
    Armenia
    Posts
    26
    go and read the loop part from the tutorials!!

    ur gonna see that it very very very easy!!!

  14. #14
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Quote Originally Posted by CornedBee
    Can't remember exactly, but I believe when both the 3 and 2 coins exist, it's possible not to have the ideal amount.

    But perhaps this is only when there's no 1 coin.

    E.g. Coin values are 1, 5, 20, 30 and 50. Money is 60. Using my algorithm, you need 3 coins (50 + 5 + 5), but the ideal way uses 2 (30 + 30).
    Ah, of course. I came up with something similar while I was at lunch. Coin values of 4, 3, and 1, trying to make change for 6. Ideal is 2 coins (3 + 3), but the algorithm uses 3 (4 + 1 + 1). Seems like an interesting problem to solve.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21