Question

I created this code but for some reason when i run it, there are no errors...

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;
}

Homework Answers

Answer #1

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

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Convert the following c code programs to c++ code #include <stdio.h> //check whether the number is...
Convert the following c code programs to c++ code #include <stdio.h> //check whether the number is prime or not int main() {    int n, i, flag = 0;           printf("Enter a positive integer: "); #include <stdio.h>        //check whether the number is prime or not            int main()            {            int n, i, flag = 0;                              printf("Enter a positive integer: ");...
I need to add two trivial functions to the following code. I am having an issue...
I need to add two trivial functions to the following code. I am having an issue with what i may need to change in the current code to meet the requirements. I am already displaying the bank record but now using a function Here are some examples of what i can add to the code 1. Obtain opening (current) balance. 2. Obtain number the number of deposits. 3. Obtain number of withdrawals. 4. Obtain deposit amounts. 5. Obtain withdrawal amounts....
I'm trying to find a code to check the occurrences of the word or string I...
I'm trying to find a code to check the occurrences of the word or string I wrote my own code but it's not working can you please help and fix my code #include <stdio.h> #include <string.h> #define MAX_SIZE 100 // Maximum string size int main() { char str[MAX_SIZE]; char tosearch[MAX_SIZE]; printf("Enter any string: "); gets(str); printf("Enter word to search occurrences: "); gets(tosearch); int cursor = 0; int i = 0; int stringLen = 0; int searchLen = 0; int count1;...
Expand the code below to display both the value of the loop variable and this value...
Expand the code below to display both the value of the loop variable and this value squared. Put the pairs of values on a new line. #include <stdio.h> #include <stdlib.h> int main(void) { int i; int a; int b; do { printf("Please enter the lower value: "); scanf("%d", &a); printf("Please enter the upper value: "); scanf("%d", &b); if (a>b) printf("The upper value must be greater than the lower value\n\n"); } while (a>b); for(i=a; i<=b; i++) printf("%d ", i); return 0;...
How can i make this lunix code print 3 numbers in reverse it must be in...
How can i make this lunix code print 3 numbers in reverse it must be in printStars format and no loops Here is the code i have but can figure out how to reverse the numbers #include<stdio.h> void printStars(int n) { if (n>0){ printf(""); printStars(n-1); } } int main() { int n; int b; int c; printf("Enter 3 numbers to reverse "); scanf("%d",&n,&b,&c); printf("your reversed numbers are %d",n); printStars(n); return 0;
Question 3. In the following code, answer these questions: Analyze the code and how it works?...
Question 3. In the following code, answer these questions: Analyze the code and how it works? How can we know if this code has been overwritten? Justify how? #include <stdlib.h> #include <unistd.h> #include <stdio.h> int main(int argc, char **argv) { int changed = 0; char buff[8]; while (changed == 0){ gets(buff); if (changed !=0){ break;} else{     printf("Enter again: ");     continue; } }      printf("the 'changed' variable is modified\n %d", changed); }
Run the below code and shift binary left and right and understand the output. #include <stdio.h>...
Run the below code and shift binary left and right and understand the output. #include <stdio.h> int main() {     int num=212, i;     for (i=0; i<=2; i++)         printf("Right shift by %d: %d\n", i, num>>i);      printf("\n");      for (i=0; i<=2; i++)         printf("Left shift by %d: %d\n", i, num<<i);             return 0; } Output:
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8...
Error compiler. need fix code for febonacci Iterative like 0 1 1 2 3 5 8 13 21 34 55 ....... and also need add code complexity time if you want! here code.c --------------------------------------------------------------------------------------------------------------------- #include <stdio.h> #include <math.h> int fibonacciIterative( int n ) { int fib[ n + 1 ]; int i; fib[ 0 ] = 0; fib[ 1 ] = 1; for ( i = 2; i < n; i++ ) { fib[ i ] = fib[ i -...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is...
What is wrong with my recursive Binary Search code? For some reason my 'middle' value is always zero? Please fix and explain! #include <stdio.h> #include <stdlib.h> #include <stdbool.h> int BinarySearch(const int A[],const int L,const int R,const int key) {              if (R >= 1){           int middle = L + (R-1)/2;                              if (A[middle] == key)        return A[middle];               if (A[middle] > key){...
Analyze the worst case running time of the following code in “big-Oh” notation in terms of...
Analyze the worst case running time of the following code in “big-Oh” notation in terms of the variable n. a. void fdisp1(int n) { for(int i=0; i < n; i++) { for(int j=0; j < n; j++) { for(int k=0; k < n; k++) { for(int m=0; m < n; m++) { System.out.println("Hi I am Here"); } } } } } b. voidprintAllPossibleOrderedPairs(intarr[], int size) { for (int i = 0; i < size; i++) { for (int j =...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT