Thread: Help with writing functions using arrays

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    Help with writing functions using arrays

    I need some help with passing arrays through a function. A portion of the program calls for us to determine the number of digits incorrectly guessed.


    Code:
    // Pre-conditions: board and answer are of length NUM_PEGS and store the player's
    //                 guess and the correct board, respectively.
    // Post-conditions: returns the number of pegs that match at INCORRECT spots
    //                  after "erasing" those pegs that match at correct spots.
    int numWrongPlaceMatches(int board[], int answer[]) {
       
           
        // Create temporary copies of both boards, since we'll need to make changes
        // to each during this process and we don't want to ruin the original
        // state of either board.
        int i, temp1[5], temp2[5];
        for(i=0; i<NUM_PEGS; i++){
                 temp1[i] = board[i];
                 temp2[i] = answer[i];
                 }
        
       
        
        // Mark out the spots on the board and answer that match up perfectly.
        for(i=0; i<NUM_PEGS; i++){
                 markOutCorrect;
                 }
        
        // Go through the board and try to match each peg on the board to
        // a spot on the answer. Hint: A double loop is needed.
        // Note: Remember to "mark" an answer when a match is found.
        int match, wrong=0;
        for(i=0; i<NUM_PEGS; i++){
         if (answer[i] != MARKED_ANSWER)
            wrong++;
            }
        for(i=0; i<NUM_PEGS; i++){
                 board[i] = temp1[i];
                 answer[i] = temp2[i];
        }
         
         return wrong;
    }                  
    
    // Pre-conditions: dest and source are both have the given length.
    // Post-conditions: All values from source are copied into the corresponding
    //                  locations in dest.
    void copyArray(int dest[], int source[], int length) {
         int i;
         for(i=0; i<NUM_PEGS; i++){
                  dest[i] = source[i];
         }
        
    }
    
    // Pre-conditions: board and answer are of length NUM_PEGS.
    // Post-conditions: For each corresponding slot that matches in both boards,
    //                  the slots are changed to equal each board's appropriate
    //                  marker.
    void markOutCorrect(int board[], int answer[]) {
         int i;
         for(i=0; i<NUM_PEGS; i++){
                  if(board[i] == answer [i]){
                              board [i] = MARKED_BOARD;
                              answer[i] = MARKED_ANSWER;
                  }
         }
                  
    }

    That is the code that I have so far for that section. I am not looking for anyone to answer this for me, I only wish to understand how I would go about solving this. If anyone could help it would be greatly appreciated.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Code:
    void foo ( int a );
    int main ( ) {
      int fred;
      foo( fred );
    }
    vs
    Code:
    void foo ( int a[] );
    int main ( ) {
      int fred[100];
      foo( fred );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    7
    You can use pointers. It is the fastest and the best solution.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by sunny1778 View Post
    You can use pointers. It is the fastest and the best solution.
    Since you weren't clear as to what you were referencing with this statement, let me point out that this:
    Code:
    void foo( char bar[] )
    Is the same as:
    Code:
    void foo( char *bar )

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Ok I got it able to calculate numiperfectmatches now, but for some reason the do-while look that this function is in in main isn't executing. Is there something that I need to add to the function for this loop to execute?

    Loop in main:
    Code:
    // Loop through each turn, ending at 10 moves or a win.
        do {
            
            // Read in the user's guess.
            getUserGuess(board);
            
            // Figure out the number of matches.
            int num_perfect = numPerfectMatches(board, answer);
            int num_imperfect = numWrongPlaceMatches(board, answer);
            
            // Update for this turn.
            printf("You have %d perfect matches and %d imperfect matches.\n", num_perfect, num_imperfect);
            num_turns++;
            
        } while (num_turns < MAX_TURNS && numPerfectMatches(board, answer) < NUM_PEGS);
    Working function:
    Code:
    // Pre-conditions: board and answer are of length NUM_PEGS and store the player's
    //                 guess and the correct board, respectively.
    // Post-conditions: returns the number of pegs that match at INCORRECT spots
    //                  after "erasing" those pegs that match at correct spots.
    int numWrongPlaceMatches(int board[], int answer[]) {
       
           
        // Create temporary copies of both boards, since we'll need to make changes
        // to each during this process and we don't want to ruin the original
        // state of either board.
        int i, temp1[5], temp2[5];
        for(i=0; i<NUM_PEGS; i++){
                 temp1[i] = board[i];
                 temp2[i] = answer[i];
                 }
        
       
        // Mark out the spots on the board and answer that match up perfectly.
        for(i=0; i<NUM_PEGS; i++){
                  if(board[i] == answer [i]){
                              markOutCorrect(board, answer);
                  }
        }
        // Go through the board and try to match each peg on the board to
        // a spot on the answer. Hint: A double loop is needed.
        // Note: Remember to "mark" an answer when a match is found.
        int right, wrong;
        for (i=0; i<NUM_PEGS; i++){
            for(i=0; i<NUM_PEGS; i++){
                 if(board[i] == MARKED_BOARD){
                             right++;
                 }
            board[i]= temp1[i];
            answer[i] = temp2[i];
            }
         
        }   
     
        wrong = NUM_PEGS - right;
        
        return wrong;     
                             
    }

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Ok so i figured out that the loop doesn't execute because the value of numPerfectMatches is greater than NUM_PEGS.

    Would I be able to alter the value of the function to make it less than NUM_PEGS?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by chrisjg04 View Post
    Ok so i figured out that the loop doesn't execute because the value of numPerfectMatches is greater than NUM_PEGS.

    Would I be able to alter the value of the function to make it less than NUM_PEGS?
    I'm confused. It's a do-while loop, right? They always execute once.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sizes of arrays within functions
    By tempster09 in forum C Programming
    Replies: 3
    Last Post: 01-13-2010, 11:17 PM
  2. Passing arrays of pointers into functions
    By ashley in forum C Programming
    Replies: 5
    Last Post: 01-13-2007, 06:48 PM
  3. Arrays and Functions
    By w274v in forum C Programming
    Replies: 19
    Last Post: 11-03-2005, 01:47 PM
  4. elements of arrays; functions
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-03-2001, 01:48 AM
  5. writing arrays to files
    By Zaarin in forum C++ Programming
    Replies: 1
    Last Post: 08-30-2001, 11:26 PM