Problem involving 3 digit numbers

  • Thread starter Thread starter issacnewton
  • Start date Start date
  • Tags Tags
    Numbers
AI Thread Summary
The discussion revolves around creating a number-guessing game where users guess a randomly generated three-digit number between 100 and 999. The program awards prizes based on the accuracy of the user's guess: an exact match yields the highest prize, while matching all digits earns a lower prize, and so on, down to no prize for incorrect guesses. The user, facing challenges with the logic for counting correct digits, seeks advice on implementing this using an if-else structure. A suggested solution involves storing the digits in an array and using nested loops to count matches, ensuring that once a digit is matched, it is marked to prevent double counting. This approach clarifies how to effectively manage the logic for varying levels of correctness in the guesses.
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
 
Technology 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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top