I created this code but for some reason when i run it, there are no errors but it keeps printing out "
Enter 4 digit between 0 and 9 with spaces in between:" after i enter my 4 digits. if someone can help me out with that, that would be great. also instead of printing the actual information everytime i add a new guess for the value of correct_num, everytime i try a guess, it just adds one to it everytime instead of actually giving out information related to the number generated by the computer. need it asap thank you soo much
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
/* first you have to let the computer generate a random number.
Then it has to declare how many tries the user has for the game
loop.
we then need the player to enter their guess. After every guess we
have to give an output of how many numbers they have in the right
location
and how many they have the right number. The player will keep
guessing until their 10 tries are over. Then we ask them if they
want
to play again*/
int main()
{
int ori_code[4]; //the variable used for the random code generated
by the computer
int user_code[4]; // the variable used for the code created by the
user
int i, j;
int correct_loc = 0; // the variable used for correct location with
the correct number
int correct_num = 0; // the variable used for correct number with
the wrong location
for (i=0; i<4; i++)
ori_code[i] = rand()%10;
for (i=0; i<10; i++)
{
printf("\nEnter 4 digit between 0 and 9 with spaces in between:
");
for(i=0; i<4; i++)
{
scanf("%d",&user_code[i]);
}
for(i=0; i<4; i++)
{
printf("%d",user_code[i]);
}
// Calculating correct location and correct number{
for(i=0; i<4; i++)
{
for(j=0; j<4; j++) {
if(i==j) {
if(user_code[i] == ori_code[j]) {
correct_loc++;
}
}
else {
if(user_code[i] == ori_code[j]) {
correct_num++;
}
}
}
}
}
// Displaying the result
printf("\n\nCorrect location: %d", correct_loc);
printf("\nCorrect number: %d", correct_num);
// Displaying the original code
printf("\nOriginal code: ");
for(i=0; i<4; i++) {
printf("%d ",ori_code[i]);
}
return 0;
}
Program
#include<stdio.h>
#include <stdlib.h>
#include <time.h>
/* first you have to let the computer generate a random
number.
Then it has to declare how many tries the user has for the game
loop.
we then need the player to enter their guess.
After every guess we have to give an output of how many numbers
they have in the right location
and how many they have the right number.
The player will keep guessing until their 10 tries are over. Then
we ask them if they want
to play again*/
int main()
{
int random;
int ori_code[4]; //the variable used for the random code generated
by the computer
int user_code[4]; // the variable used for the code created by the
user
int i, j,k;
int correct_loc = 0; // the variable used for correct location with
the correct number
int correct_num = 0; // the variable used for correct number with
the wrong location
srand(time(0));
for (i=0; i<4; i++)
{
random=rand()%10;
ori_code[i] = random;
}
for (k=0; k<10; k++)
{
printf("\nGuess No %d: ",(k+1));
printf("Enter 4 digit between 0 and 9 with spaces in between:
");
for(i=0; i<4; i++)
{
scanf("%d",&user_code[i]);
}
for(i=0; i<4; i++)
{
printf("%d",user_code[i]);
}
j=0;
// Calculating correct location and correct number{
for(i=0; i<4; i++)
{
if(user_code[i] == ori_code[i]) {
j++;
}
}
if(j==4)
correct_num++;
correct_loc+=j;
}
// Displaying the result
printf("\n\nCorrect location: %d", correct_loc);
printf("\nCorrect number: %d", correct_num);
// Displaying the original code
printf("\nOriginal code: ");
for(i=0; i<4; i++) {
printf("%d ",ori_code[i]);
}
return 0;
}
Output
Guess No 1: Enter 4 digit between 0 and 9 with spaces in between: 1
8 0 1
1801
Guess No 2: Enter 4 digit between 0 and 9 with spaces in between: 1
2 3 4
1234
Guess No 3: Enter 4 digit between 0 and 9 with spaces in between: 6
8 9 0
6890
Guess No 4: Enter 4 digit between 0 and 9 with spaces in between: 4
3 2 1
4321
Guess No 5: Enter 4 digit between 0 and 9 with spaces in between: 5
6 7 8
5678
Guess No 6: Enter 4 digit between 0 and 9 with spaces in between: 9
8 7 6
9876
Guess No 7: Enter 4 digit between 0 and 9 with spaces in between: 1
8 0 1
1801
Guess No 8: Enter 4 digit between 0 and 9 with spaces in between: 1
2 3 4
1234
Guess No 9: Enter 4 digit between 0 and 9 with spaces in between: 9
8 0 2
9802
Guess No 10: Enter 4 digit between 0 and 9 with spaces in between:
1 4 5 6
1456
Correct location: 16
Correct number: 2
Original code: 1 8 0 1
Get Answers For Free
Most questions answered within 1 hours.