-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake-matrix.c
More file actions
99 lines (99 loc) · 2.31 KB
/
make-matrix.c
File metadata and controls
99 lines (99 loc) · 2.31 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//This file creates square matrix and each value is a 64-bit double precision floating point.
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<string.h>
void print_help()
{
printf("matrix should be square matrix (r==c)\nmake sure r and c are multiples of 2520\n");
printf("usage: ./executable -r \"rows\" -c \"columns\" -l \"lower bound\" -u \"upper bound\" -o \"file name\"\n");
}
int main(int argc, char *argv[])
{
int opt, r = 2520, c = 2520, l = 50, u = 150;
char *filename = "default-matrix-file1.dat";
while((opt = getopt(argc, argv, ":r:c:l:u:o:")) != -1)
{
switch(opt)
{
case 'r':
r = atoi(optarg);
if(r < 1)
{
printf("invalid value for -r: %d (it requires positive integer)\n", r);
print_help();
exit(1);
}
break;
case 'c':
c = atoi(optarg);
if(c < 1)
{
printf("invalid value for -c: %d (it requires positive integer)\n", c);
print_help();
exit(1);
}
break;
case 'l':
l = atoi(optarg);
if(l < 1)
{
printf("invalid value for -l: %d (it requires positive integer)\n", l);
print_help();
exit(1);
}
break;
case 'u':
u = atoi(optarg);
if(u < 1)
{
printf("invalid value for -u: %d (it requires positive integer)\n", u);
print_help();
exit(1);
}
break;
case 'o':
filename=strdup(optarg);
break;
default:
if(optopt == 'r')
r = 2520;
if(optopt == 'c')
c = 2520;
if(optopt == 'l')
l = 100;
if(optopt == 'u')
u = 100;
else if(optopt == 'o')
filename = "default-matrix-file1.dat";
else
{
print_help();
exit(1);
}
}
}
if(r!=c)
{
printf("It's not a square matrix (rows(%d) != columns(%d))\n", r, c);
print_help();
}
size_t i, size = (size_t)r*c*sizeof(double), elems = (size_t)r*c;
double *matrix = (double*)malloc(size);
for(i=0;i<elems;i++) matrix[i] = ((double)l + ((double)rand() / (u - l))) / 1000.0;
printf("r: %d, c: %d, l: %d, u: %d, o: %s\n", r, c, l, u, filename);
FILE *output_file;
output_file = fopen(filename, "wb");
if(output_file != NULL)
{
fwrite(&r, sizeof(int), 1, output_file);
fwrite(&c, sizeof(int), 1, output_file);
fwrite(matrix, sizeof(double), (size_t)r*c, output_file);
}
else
{
printf("Unbale to open file %s\n", filename);
exit(1);
}
free(matrix);
}