Thread: The best way to make two array functions distinguish guesses vs actual?

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    4

    The best way to make two array functions distinguish guesses vs actual?

    In the program I am making the user will enter in 4 numbers ranging from 1 to 6. he will have twenty attempts to guess the correct numbers and in the correct order. to do that i need to make two void function arrays.


    Code:
    void calculate correct numbers
        if(answer[1]==guess[0] || answer[2]==guess[0] || answer[3]==guess[0]){
            correctnum++;
      }
        if(answer[0]==guess[1] || answer[2]==guess[1] || answer[3]==guess[1]){
            correctnum++;
      }
        if(answer[0]==guess[2] || answer[0]==guess[2] || answer[3]==guess[2]){
            correctnum++;
      }
        if(answer[0]==guess[3] || answer[1]==guess[3] || answer[2]==guess[3]){
            correctnum++;
      }
    
    
    
    void calculate correct locations /*something!*/
        if(answer[0]==guess[0]){
            correctloc++;
     }
        if(answer[1]==guess[1]){
            correctloc++;
    
     }  if(answer[2]==guess[2]){
            correctloc++;
    
     }  if(answer[3]==guess[3]){
            correctloc++;
     }
    however i was told the best way to do the "correct numbers" function is to put a for loop within a for loop, but i wasn't sure about that. i also was told that it was recommended to create a "dummy" function so that i don't get too high of a number when guessing the actual numbers. ie:

    real- 1, 2, 3, 4
    guess- 1, 1, 1, 4 ----> that should be two correct choices, the first guessed 1 and the 4.

    any guidance would be greatly appreciated. thank you!

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Sounds like a Mastermind game.
    The correct function for "calculate correct locations" would be:
    Code:
    void calculate_correct_locations(void) {
    (no spaces in the function's name)
    The "calculate correct numbers" function needs more work.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Start by correctly defining your functions:
    Code:
     return_type func_name(arg_type arg, ...){
        // do stuff
    }
    makes a function named func_name, which takes arguments arg (of type arg_type), and returns some value of type return_type.

    So start with what nonoob gave you for your correct locations function.

    Quote Originally Posted by nonoob View Post
    Code:
    void calculate_correct_locations(void) {
    (no spaces in the function's name)
    and do the same for your correct numbers function.

    As far as the for loop is concerned, is your problem that you don't know how to use a for loop, or just that you can't see how it is useful in this situation?

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    4
    i understand for loops but the idea of doing a double for loop doesn't make sense in this situation. i wouldn't even know how to create that to be accurate. however i'm sure there are many other ways to avoid going that route so that i don't need to make another array with a dummy value. thanks.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    The way I would do it is:
    Code:
    for each guessed
        for each actual
            if guessed=actual
                continue;
            if board[guessed]=board[actual]
                correct num++;
                break;
    Last edited by FrankTheKneeMan; 04-06-2011 at 01:32 PM. Reason: code tag. Again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Make an array of arrays
    By JOCAAN in forum C Programming
    Replies: 3
    Last Post: 01-02-2009, 12:18 PM
  2. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  3. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM
  4. how to make a poiner array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 12-03-2001, 09:12 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM