Thread: Rookie looking for help!

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Mr.777, your code will look, and fit into the forum's software limits, much better, if you can make the following change:

    In your options >> editor, have it replace tabs with 2-5 spaces, instead. Tabs are for text, not code, in the forum software.

    That will make your code a lot easier to read/study, and understand.

  2. #17
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Quote Originally Posted by Mr.777 View Post
    That's why i put this code after..... Did you really try this one????
    Yes, i tried it... Did you? :P

    Quote Originally Posted by Salem View Post
    I'm dismayed by the continuing dog's breakfast of poor code indentation in this thread...
    Mine was nicely indented though, wasn't it?

  3. #18
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I am not indenting anyways... Sorry. Next time i'll definitely indent it... Sorry again....

    I am not sure, where exactly you are replacing this code in your original source code. If you can come up with your original source code, that will be much appreciated...
    *Waiting*
    Last edited by Mr.777; 03-11-2011 at 03:10 AM.

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    @Newklear:

    I didn't understand the help[] array in your code snippet. Your code looks very close to what I put up in pseudo code, however.

    That pseudo code was taken directly from a mastermind program I wrote so I could be sure it was accurate. Testing was brief (very brief), but it was accurate for the input I tested it on.

    So what does help[] do, and why are you using it? If you use the logic I posted and it fails, tell me what your input was that it failed on and I'll fix it in my program (and in the logic).

    In any case, post up your most recent code snippet, and let's see what's up with it.

  5. #20
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Quote Originally Posted by Adak View Post
    You have to record one of three states, for each number. Your logic should be something like this.
    I tried following that, and got this:

    Code:
        for(i=0; i<ARR_SIZE; i++)
        {
            if( guess[i] == secretNumber[i] )
            {
                help[i] = 'R';
    
            }
    
            for (j=0; j < ARR_SIZE ; j++)
            {
                if (guess[j] == secretNumber[i] && j != i)
                {
                    help[i] = 'S';
                }
            }
    
            if (help[i] != 'R' && help[i] != 'S')
            {
                help[i] = '_';
            }
        }
    It didnt't work though, but im not sure i got it all right... :S

    When i tried it and got the secretNumber array 4 6 5 3, and made the
    guess array 4 4 4 4 , it returned S _ _ _, when it should have returned S S S S...

    Also when the secretNumber array was 1 3 6 5 and guess array was 1 2 3 4
    it returned R S _ _ when it should have returned R _ S _

    Quote Originally Posted by Adak View Post
    @Newklear:

    I didn't understand the help[] array in your code snippet. Your code looks very close to what I put up in pseudo code, however.
    the help[] array should contain the characters R, S or _ depending on what you guessed... it should be the same as the state[] that you used in your pseudo code i think

    I'll paste the current code unto pastebin again... line 80 to 102

    The code

    This is making me crazy, btw >.<
    Last edited by Newklear; 03-11-2011 at 03:53 AM.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by Newklear View Post
    I tried following that, and got this:

    When i tried it and got the secretNumber array 4 6 5 3, and made the
    guess array 4 4 4 4 , it returned S _ _ _, when it should have returned S S S S...
    Well, with Number[]={4,6,5,3}, and guess = 4,4,4,4, the answer would have to include at least one R.

    The way I did mine, if a number was guessed correctly, it did not get compared to other non-guessed numbers. So my programs returns: R___ in this case.

    Let me refine my program code, so the correct answer would be RSSS in this case, right?

    It's a great exercise in logic though. Good on your teacher or book for challenging you with it.

    Back in a bit.

    It's late and I'm bushed, but this is what I have:
    Code:
    #include <stdio.h>
    #define MAX 4
    
    int main(void) {
    
      int i, j, guess[MAX]={0}, numRight=0, n[MAX]={1,3,6,7};
      char state[MAX+1]={""}; 
      while(numRight < MAX) {
        
        printf("\n\nTo be guessed: %d. Guessed correctly: %d\n\n", (MAX-numRight),numRight);
       
        printf("\n What's your guess? ");
        for(i=0;i<MAX;i++)
          scanf("%d", &guess[i]); 
        (void) getchar();
        numRight = 0;
        for(i=0;i<MAX;i++) {
          if(guess[i]==n[i]) {
            state[i]='R';    
            numRight++;
          }      
          else {
            for(j=0;j<MAX;j++) {
              if(guess[i]==n[j] && j != i) 
                state[i]='S';
            }
          }         
          if(state[i] != 'R' && state[i] != 'S')
            state[i]='_';
        }
        printf("\n %s", state);    
      }
      printf("\n\n\t\t\t    press enter when ready");
      (void) getchar();
      return 0;
    }
    Try that kind of logic out. Any changes will have to be done tomorrow as I'm off to bed. You have to break out with Control+C or Control+break keys, if you want to quit before solving all the numbers. Sorry about that.
    Last edited by Adak; 03-11-2011 at 04:45 AM.

  7. #22
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    I tried following that, and got this:

    When i tried it and got the secretNumber array 4 6 5 3, and made the
    guess array 4 4 4 4 , it returned S _ _ _, when it should have returned S S S S...
    Infact it should return RSSS.
    The reason is, after making the first decision of R, it comes inside the loop (with variable j) and executes itself....
    So instead of doing guess[j]==secretNumber[i] i'll recommend you to do guess[i]==secretNumber[j] and also inside the first if from line number 84 to 88, you should set a flag that will not allow the program to enter in the loop(with variable j) if program has been entered in the if.....
    Last edited by Mr.777; 03-11-2011 at 04:19 AM.

  8. #23
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Underlines _ for wrong guesses, R for right guesses, and S for "number is in the string, but not in the right place in the string".

  9. #24
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Exactly that what his/her program will generate in case of inputs 4653 in secretNumber and 4444 in guess......
    Look up the code and try to edit it as i mentioned some of the things in my post...

  10. #25
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Quote Originally Posted by Adak View Post
    the answer would have to include at least one R.

    The way I did mine, if a number was guessed correctly, it did not get compared to other non-guessed numbers. So my programs returns: R___ in this case.

    Let me refine my program code, so the correct answer would be RSSS in this case, right?
    Ahh yes, sorry... But it should return R S S S , since the first 4 is correct and should give you an 'R', but the next three 4's are wrong, but still exist in the secretNumber array and should give you three 'S'...

    Quote Originally Posted by Adak View Post
    It's a great exercise in logic though. Good on your teacher or book for challenging you with it.
    Yeah, i'm sure its good, but it feels like i dont have all the pieces to lay the puzzle yet...

  11. #26
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The program I just posted gives RSSS for your 4444 input, btw. Appears correct all around, but not tested thoroughly.

    Give it a try and see if it's good. I would, but it's too late for me. You do have to set the number array, first.

  12. #27
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Code:
    int flag=0;
    for(i=0; i<ARR_SIZE; i++)
        {
            if( guess[i] == secretNumber[i] )
            {
                help[i] = 'R';
                flag=1;
     
            }
     
            for (j=0; j < ARR_SIZE ; j++)
            {
                if (guess[i] == secretNumber[j] && j != i && flag==0)
                {
                    help[i] = 'S';
                    break; \\ Will stop iterations of loop un-necessary.
                }
            }
     
            if (help[i] != 'R' && help[i] != 'S')
            {
                help[i] = '_';
            }
            flag=0;
        }
     
        printf("help: ", help[i]);
        for(i=0; i<ARR_SIZE; i++)
        {
            printf("%c ", help[i]);
        }
    Try this........

    EDIT: Ahhh...... A little late to the party :-)
    Anyways, if your problem has been solved, ignore this one, else use.
    Simple :-)

  13. #28
    Registered User
    Join Date
    Mar 2011
    Posts
    21
    Ahh, lots of thanks to both of you, it seems to be working now.

    Gonna go over it a couple more times to see what it was i missed and try to learn from it now!

  14. #29
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Good luck...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Variable/pointer scope (rookie Qs)
    By twinbee in forum C Programming
    Replies: 3
    Last Post: 05-20-2010, 03:33 PM
  2. an uber-simple array/function question for a c rookie!
    By variable83 in forum C Programming
    Replies: 13
    Last Post: 12-06-2009, 01:24 PM
  3. Help a rookie
    By elrookie in forum C Programming
    Replies: 14
    Last Post: 06-14-2007, 10:28 AM
  4. Rookie pointer problem: 2 dimensions, malloc
    By GatesAntichrist in forum C Programming
    Replies: 11
    Last Post: 12-20-2005, 12:35 PM
  5. my code.. remember i am a rookie dont laugh
    By aubrey in forum C Programming
    Replies: 2
    Last Post: 11-24-2002, 10:08 PM