-
Notifications
You must be signed in to change notification settings - Fork 0
/
RandomNum_and_Insert.c
46 lines (37 loc) · 1.04 KB
/
RandomNum_and_Insert.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
//A program to generate random numbers and store/insert in an empty array
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void display(int arr[], int n);
void numInsert(int arr[], int size, int index, int num);
int main(){
int arr[100];
// int size = sizeof(arr)/sizeof(int);
int i, num;
srand(time(0));
printf ("Program to get the random number from 1 to 100\n");
for (i = 0; i < 20; i++){
num = rand() % 20+1; // use rand() function to get the random number
printf("%d ", num);
numInsert(arr, 20, i, num);
}
printf("\nElements of the arrays after insertion\n");
display(arr, 20);
return 0;
}
// Display function to print arrays
void display(int arr[], int n){
//Code for Traversal
for (int i = 0; i < n; i++){
printf("%d ", arr[i]);
}
printf("\n");
}
// Insertion function
void numInsert(int arr[], int size, int index, int num){
for(int i = size-1; i>=index; i--){
arr[i+1] = arr[i];
}
arr[index] = num;
// return 1;
}