Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir committed Dec 1, 2024
1 parent 6f87706 commit eefd17f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
Binary file modified assignment
Binary file not shown.
46 changes: 27 additions & 19 deletions assignment.c
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <string.h>

int is_positive_integer(const char *str) {
if (!str || *str == '\0') return 0;
while (*str) {
if (*str < '0' || *str > '9') {
if (str == NULL || *str == '\0') {
return 0;
}
for (int i = 0; str[i] != '\0'; i++) {
if (str[i] < '0' || str[i] > '9') {
return 0;
}
str++;
}
return 1;
}
Expand All @@ -28,22 +29,24 @@ int main(int argc, char *argv[]) {
int rows = atoi(argv[1]);
int cols = atoi(argv[2]);

if (rows != 5 || cols != 10) { // Ensure the dimensions match the test expectations
if (rows != 5 || cols != 10) {
printf("Incorrect usage. Expected dimensions are 5 rows and 10 columns\n");
return 1;
}

int **matrix = malloc(rows * sizeof(int *));
if (!matrix) {
perror("Failed to allocate memory for rows");
int **matrix = (int **)malloc(rows * sizeof(int *));
if (matrix == NULL) {
printf("Failed to allocate memory for rows\n");
return 1;
}

for (int i = 0; i < rows; i++) {
matrix[i] = malloc(cols * sizeof(int));
if (!matrix[i]) {
perror("Failed to allocate memory for columns");
for (int j = 0; j < i; j++) free(matrix[j]);
matrix[i] = (int *)malloc(cols * sizeof(int));
if (matrix[i] == NULL) {
printf("Failed to allocate memory for columns\n");
for (int j = 0; j < i; j++) {
free(matrix[j]);
}
free(matrix);
return 1;
}
Expand All @@ -57,27 +60,32 @@ int main(int argc, char *argv[]) {
}

FILE *file = fopen("matrix.txt", "w");
if (!file) {
perror("Failed to create matrix.txt");
for (int i = 0; i < rows; i++) free(matrix[i]);
if (file == NULL) {
printf("Failed to create matrix.txt\n");
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);
return 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, " ");
if (j < cols - 1) {
fprintf(file, " ");
}
}
fprintf(file, "\n");
}

fclose(file);

for (int i = 0; i < rows; i++) free(matrix[i]);
for (int i = 0; i < rows; i++) {
free(matrix[i]);
}
free(matrix);

printf("Matrix successfully written to matrix.txt\n");
return 0;
}
// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
10 changes: 5 additions & 5 deletions matrix.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
66 58 48 5 46 56 25 28 69 63
56 100 85 38 32 38 69 8 92 99
76 82 51 78 25 60 48 95 84 56
78 49 14 25 5 11 80 81 90 49
43 98 100 80 35 83 69 4 90 60
25 70 17 59 30 56 77 98 75 44
85 1 91 22 50 70 75 9 89 83
82 64 73 42 13 42 22 19 41 37
93 65 6 61 24 35 68 52 84 42
95 68 42 37 41 44 7 15 52 95

0 comments on commit eefd17f

Please sign in to comment.