Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Computer selects a random number \'s\' between [1000, 9999] User tries to guess

ID: 650542 • Letter: C

Question

Computer selects a random number 's' between [1000, 9999]

User tries to guess it by entering 'g'

Computer tells how many digits are in place, out of place, not in secret number

For example, if 's' is 6234

- User enters 'g' as 7436, then computer says

-1 digit is in place

-2 digits are out of place

-1 digit is not in secret number

User keeps trying until he finds the secret number.

I have to use rand(), but Professor told us that we can't use array([])

so i don't know what to do!

It is a C language!

Please help me :x

i use the function

srand(time(0));

int rand_range(int a, int b)

{

return rand() % (b - a + 1) + ;

}

Explanation / Answer

#include <time.h>
#include <stdlib.h>
#include <stdio.h>

int init(int a,int b){
   while (a > 0){
       if (a % 10 == b)
           return 1;
       a = a/10;
   }
   return 0;
}

void display(int n){
while (1){
int guess;
printf("Guess the Secret Number between 1000 and 9999 : ");
    scanf("%d",&guess);
guess = 7436;
if (guess == n){
   printf("%s","Well done u have guess the secret number");
   break;
}
else{
   int temp1 = n;
   int temp2 = guess;
   int inplace = 0;
   int outplace = 0;
   int not_in = 0;
   while (temp1 > 0 && temp2 > 0){
       if (init(n,temp2%10) == 1){
           if (temp1 % 10 != temp2 %10)
               outplace++;
           else
               inplace++;
       }
       else{
           not_in += 1;
       }
       temp1 = temp1/10;
       temp2 = temp2/10;
   }
   printf("%s %d %s","-",inplace," digit is in place");
   printf("%s %d %s","-",outplace," digit is in place");
   printf("%s %d %s","-",not_in," digit not in secret number");
}
}
}

int main(){
srand(time(NULL));
int number = rand() % 9000 + 1000;
display(number);
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote