Can someone correct and complete the code below? i'm unable to do that...
I want first a random number generated from an array with the values 1 to 20.
The program has to find what is the random number by guessing at different stages the middle number of the array and eliminate half of the remaining numbers after every loop.
Let’s say the random number is 13
As the array is between 1 and 20 , the first guess number is 10 as this is the number in the middle of the array.
As the guess number 10 is lower than the random number 13, the next test is then 15 ( which corresponds to ( 20 +10 ) /2).
As the guess number 15 is higher than the random number 13, the next test is then 12 ( which corresponds to ( 15 +10 ) /2).
As the guess number 12 is lower than the random number 13, the next test is then 13 ( which corresponds to ( 12+15 ) /2).
The guess number now match the random number
There is my code
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
int randomIndex = rand() % 20;
int randomValue = array[randomIndex];
int low = 0;
int high = 20;
int middle = (low + high) / 2;
printf("The random number to find is %d\n", randomValue);
while (middle <= randomValue) {
if (middle < randomValue) {
printf("The number %d is lower than the random number\n", middle);
}
if (middle > randomValue) {
printf("The number %d is lower than the random number\n", middle);
}
if (middle == randomValue) {
printf("The number %d is the correct random number", middle);
}
}
return 0;
}
and there is the output expected
Output expected ( for 13 as the random number):
The number 10 is lower than the random number
The number 15 is higher than the random number
The number 12 is lower than the random number
The number 13 is the correct random number
I struggled for hours trying to do that.
Any help would be highly appreciated.Thank you in advance.
EDITED : What should be the value of the variables "low" and "high" in the loop for each statement ?
##include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
srand(time(NULL));
int array [20] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16,17,18,19,20};
int randomIndex = rand() % 19;
int randomValue = array[randomIndex];
int low = 0;
int high = 19;
int middle = (low + high) / 2;
printf("The random number to fine is %d\n", randomValue);
while (middle <= randomValue) {
if (middle < randomValue) {
printf("The number %d is lower than the random number\n", middle);
low = ;
high = ;
middle = (low + high) / 2;
}
if (middle > randomValue) {
printf("The number %d is lower than the random number\n", middle);
low = ;
high = ;
middle = (low + high) / 2;
}
if (middle == randomValue) {
printf("The number %d is the correct random number", middle);
}
}
return 0;
}
middlewithin the loop, and updatelowandhighaccording to whether you're going left or right.high = 20;vshigh = 19;similarly. In the loop, calculatemiddle = (low + high) / 2;, and when you make a directional choice, adjustloworhigh.whileis a loop, but the condition is wrong and also the value of the condition never changes inside the loop body, so the loop is either infinite or never executed.breaksomewhere in the loop, otherwise it's an infinite loop.