Hi I am trying to write a program that it will going to display random numbers from 1 to 10000 and it going to display 1000 numbers and then the user need to enter a number and the program needs to search for the nearest number that the user entered. Till now this is the program that I had done. Can someone help me!!!
#include<stdio.h>
[code]#include<stdlib.h>
#include<time.h>
#include<math.h>
[code]int main()
{
int loop;
int a[1000];
int num;
int b = 0, search;
srand(time(0)); // to get a random number other wise the number remains the saim
printf("Enter number :");
scanf_s("%d", &b);
for (loop = 0; loop < 10; loop++)
{
a[50] = rand() % 99 + 1; // 1-100 = +1 or else 0-99
printf("%d\n\n", a[50]);
}
for (b = 0; b > a; b++)
{
if (a[1000] == b)
{
printf("%d is present at location");
}
}
else()
}
Click it, and under the Format: section there is an icon <>. Use that on all of your code, if that doesnt work for some reason, do it manually. Put your code between these - [code] code goes here [/code,] (remove the semicolon after the second "code").
As for your code, tell us what you need help with. What is your next step? What have you tried?
a[50] is just a single item in the array, which is modified repeatedly by the loop. By the way rand() % 99 will give a number in the range 0 to 98.
Line 24:
if (a[1000] == b)
This is an error. Again it accesses the same element repeatedly. But because array subscripts start from 0, not 1, a[1000] is the 1001st element, it is outside the array.