Thread: Guess a 4 int array, with how many are in the right and wrong positions

  1. #1
    Registered User
    Join Date
    Feb 2013
    Posts
    2

    Guess a 4 int array, with how many are in the right and wrong positions

    For an assignment I have to create a random array of four integers, and then I have to allow someone to input up to ten guesses to guess the array in the correct order. I also need to be able to display whatever was generated by inputting -1. Finally, after every guess I have to tell the inputter how many of the guessed integers are correct and in the correct position, as well as how many integers are correct but not in the correct position.

    So far I've been able to get the random array to generate properly, but inputting negative one has no effect, although if I input it four times in a row I get to my 'lose' condition. Also, it only seems to allow the user to input 4 guesses and not 10 before going straight to the 'lose' condition. I need to get these issues sorted out before I can move on to showing how many guesses are right etc.

    Anyways here's the code I have so far, any help is very much appreciated.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
     srand(time (NULL));
     int pick[4];
     int guess[4];
     int tries=0;
     int loop;
     int rp=0;
     int wp=0;
     int flag[4];
     
     
     for(int i=0; i<10; i++)
     {
         pick[i] = rand() %10;
     }
    
     
    
     printf("\t***Hello Mr. or Mrs.(or Ms. I guess) Number Guesser!***\n");
     printf("Guess four positive integers, you have ten tries to complete the code.\n");
     printf("Enter negative one (-1) four times if you want to see the code, you naughty cheater you.\n");
     printf("If you guess correctly, there will be cake!\n");        
     printf("What is your first guess?\n\n");
     
     
     scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);
     tries++;
     
     if(guess<0)
        {
            printf("The answer is\t:\t%d%d%d%d", pick[0], pick[1], pick[2], pick[3]);
            return 0;
        }
     
    
        if(pick == guess)
            {
            printf("You win! However, about that whole cake thing, it was a lie. Sorry.");
            }
     
        else if(pick != guess)
            {
            printf("\nThe code was : %d%d%d%d\nYou lose, dummy!\n\n",pick[0], pick[1], pick[2], pick[3]);
            }
     
     
    
       
          
     return 0;
    }

  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    Check your if statements, you are comparing an array instead of a single integer

    ex.)
    Code:
     if(guess<0)//should be guess[0] or whatever index you choose
    Same goes for 
    if(pick == guess)// you need to compare pick[index] with guess[index]
    Last edited by camel-man; 02-11-2013 at 06:58 PM.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Posts
    2
    Ok I've gotten everything up and running other that the functions that check and report to the user if any guesses are correct and whether they are in the correct positions. That's what my variables wp and rp are going to be for. Any tips on how to get something like that running?

    Code:
    
    
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int main()
    {
     srand(time (NULL));
     int pick[4];
     int guess[4];
     int loop;
     int rp=0;
     int wp=0;
     int flag[4];
     
     
            for(int i=0; i<4; i++) // selects a random set of 4 integers
                    {
                    pick[i] = rand() %9;
                    }
    
     
    
     printf("\t***Hello Mr. or Mrs.(or Ms. I guess) Number Guesser!***\n");
     printf("Guess four positive integers, you have ten tries to complete the code.\n");
     printf("Enter negative one (-1) as your first entry if you want to see the code, you naughty cheater you.\n");
     printf("If you guess correctly, there will be cake!\n");        
     
     for(loop=0;loop<10;loop++)
     {
     printf("\nWhat is your guess?\n\n");
     printf("You have %d correct numbers in the correct place\n\n", rp);
     printf("You have %d correct numbers in the wrong place\n\n", wp);
     
     
     scanf("%d %d %d %d", &guess[0], &guess[1], &guess[2], &guess[3]);
     
     
     
     if(guess[0]<0)
            {
            printf("\nThe answer is\t:\t%d%d%d%d\n", pick[0], pick[1], pick[2], pick[3]);
            }
     
    
                    else if(pick[0] == guess[0] && pick[1] == guess[1] && pick[2] == guess[2] && pick[3] == guess[3])
                    {
                    printf("You win! However, about that whole cake thing, it was a lie. Sorry.");
                    break;
                    }
     
                            else if(pick[0] != guess[0] && pick[1] != guess[1] && pick[2] != guess[2] && pick[3] != guess[3] && loop==9)
                            {
                            printf("\nThe code was : %d %d %d %d\nYou lose, dummy!\n\n",pick[0], pick[1], pick[2], pick[3]);
                            }
     
            
     }
            
             return 0;
    }

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Have you worked out how to do this on paper yet?
    If you haven't then put down the keyboard, step away from the mouse.

    Imagine you have
    Code:
    int secret[4] = { 1, 2, 3, 4 };
    int guess[4] = { 5, 6, 7, 8 };
    rightGuess = func1(secret,guess);
    wrongGuess = func2( secret, guess);
    Write out the steps in Pseudocode (words, pictures, whatever you feel like) so you get the answers 0 and 4 respectively.

    Then try your approach with
    1, 2, 3, 4
    1, 2, 3, 4

    Then get creative, with say
    1, 2, 3, 4
    4, 3, 2, 1

    When you've got something which seems to pan out on paper, then you can start translating it into proper code you can compile and run.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 11-27-2012, 02:11 PM
  2. Replies: 17
    Last Post: 10-20-2011, 06:32 PM
  3. Replies: 7
    Last Post: 10-19-2011, 08:45 AM
  4. finding positions of characters in a 2d array
    By synhyborex in forum C Programming
    Replies: 2
    Last Post: 02-13-2011, 03:37 PM
  5. counting array positions
    By Cpro in forum C++ Programming
    Replies: 4
    Last Post: 02-04-2008, 11:25 PM