CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2011
    Posts
    14

    Exclamation hey guys need help with example.

    ***********
    okay guys i need help with my count of correct answers... also need help filtering out duplicate numbers that are generated by random #s just how the directions say. Also how to re-enter into a loop; with y or n. PLEASE HELP ASAP! much <3

    ***************






    Write a program that keeps generating two random numbers between 2 and 9 and asks the user for the product of the two numbers, e.g.: "What is 4 x 6?". If the user answers correctly, the program responds with "Right!"; otherwise, it displays: Wrong! 4 x 6 = 24.

    Generate 5 such pairs of numbers and get the answers from the user. If at any time, both numbers are the same as last time, generate two new numbers. Continue generating 2 new numbers until at least one is different from last time.

    After presenting 5 pairs of numbers and getting the answers, display how many the user got right; e.g.: You got 4 of 5 right. Then, ask if he or she wants to play again, like so: "Do you want to play again? [y/n]". If the user answers with 'y' or 'Y', it generates another 5 pairs of numbers and asks for their products. If not, it quits generating numbers.

    If the user gets less than 25% of the last round of 5 questions, the program displays the multiplication table before terminating. Use a nested for loop to display the table. A bunch of cout statements will not be acceptable. You must also use a loop for any part that calls for repetition such as generating 5 pairs of numbers.

    The following is a sample interaction between the user and the program:

    What is 3 x 9? 27
    Right!

    What is 2 x 7? 14
    Right!

    What is 8 x 9? 63
    Wrong! 8 x 9 = 72

    What is 6 x 3? 21
    Wrong! 6 x 3 = 18

    What is 2 x 9? 18
    Right!

    You got 3 out of 5 right which is 60%.

    Play agian? [y/n] n

    (If the user had answered with y or Y the program would generate another 5 pairs of numbers and would again display the score and ask to do it again or not. But, because the user answered with n (or N), it quits, but before quitting, it prints the multiplication table since the score was less than 75%).

    You need to study the multiplication table:
    1 2 3 4 5 6 7 8 9
    2 4 6 8 10 12 14 16 18
    3 6 9 12 15 18 21 24 27
    4 8 12 16 20 24 28 32 36
    5 10 15 20 25 30 35 40 45
    6 12 18 24 30 36 42 48 54
    7 14 21 28 35 42 49 56 63
    8 16 24 32 40 48 56 64 72
    9 18 27 36 45 54 63 72 81

    Press any key to continue.

    Upload both cpp and exe files using the Browse and Upload buttons below and click Finish once.

  2. #2
    Join Date
    Nov 2011
    Posts
    14

    Re: hey guys need help with example.

    #include <ctime>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
    {

    int num = 0;
    int num2 = 0;
    int user = 0;
    int answer1 = 0;
    int count = 0;

    srand(unsigned(time(0)));

    for(int i=1 ; i<=5 ; i++)
    {
    num = (rand()&#37;8)+ 2;
    num2= (rand()%8)+ 2;
    answer1=num*num2;
    cout<<"\nWhat is "<<num<<" X "<<num2<<endl;
    cin>>user;

    if(answer1==user)
    cout<<"Correct!\n";
    count++;
    if(answer1!=user)
    cout<<"Wrong! -> "<<num<<" X "<<num2<<" = "<<answer1;

    }


    cout<<"\nYou got "<<count<<" out "<<"5 right!\n";















    system("pause");
    return 0;
    }

  3. #3
    Join Date
    Nov 2011
    Posts
    14

    Lightbulb Re: hey guys need help with example.

    Anyone ?? please lol i need help!

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: hey guys need help with example.

    You have some work to do to finalize this so I only give a comment as you know (I guess) that we don't provide homework solutions on this board.
    - rand&#37;8 will produce numbers between 0 and 7
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Nov 2011
    Posts
    14

    Re: hey guys need help with example.

    i dont need solution i need help. and no (rand()%8)+2 will give u #s from 2-9.

  6. #6
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: hey guys need help with example.

    The first thing to do is reformat that code properly.....

    Code:
    #include <ctime>
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    int main()
    {
    
        int num = 0;
        int num2 = 0;
        int user = 0;
        int answer1 = 0;
        int count = 0;
    
        srand(unsigned(time(0)));
    
        for(int i=1 ; i<=5 ; i++)
        {
            num = (rand()&#37;8)+ 2;
            num2= (rand()%8)+ 2;
            answer1=num*num2;
            cout<<"\nWhat is "<<num<<" X "<<num2<<endl;
            cin>>user;
    
            if(answer1==user)
                cout<<"Correct!\n";
            count++;
            if(answer1!=user)
                cout<<"Wrong! -> "<<num<<" X "<<num2<<" = "<<answer1;
    
        }
    
        cout<<"\nYou got "<<count<<" out "<<"5 right!\n";
    
        system("pause");
        return 0;
    }
    Now, let's go through the specific requirements....
    Write a program that keeps generating two random numbers between 2 and 9 and asks the user for the product of the two numbers, e.g.: "What is 4 x 6?". If the user answers correctly, the program responds with "Right!"; otherwise, it displays: Wrong! 4 x 6 = 24.
    You mostly have this part down. Depending on how picky your teacher is, you may get points off for outputting "Correct!" instead of "Right!". There are a couple of other things, you use X where the instructions say lowercase x, but the basic functionality is fine.

    Generate 5 such pairs of numbers and get the answers from the user. If at any time, both numbers are the same as last time, generate two new numbers. Continue generating 2 new numbers until at least one is different from last time.
    This will require 2 additional variables and one additional loop. It shouldn't be hard to add.

    After presenting 5 pairs of numbers and getting the answers, display how many the user got right; e.g.: You got 4 of 5 right.
    You attempted this but there's a problem with it. The indenting in the code above should provide a clue.

    Then, ask if he or she wants to play again, like so: "Do you want to play again? [y/n]". If the user answers with 'y' or 'Y', it generates another 5 pairs of numbers and asks for their products. If not, it quits generating numbers.
    Another loop. Not hard to add.

    If the user gets less than 25% of the last round of 5 questions, the program displays the multiplication table before terminating. Use a nested for loop to display the table. A bunch of cout statements will not be acceptable.
    You will need to write code to output the table inside an appropriate if statement.

  7. #7
    Join Date
    Oct 2011
    Posts
    59

    Re: hey guys need help with example.

    Quote Originally Posted by heymrjack23 View Post
    ***********
    okay guys i need help with my count of correct answers... also need help filtering out duplicate numbers that are generated by random #s just how the directions say. Also how to re-enter into a loop; with y or n. PLEASE HELP ASAP! much <3
    if a==b then reassign b to a new random number. Maybe a good idea to loop on this tool

    What do you mean reenter a loop? Do you mean to reiterate the loop body? Try googling for the answer: http://www.google.ca/search?q=c&#37;2B%2B+loops

    in psudocode it would look like this:
    Code:
    begin loop
      do stuff
    end loop if not done doing stuff
    or
    Code:
    begin loop if condition
      do stuff
    end loop // which will then go to back to the test condition

    You can use tolower() to make any character lowercase, or you can just test for the value being either of value A or B.

    Quote Originally Posted by S_M_A View Post
    You have some work to do to finalize this so I only give a comment as you know (I guess) that we don't provide homework solutions on this board.
    - rand%8 will produce numbers between 0 and 7
    Quote Originally Posted by heymrjack23 View Post
    i dont need solution i need help. and no (rand()%8)+2 will give u #s from 2-9.
    Hmmm, S_M_A just forgot the parenthesis. Other than that what do you think (rand()%8)+2-2 would give you?

    Exactly what sort of help are you looking for?


    A

  8. #8
    Join Date
    Nov 2011
    Posts
    14

    Re: hey guys need help with example.

    Thanks alot guys this all i needed.. When it comes to long problems I just over analyze the problem. Again thanks alot guys!!!

  9. #9
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: hey guys need help with example.

    My bad regarding the modulo code... I claim that it's late and friday...
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured