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;
}
You should compare the value of array (array[middle]
) with randomValue
, because if the array is not from 1
to 20
as you did (for example, int array [20] = {0, 3, 4, 10, 15, ...}
), your program will be never correct.
The code for while loop
(the explication in the comment of code):
while (high >= low) {
int middle = (low + high) / 2; // update middle in each iteration of while loop
if (array[middle] < randomValue) {
printf("The number %d is lower than the random number\n",array[middle]);
low = middle+1; // If randomValue greater than value at middle position, we can ignore left half
}
if (array[middle] > randomValue) {
printf("The number %d is lower than the random number\n", array[middle]);
high = middle - 1; // If randomValue smaller than value at middle position, we can ignore right half
}
if (array[middle] == randomValue) {
printf("The number %d is the correct random number", array[middle]);
break; // exit while loop if you find out the number.
}
}
The output when randomValue = 13
:
The random number to find is 13
The number 10 is lower than the random number
The number 15 is lower than the random number
The number 12 is lower than the random number
The number 13 is the correct random number
The complete 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 = 19;
printf("The random number to find is %d\n", randomValue);
while (high >= low) {
int middle = (low + high) / 2;
if (array[middle] < randomValue) {
printf("The number %d is lower than the random number\n",array[middle]);
low = middle+1;
}
if (array[middle] > randomValue) {
printf("The number %d is lower than the random number\n", array[middle]);
high = middle - 1;
}
if (array[middle] == randomValue) {
printf("The number %d is the correct random number", array[middle]);
break;
}
}
return 0;
}