-
Notifications
You must be signed in to change notification settings - Fork 0
/
assignment.c
91 lines (79 loc) · 2.44 KB
/
assignment.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
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
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
// Function to check if a string represents a positive integer.
int is_int_pos(int rows, int cols) {
if (rows <= 0 || cols <= 0) return 0;
return 1;
}
// Function to create a dynamically allocated matrix
int** create_matrix(int rows, int cols) {
int** matrix = (int**)malloc(rows * sizeof(int*));
if (matrix == NULL) {
printf("Memory allocation failed for matrix rows.\n");
exit(1);
}
for (int i = 0; i < rows; i++) {
matrix[i] = (int*)malloc(cols * sizeof(int));
if (matrix[i] == NULL) {
printf("Memory allocation failed for matrix columns.\n");
exit(1);
}
for (int j = 0; j < cols; j++) {
matrix[i][j] = rand() % 100 + 1; // Random number between 1 and 100
}
}
return matrix;
}
// Function to write the matrix to a file
void write_matrix_to_file(int** matrix, int rows, int cols) {
FILE *file = fopen("matrix.txt", "w");
if (file == NULL) {
printf("Error: Could not open file for writing.\n");
exit(1);
}
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
fprintf(file, "%d", matrix[i][j]);
if (j < cols - 1) {
fprintf(file, " ");
}
}
fprintf(file, "\n");
}
fclose(file);
}
// Function to free the dynamically allocated matrix
void free_matrix(int** matrix, int rows) {
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
}
int main(int argc, char *argv[]) {
// Check argument count
if (argc != 3) {
printf("Incorrect usage. You provided %d arguments. The correct number of arguments is 2\n", argc - 1);
return 1;
}
// Parse dimensions
int rows = atoi(argv[1]);
int cols = atoi(argv[2]);
// Validate arguments
if (!is_int_pos(rows, cols)) {
printf("Incorrect usage. The parameters you provided are not positive integers\n");
return 1;
}
// Seed random number generator
srand(time(NULL));
// Create and populate the matrix
int** matrix = create_matrix(rows, cols);
// Write the matrix to a file
write_matrix_to_file(matrix, rows, cols);
// Free the allocated memory
free_matrix(matrix, rows);
printf("Matrix of size %dx%d created and written to 'matrix.txt'.\n", rows, cols);
return 0;
}