-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomarray.c
61 lines (43 loc) · 1.31 KB
/
randomarray.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int* create_array(int size)
{
int* array = (int*)calloc(size,sizeof(int));
int i;
for (i = 0; i < size; i++)
array[i] = rand() % 9901 + 100; // Generates random number in the range 100 - 1000
return array;
}
void print_array(int* array, int size)
{
int i;
printf("Array elements:\n");
for (i = 0; i < size; i++)
printf("%d ", array[i]);
printf("\n");
}
int check_integer(int* array, int size, int num)
{
int i;
for (i = 0; i < size; i++)
if (array[i] == num)
return 1; // Number found in the array
return 0; // Number not found in the array
}
int main() {
int size, *arr, num;
srand(time(0)); // Seed the random number generator with the current time
printf("Enter the size of the array: ");
scanf("%d", &size);
arr = create_array(size);
print_array(arr, size);
printf("Enter a number to check in the array: ");
scanf("%d", &num);
if (check_integer(arr, size, num))
printf("%d is present in the array.\n", num);
else
printf("%d is not present in the array.\n", num);
free(arr); // Free the dynamically allocated memory
return 0;
}