Results 1 to 8 of 8

Thread: Newbie C++ programming,need help!

  1. #1
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Qt3
    Platforms
    Windows

    Default Newbie C++ programming,need help!

    Hi,I have been trying to learn C++ programming from the book called C++ Without Fear 2nd Edition.There is a challenging exercise which I couldn't solve.Is anyone skilled enough to provide me with hint or display your source code so I can learn?Please help!

    Exercise 2.5.2 For the more ambitious: Write a version that permits subtracting any number from 1 to N, where N is stipulated at the beginning of the game. For example, the user when prompted might say that each player can subtract any number from 1 to 7. Can you create an optimal computer strategy for this general case?

    *In addition,you have to also prompt the user for any total number to be subtracted.


    If you guys don't get it maybe a preview of the book might be helpful for you solvers.
    Initially the author created a subtraction game that ensures that the computer will win almost everytime unless the user enters correctly which is 3. The computer will win if it is the first to reduce the total to zero. For example:
    1. We agree to start with the number 7,and you go first.
    2.You subtract 2 from the total,making it 5.
    3.I subtract 2 from the total,making it 3.
    4.You subtract 1 from the total,making it 2.
    5.I subtract 2 from the total making it 0.I win!

    Here is the source code:

    Qt Code:
    1. using namespace std;
    2. int main(){
    3. int total,n;
    4. do{
    5. cout<<"Welcome to NIM.Pick a total (total>1): ";
    6.  
    7. cin>>total;
    8. if(total<1){
    9. cout<<"Please re-enter input."<<endl;
    10. }
    11. }while(total<1);
    12. while(true){ //infinite loop
    13. //Pick best response and print results.
    14. if((total%3)==2){
    15. total=total-2;
    16. cout<<"I am subtracting 2."<<endl;
    17. }else{
    18. total--;
    19. cout<<"I am subtracting 1."<<endl;
    20. }
    21. cout<<"New total is "<<total << endl;
    22. if(total==0){
    23. cout<<"I win!"<<endl;
    24. break;
    25. }
    26. //Get user's response;must be 1 or 2.
    27. cout<<"Enter num to subtract (1 or 2): ";
    28. cin>>n;
    29. while(n<1||n>2){//validation
    30. cout<<"Input must be 1 or 2."<< endl;
    31. cout<<"Re-enter: ";
    32. cin>>n;
    33. }
    34. total=total-n;
    35. cout<< "New total is "<<total << endl;
    36. if(total==0){
    37. cout<<"You win!"<<endl;
    38. break;
    39. }
    40. }
    41. system("PAUSE");
    42. return 0;
    43. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 14th March 2013 at 10:47. Reason: missing [code] tags

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows
    Thanks
    21
    Thanked 418 Times in 411 Posts

    Default Re: Newbie C++ programming,need help!

    This usually works the other way around - you show us what you have done and where you get the problems, we help you from there.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Newbie C++ programming,need help!

    Hi,I am really glad someone has responded to my thread.I really appreciate it. The problem is I have not started in typing out the source code because I still couldn't find a way to solve it. I am still overwhelm by two factor:
    1) The total is being first specified by the user. This can be any number which I find it is 'Infinite' numbers for me to cater to.
    2) Subtraction ranging from 1 to N. The N can be any number which is in the sense 'Infinite' numbers.

    The book is really good in teaching C++ to newbie because of its simple explanation & exercises to apply what readers have learned from the book
    to help improve their programming skill.However, it somehow lack answers for the exercises provided.

    I have thought initially that maybe if I have a deep understanding of the previous example I could solve this exercise. I believe the preview coding which I have shown works because it always eliminates the remainder of the total number given by the user. So in anyway, even if the user randomly subtract the total, the logic is always to elminate the remainder by checking with %3.
    For example, let say the user provides a total of 10.
    11%3 will give a remainder of two. So I will subtract two from it forcing it to be a multiplication of 3.
    Therefore,the total now is 9.
    Next,let say the user subtracts 1.
    The total is 8.
    8%3 will give a remainder of two.I will then subtract two from it forcing it to be a multiplication of 3.
    The total is 6.
    .
    .
    .
    So do you see the pattern now?
    The program is designed that it will meet its target of reaching 3 to win the game.Why so?
    This I believe is also due to the program having the chance to start first in subtracting.
    Hence, whenever it reaches 3, the user will subtract and the computer will win the game by subtracting
    whatever is left.
    Yup,but I still can't figure out the exercise though.
    Do you have any idea of solving it?
    Last edited by bestmaster99; 14th March 2013 at 14:06.

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Newbie C++ programming,need help!

    Quote Originally Posted by bestmaster99 View Post
    1) The total is being first specified by the user. This can be any number which I find it is 'Infinite' numbers for me to cater to.
    You store the value in some variable and then operate on it.

    2) Subtraction ranging from 1 to N. The N can be any number which is in the sense 'Infinite' numbers.
    No, it can vary from 1 to the number specified by the user which sits in the variable.

    Do you have any idea of solving it?
    You can use the min-max algorithm. It's a classic when it comes to this particular game.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Newbie C++ programming,need help!

    min-max algorithm?

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Newbie C++ programming,need help!

    Yes, min-max algorithm.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Mar 2013
    Posts
    4
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: Newbie C++ programming,need help!

    I have searched online about min-max algorithm. Haha,I am new to programming.What exactly is the algorithm and how do I write the source code?

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,376
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    4
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Newbie C++ programming,need help!

    Search again then. This algorithm is also called "minimax".
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. a newbie..
    By Lady Sophia in forum General Discussion
    Replies: 3
    Last Post: 1st May 2012, 01:15
  2. Replies: 3
    Last Post: 25th September 2011, 19:41
  3. Newbie Says Hi
    By ScottBrady in forum General Discussion
    Replies: 1
    Last Post: 25th July 2011, 03:56
  4. Complete newbie with programming and Qt
    By Jacob in forum Newbie
    Replies: 1
    Last Post: 6th March 2010, 07:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.