Originally Posted by
Newklear
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.