-
Notifications
You must be signed in to change notification settings - Fork 1
/
random.c
61 lines (49 loc) · 1.34 KB
/
random.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
/*
Name: Yunika Upadhayaya
Student ID: 1001631183
Generated random coordinates:
-> Any number and range of random coordinates can be generated.
-> Generated coordinates are then written onto the file.
*/
#include <stdio.h>
#include <stdlib.h>
//Funtion to generate random coordinates and write onto the file
void generate_random_file(int size,int max_range);
int main (int argc, char *argv[])
{
int size = atoi(argv[1]);
int max_range = atoi(argv[2]);
//Checks if total number of arguments is 3 or not
if (argc < 3 || argc > 3)
{
printf("Error: Please provide an integer number of a data size followed by maximum range of the data as arguments.\n");
printf("Example: ./a.out 10 100\n");
exit(EXIT_FAILURE);
}
else
{
generate_random_file(size,max_range);
printf("Writing to the file successful!\n");
}
return 0;
}
void generate_random_file(int size, int max_range)
{
int i,x,y = 0;
FILE *fp;
fp = fopen("random.txt","w");
if(fp == NULL)
{
printf("Error\n");
exit(EXIT_FAILURE);
}
fprintf(fp, "%d\n",size);
//Generate random coordinates
for(i = 0; i < size; i++)
{
x = rand() % max_range;
y = rand() % max_range;
fprintf(fp,"%d %d\n",x,y);
}
fclose(fp);
}