arrays

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!!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#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()
}
Last edited on
Please edit your post and use code tags to make it more readable - http://www.cplusplus.com/articles/jEywvCM9/

We also can't help you if you never say what you need help with. Be specific.
i can't do it :( how ??
"I cant do it" means nothing without context, please try and be specific when you need help, we can't read minds.

If you meant you can't edit your code, there is an edit button at the bottom of your post - https://gyazo.com/1feb6ba9909a45de516f95578b5927f8

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?
Last edited on
I need to know how should I continue the program.. the program is updated.
Last edited on
There are (at least) a couple of issues with your code.

Line 19:
 
    a[50] = rand() % 99 + 1;   // 1-100 = +1 or else 0-99 

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.
no but at first I am trying to display fewer numbers before doing the full program. Can someone help me to come to a conclusion because I got stuck.
Topic archived. No new replies allowed.