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

I need a function that will have the randomly generate 4 digits for the user to

ID: 3547821 • Letter: I

Question

I need a function that will have the randomly generate 4 digits for the user to guess. The digits can nly be from 1-4, so 1234, 4233, and so on.                 

                    .The user will have 20 tries to guess the 4 digits.                 

                    .The guesses need to be saved in an array.                 

                    .Also, a switch needs to be applied to allow the user to decide if he will guess the digits or provide the digits.                 

                    .Here is the code for the user to provide the digits.                 

                    .#include"stdafx.h"                 

                    .                 

                    .#include"stdio.h"                 

                    .                 

                    .#include<stdlib.h>                 

                    .                 

                    .int valid(char);                 

                    .                 

                    .int main(void)                 

                    .                 

                    .{char c;                 

                    .                 

.int test, i, guess=0, j=0, k=0;                 

                    .                 

.char numbers[4];                 

                    .                 

.char computerNumber[4];                 

                    .                 

.for(i=0; i<4; i++)                 

                    .                 

                    .{                 

                    .                 

.do{                 

                    .                 

                    .printf(" Enter a number between 1 and 4: ");                 

                    .                 

                    .numbers[i]=getchar();                 

                    .                 

        .test=valid(numbers[i]);}                 

                    .                 

.while(test);}                 

                    .                 

.for(j=1;j<=20;j++)                 

                    .                 

                    .{printf("The computer guess is: ");                 

                    .                 

.for(i=0; i<4;                    i++){computerNumber[i] = rand()%4+49;                 

                    .                 

                    .printf("%c", computerNumber[i]);}                 

                    .                 

                    .printf(" ");                 

                    .                 

                    .guess = 0;                 

                    .                 

.for(k = 1; k<5; k++)                 

                    .                 

.if(computerNumber[k] == numbers[k])                 

                    .                 

                    .guess++;                 

                    .                 

.if(guess==4)                 

                    .                 

                    .{                 

                    .                 

                    .printf("The computer guessed correctly! ");                 

                    .                 

                    .j = 21;                 

                    .                 

                    .}                 

                    .                 

.else                 

                    .                 

                    .printf("The computer did not guess correctly ");                 

                    .                 

                    .}                 

                    .}                 

                    .                 

                    .int                 

                    .                 

.valid(char c)                 

                    .                 

                    .{if ((c>='1') && (c<='4'))                 

                    .                 

                    .{return 0;                 

                    .                 

                    .}                 

                    .                 

.else                 

                    .                 

                    .{                 

                    .                 

.return 1;                 

                    .                 

                    .}                 

                    .                 

                    .}

Explanation / Answer

#include<stdio.h>

#include<stdlib.h>

int valid(char);

int main(void)

{

char c;

int test, i, guess=0, j=0, k=0;

char numbers[4];

char computerNumber[4];

int key=0;

printf("If you want to Provide press 0 otherwise for Guess press 1 ");

scanf("%d",&key);

key=key%2;


if(key==1)

{

goto x;

}




for(i=0; i<4; i++)

{

do{

printf("Enter a number between 1 and 4: ");

scanf("%c",&numbers[i]);

}

while(valid(numbers[i]));

}

for(j=1;j<=20;j++)

{

printf("The Computer guess is: ");

for(i=0; i<4; i++){computerNumber[i] = rand()%4+49;

printf("%c", computerNumber[i]);}

printf(" ");

guess = 0;

for(k = 1; k<5; k++)

if(computerNumber[k] == numbers[k])

guess++;

if(guess==4)

{

printf("The Computer guessed correctly! ");

j = 21;

}

else

printf("The Computer did not guess correctly ");

}


x:

for(i=0; i<4; i++)

{

numbers[i] = rand()%4+49;

}



for(j=1;j<=20;j++)

{

printf(" The User guess is:");

scanf("%s",computerNumber);

printf("The User guessed:%c%c%c%c ",computerNumber[0],computerNumber[1],computerNumber[2],computerNumber[3]);

guess = 0;

for(k = 0; k<4; k++)

{

if(computerNumber[k] == numbers[k])

guess++;

}

if(guess==4)

{

printf(" The User guessed correctly! ");

j = 21;

}

else

printf("The User did not guess correctly ");

}


printf(" The Computer guessed:%c%c%c%c ",numbers[0],numbers[1],numbers[2],numbers[3]);

}



int valid(char c)

{

if ((c>='1') && (c<='4'))

{

return 0;

}

else

{

return 1;

}

}

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