Thread: Loop help

  1. #1
    Registered User
    Join Date
    Sep 2009
    Posts
    32

    Loop help

    Allright pretty much i just need help on loop


    Basically, Im supposed to make a program that involves two games: Arithmetic and Factorization. The latter is what I'm having trouble with

    Here's what I've written.

    This is the factorization function, which it will return a value in seconds to main (dont worry about this part)


    Code:
    double factorGAME (int max, int quantity) {
          
    
           int correct, a, b, i, guesses=1, answer, points;
           int wrong_ans=0;
            //This is will start the time
           double start = time(0);
           
           
           
           for (i=1; i <= quantity; i++){
                 //Two random numbers will be set the question
                 int x = rand()%max+1;
                 int y = rand()%max+1;
                 correct = x*y;       
                 
              
                 printf("Please factor %d\n", correct);
                 scanf("%d %d", &a, &b);
                 answer = a*b;
                 
                 
                 
                 if (answer == correct){
                    
                    if (a == x && b == y);
                    else if (a == y && b == x);
                    
                    else
                    
                          
                        printf("Sorry, that is not the factorization we are looking for\nPlease try again.\n");
                        
                    
                 }
                 //when the user types the wrong answer, this will display
                 else{
                     printf("Sorry, %d x %d does NOT equal %d\n", a, b, correct);
                     printf("Please try again\n");
                     guesses+=1;
                     //it adds 5 seconds penalty
                     wrong_ans= wrong_ans + 5;
                     //the loop will set the program to repeat itself until the right answer is given
                     while (answer != correct) {
                           scanf("%d %d", &a, &b);
                           answer = a*b;
                           
                           if (answer == correct);
                           else
                               {
                               printf("Sorry, %d x %d does NOT equal %d\n", a, b, correct);
                               printf("Please try again\n");
                               guesses+=1;
                           }
                     }
                 }
                 printf("Correct, great job!\n");
                 printf("You got the factorization in %d guesses\n", guesses);
          } 
          // Ends the timer for factorization game
          double end = time(0);
    
          double time_spent = end + wrong_ans - start;
          double performance;
          
          performance = time_spent / quantity;
          
          return performance;
           
    }
    The prob is that when a number, like 24 for instance, it causes prob.
    there are multiple factors that sometime doesnt match up the user's answer to the comp

    I want the output to write something like:


    ...
    Please Factor 124
    4 31
    Sorry, that is not the factorization we are looking for.
    Please try again
    2 62
    Correct, great job!
    Any solutions?

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Sounds like a basic logic problem. Start by writing pseudo code or a flowchart.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Neotriz View Post
    Allright pretty much i just need help on loop


    Basically, Im supposed to make a program that involves two games: Arithmetic and Factorization. The latter is what I'm having trouble with

    Here's what I've written.

    This is the factorization function, which it will return a value in seconds to main (dont worry about this part)


    Code:
    double factorGAME (int max, int quantity) {
          
    
           int correct, a, b, i, guesses=1, answer, points;
           int wrong_ans=0;
            //This is will start the time
           double start = time(0);
           
           
           
           for (i=1; i <= quantity; i++){
                 //Two random numbers will be set the question
                 int x = rand()%max+1;
                 int y = rand()%max+1;
                 correct = x*y;     
                 wrong = 1;  
                 
               while(wrong) {          
                 printf("Please factor %d\n", correctly);
                 scanf("%d %d", &a, &b);
                 answer = a*b;
                 
                 
                 
                 if (answer == correct){
                   if (a == x || b == x) {
                      printf("\n Good Job! You are correct! ");
                      printf("You got the factorization in %d guesses\n", guesses);
                      wrong = 0;
                      continue;
    
                   }         
                   else                       
                      printf("Sorry, that is not the factorization we are looking for\nPlease try again.\n");   
                 }
                 if(answer != correct) {
                   //when the user types the wrong answer, this will display
                   printf("Sorry, %d x %d does NOT equal %d\n", a, b, correct);
                   printf("Please try again\n");
                   guesses+=1;
                   //it adds 5 seconds penalty
                   wrong_ans= wrong_ans + 5;
                   //the loop will set the program to repeat itself until the right answer is given
                }
                 
             } //end of while loop
              if (wrong == 0)
                   break;   //break out of the for loop if guess was correct
          } //end of for loop 
          // Ends the timer for factorization game
          double end = time(0);
    
          double time_spent = end + wrong_ans - start;
          double performance;
          
          performance = time_spent / quantity;
          
          return performance;
           
    }
    The prob is that when a number, like 24 for instance, it causes prob.
    there are multiple factors that sometime doesnt match up the user's answer to the comp

    I want the output to write something like:


    ...


    Any solutions?
    Something like the above is what you want. Remember that if the two numbers given by the user are factors of the correct number, then if only one of those factors is a correct guess, the other factor MUST BE correct. (otherwise the answer couldn't be correct)


    I haven't run the above, so consider it prototype or idea code,not ready to run in your program, code.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Poll event loop
    By rogster001 in forum C++ Programming
    Replies: 2
    Last Post: 09-17-2009, 04:28 AM
  2. need help with a loop
    By Darkw1sh in forum C Programming
    Replies: 19
    Last Post: 09-13-2009, 09:46 PM
  3. funny-looking while loop
    By Aisthesis in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2009, 11:54 PM
  4. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM