Coin counting (logic problem)

This is a discussion on Coin counting (logic problem) within the C++ Programming forums, part of the General Programming Boards category; --EDIT-- Sorry about that andyhunter...

  1. #31
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    --EDIT--
    Sorry about that andyhunter
    Last edited by Kleid-0; 01-17-2005 at 04:01 PM.

  2. #32
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Kleid-0 - sshhh

    won't post any code for that project, but it's a typical learning tool.

    Hint. Search your textbook for binary search method.
    See, we agree on stuff.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  3. #33
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    Quote Originally Posted by andyhunter
    One solution was originally in C and one in C++. No reason to get all huffy.
    Sorry, I think you misunderstood and thought that comment was directed toward you. It wasn't. The distinction I made about C and C++ was directed to when the OP was receiving advice to mix C code within his C++ code (ie. inlcuding stdio.h and using the getchar function), and then advised to defend poor design to the instructor to boot.

    I then posted my code to show that you and I were on the same page

  4. #34
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Couldn't find the Binary Search Method in my textbook...
    Anyway, I started it...
    Code:
    #include <iostream.h>
    main()
          int guess, ans, ans2;
          int high = 100;
          int low = 1;
          
          cout << "Think of a number between 1 and 100\n";
          cout << "My first guess is 50, is your number higher, lower, or 50?";
          cout << "\n(higher = 1, lower = 2, correct guess = 0)";
          cin >> ans;
          
          if (ans = 1)
             low = 50;
             cout << "My guess is 75, is your number higher, lower, or 75?";
             cout << "\n(higher = 1, lower = 2, correct guess = 0)";
                  if (ans2 = 1)
                     high = 100;
                     low = 76;
                     cout << "My guess is 88, is your number higher, lower, or 88?";
                     cout << "\n(higher = 1, lower = 2, correct guess = 0)";
                  else
                      if (ans2 = 2)
                      high = 74;
                      low = 51;
                      cout << "My guess is 62, is your number higher, lower, or 62?";
                      cout << "\n(higher = 1, lower = 2, correct guess = 0)";
                      if (ans2 = 0)
                      ???????????
                      ???????????
    What would I put where the question marks are, to end the program?
    I'm probably starting this wrong anyway. So maybe a few suggestions would help. I started with, if the user entered 1, as in the number is greater than 50.
    Last edited by xlokix; 01-17-2005 at 04:07 PM.

  5. #35
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Actually, I'm in C++ 1, and there will be no C++ 2 next year. So I will not learn more. And it's only a half a year class, so I'm finished with the class in about a week.
    Dude, have you been sleeping throught this class?

    edit:

    Ok here is some psuedocode:
    Code:
    Set guessnumber = 0
    Set a "found" flag to false
    Set lower index to min number (your case 0)
    Set higher number to max number(your case 100)
    while found is false
           guess = (max + min) / 2
           Ask if lower or higher
           if higher then
                  min = guess
           if lower than
                  max = guess
           if guess = number then
                   found = true
           guessnumber = guessnumber + 1
    loop
    Print I guessed your number in guessnumber tries
    This should help.
    Last edited by andyhunter; 01-17-2005 at 04:21 PM.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  6. #36
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    No, there's no room to sleep on the desk with the computer there and all.

  7. #37
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Look above I edited my post.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  8. #38
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Ahhhhh. Let me try this out.

    What do you mean by "found", never done that before :x
    Last edited by xlokix; 01-17-2005 at 04:30 PM.

  9. #39
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Just an integer variable indicating a true (1) or false (0) condition to be used in the while loop.
    i don't think most standard compilers support programmers with more than 4 red boxes - Misplaced

    It is my sacred duity to stand in the path of the flood of ignorance and blatant stupidity... - quzah

    Such pointless tricks ceased to be interesting or useful when we came down from the trees and started using higher level languages. - Salem

  10. #40
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    Code:
    bool found = false;
    And remember pseudo doesn't exactly have to deal with the language's symantics.

  11. #41
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Blah!!! I was sure I got it this time. Anyway, I need help again.
    I'm not sure if it's just crashing, or it's closing again. But I tried the getchar();getchar(); and that didn't stop it from closing, so I don't know.

    Code:
    #include <iostream.h>
    
    main()
    {
         int max = 100;
         int min = 1;
         int guess, ans;
         
         cout << "Think of a number between 1 and 100 and I will try to guess it\n";
       do
       {
         guess = (max+min) / 2;
         cout << "My guess is " << guess << ", is your number higher,lower, or 50?\n";
         cout << "(higher = 1, lower = 0, correct guess = 2)";
         
         if (ans = 1)
            min = guess;
         if (ans = 0)
            max = guess;
         if (ans = 2);
       }
       while (ans != 2)
       cout << "I guessed your number!";
       return 0;
    }
    For those who don't know, The objective of the program was for the user to think of a number between 1 and 100 and the program tries to guess what number it is.

    edit:
    Code:
    #include <iostream.h>
    #include <stdio.h>
    
    main()
    {
         int max = 100;
         int min = 1;
         int guess, ans;
         
         cout << "Think of a number between 1 and 100 and I will try to guess it\n";
       do
       {
         guess = (max+min) / 2;
         cout << "My guess is " << guess << " is your number higher,lower, or 50?\n";
         cout << "(higher = 1, lower = 0, correct guess = 2)";
         cin >> ans;
         
         if (ans = 1)
            min = guess;
            else
                if (ans = 0)
                max = guess;
                    else
                        if (ans = 2)
       }
       while (ans != 2);   
       cout << "I guessed your number!";
       getchar();getchar();
       return 0;
    }
    Well, I corrected myself, It works good now.
    P.S. thanks for the pseudo code!

    edit edit: Actually It's still messed up, 0 and 2 don't work, no matter what I press it keeps going as if I went higher.
    Last edited by xlokix; 01-19-2005 at 11:51 AM.

  12. #42
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    I see this in your code
    Code:
         if (ans = 1)
    It will never make it past the first "if" because you are assigning "=" 1 to ans so it will always be true. I think you want to compare "==" to 1 so that would be this.
    Code:
         if (ans == 1)
    I only picked the first one you have two more to look at.
    Code:
            else
                if (ans = 0)
                max = guess;
                    else
                        if (ans = 2)

  13. #43
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    Ahhhhhhhhh! My teacher would kill me for that. Woops! Thanks man, that worked .

  14. #44
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Here's one solution I wrote a while ago:
    The comments, variable names and money is in Swedish, so try to figure the code out as an excercise.
    Code:
    int main()
    {
        using namespace std;
        
        //De här fälten innehåller de sedlar och namn på sedlar vi använder
        //Endast dessa behöver ändras vid en enventuell införsel av Euro
        const int   valorer[]   = {500,100,50,20,10,5,1,0};
        const char* valorNamn[] = {"Femhundralappar","Hundralappar","Femtiolappar","Tjugolappar","Tiokronor",
                                   "Femkronor","Enkronor",0};
    
        //Hämta input
        int belopp, betalat;
        cout << "Skriv in belopp: ";
        cin >> belopp;
        cout << "Skriv in hur mycket du betalar med: ";
        cin >> betalat;
    
        //Beräkna växel
        int tillbaka = betalat - belopp;
    
        cout << "Tillbaka " << tillbaka << " kronor" << endl;
    
        //Gå igenom listan med de olika valörerna
        const int*   valor = valorer;
        const char** namn  = valorNamn;
        while (*valor && *namn)
        {
            //Så här många av denna typ ges tillbaka
            const int antal = tillbaka / *valor;
            //Minska summan med summan vi nu ger tillbaka
            tillbaka = tillbaka % *valor;
    
            //Visa att vi ger tillbaka 
            cout << *namn << " : " << antal << endl;
            
            ++valor;
            ++namn;
        }
    }
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #45
    Registered User
    Join Date
    Jan 2005
    Posts
    18
    It's like you're speaking 3 different languages!

Page 3 of 4 FirstFirst 1234 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Josephus problem variant logic troubles
    By misterMatt in forum C++ Programming
    Replies: 0
    Last Post: 04-29-2009, 02:38 PM
  2. Replies: 5
    Last Post: 01-31-2006, 12:54 AM
  3. Logic problem?
    By biosninja in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-10-2002, 04:10 PM
  4. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 04:02 PM

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