Problem involving 3 digit numbers

  • Thread starter Thread starter issacnewton
  • Start date Start date
  • Tags Tags
    Numbers
Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
2 replies · 2K views
issacnewton
Messages
1,035
Reaction score
37
Hello

I am writing a program where program will randomly generate a three digit number from 100 to 999 and then user will be asked to guess that number. If the number is matched exactly, then the highest prize is given to the user, if all digits in a guess are also in the original number, next highest prize will be given to the user. If two digits from guess are correct, then the next prize . If one digit from the guess is correct, then the next one. Finally no prize for the guess where no digits match.
I tried to use if-else construct for this. I am able to get the first two correct. But I am having problems with logic where guess might have two of its digits correct and where guess might have only single digit correct. Any specific ideas ?

thanks
IssacNewton
 
Physics news on Phys.org
Store the digits in an array. Loop through the array and count the matches.
nCount=0;
for(nR=0;nR<3;nR++) naTemp[nR] = naRandom[nR]
for(nG=0;nG<3;nG++) {
nGD = naGD[nG];
for(nR=0;nR<3;nR++) {
if(nDG==naTemp[nR]) {
nCount++;
naTemp[nR] = -1;
break;
}
}
}
 
thanks Scott... makes sense