From eefd17f79a94e7090194e13d887419df7dc40ed6 Mon Sep 17 00:00:00 2001 From: Krasimir Date: Sun, 1 Dec 2024 18:26:33 +0100 Subject: [PATCH] done --- assignment | Bin 16536 -> 16464 bytes assignment.c | 46 +++++++++++++++++++++++++++------------------- matrix.txt | 10 +++++----- 3 files changed, 32 insertions(+), 24 deletions(-) diff --git a/assignment b/assignment index 8dec10377c9a8e90e693a44b78bafe41718596c6..0cdaacc98db01a36e2d0eb6c41ec16ddb28c1ead 100644 GIT binary patch delta 2844 zcmZ`*Yitx%6uz?$cel2?JMAvBtF3)dXbZHv!y|3!Zs}HxgDEd9)F_V?4NyTS+7gLC zDN92)%O)*Hq$U{4AE*%(D4Jr3tdxp~gcglfK^{K{gC!{P2oG^QcV=!=n|L=f=brC; z=iGD8y>sujH*tNNxK8v`PRRHwQ@wQFt5 z&+A?~vRByoRY^Q~$B)*;=y^+En=#+bV=$H!Ax&y%9!?tbxHPzF6yTD{!*xKm$;T~K z^*N}|r9JKlq5|hp`(xC13Gikg>_^5pRX?RNo~M0N4Mv7GF%3R6Ti_G(Wr#oaWTOd( zHsDWd95Ir&u&bAf)O%}fgqX+%9Xr(a9+u8XqPpJj1hpH;yP7*;wbWOI%yDam<$CoP z?$mKB6j)Y>4&9rId($+9t`IE@Yj5kg1py9zvj}kBlAO{q3^hZFRe)q`a!UI!)B-_7 z1BiAGLzCbQL{}o(Jq%5T4-nmj=(odAJDf(e-&fq#>dvricjj0seS|1urbB`A)Ffrh zKShKrb!1K_>cM2)GMbU6nSKJG*`ma)YQgvvYT?E({rs-bHcGDjsYE+o6w&e&BvE~ z_a7Q!J({DYiz?+)nq#DglOhkz4Zn(t+=GwTuGS|a;xGE`Fb4joK_lnP$kSH z(yM*ZZ=~9@nazVxo#txvVl)#*%WpU>MS87OrFR;RNbxC)agsRK6?Cpm=voyoe4Rou zb%-7{mrIx;M&E*hkgC`QGBdsm$JO*u%<&c7)m72orRYU5F(Y!qBtJ`&h+ddkdbNJq z(JZvej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d8Juf)!fKx_qF3tU$fj#?*ZFF;*835e`IE~>&a>x@B%@fh z?;#dpReP0r=C$=`FO}5FjHQx(_9a@GU7BR?gc7*zG!3EY5CSeA94xEa zK8MbF`9qC_{QIlgBKc`V)Qt566PxDayHTHJk;D5uaq^DkZBgYuOdx;_bKYi_4e_^`%ftw#STSVa3W z)YN*`=g`g}Pc(K5v14@^{o6RGq8!5iM>toW!cS{&mqPpCiCV9(3ej2o*W*YxN>Ut3 z+J03yV9i*5Q}Bc|I#Qcj;T4x3zwC^Ao^cbm=onmS^3efUX!;3_L6`p*^g8^}|4zAt z7y6WJdKk+^bq##C+2h@a8XG;tU#%vGkX9IJ_VnJ#N5vC}(N0t-sU+@@iLlep+5!Ai zavKrXy#Zne_-C_-j0pTWR46H5dTj`N4JsBg2fLbO$2o&jMsjdOSsj@)EDsScW8rbh zP)bREjX+GPp`fIKh%AcHTgE%l1$4cKy*;si4ixNF1KAB-sCP2m`w%`{i6j$IFA?F0 z!Au*RXOQD2SjmtWHRa1j*ahcpc}9DhV$IaW>0#{)Ww?KWpD)v(S5tzKiwVI5K6) zyHsbBN61k&YW*FKR>O!?N>4*ii^uyOiz_+5`g-8>0eHTp)$u^r{s)`xJ9uE<=wDje zB>o{XDz#3~(Og?6rEi1M?w5!72b_?z(1Yjz`yRk1jG1$MQb?ID3@xy_&VHnw>7oFl8 cVkIJ_aCU2>Lu)aH$t9TE+DgwtLumDX0Ewp96#xJL diff --git a/assignment.c b/assignment.c index 3f16c69..0810ba3 100644 --- a/assignment.c +++ b/assignment.c @@ -1,15 +1,16 @@ #include #include -#include #include +#include 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; } @@ -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; } @@ -57,9 +60,11 @@ 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; } @@ -67,17 +72,20 @@ int main(int argc, char *argv[]) { 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 \ No newline at end of file diff --git a/matrix.txt b/matrix.txt index 7a71aac..b4c08c7 100644 --- a/matrix.txt +++ b/matrix.txt @@ -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