Skip to content

Commit

Permalink
test
Browse files Browse the repository at this point in the history
  • Loading branch information
Krasimir committed Dec 1, 2024
1 parent 0daf908 commit e653c49
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions assignment.c
Original file line number Diff line number Diff line change
@@ -1,26 +1,11 @@
#include <stdio.h> // For input and output operations
#include <stdlib.h> // For memory allocation and other utilities
#include <time.h> // For seeding the random number generator with current time
#include <time.h> // For random number generation

int main(int argc, char *argv[]) {
// Seed the random number generator with the current time
srand(time(NULL));

// Validate the number of arguments provided
if (argc == 1) {
// No arguments provided
printf("Incorrect usage. You provided 0 arguments. The correct number of arguments is 2");
fflush(stdout); // Ensure the output is flushed
return 1;
} else if (argc == 2) {
// Only one argument provided
printf("Incorrect usage. You provided 1 arguments. The correct number of arguments is 2");
fflush(stdout); // Ensure the output is flushed
return 1;
} else if (argc > 3) {
// More than two arguments provided
printf("Incorrect usage. You provided %d arguments. The correct number of arguments is 2", argc - 1);
fflush(stdout); // Ensure the output is flushed
if (argc != 3) {
printf("Incorrect usage. You provided %d arguments. The correct number of arguments is 2\n", argc - 1);
return 1;
}

Expand All @@ -29,11 +14,13 @@ int main(int argc, char *argv[]) {
int cols = atoi(argv[2]);

if (rows <= 0 || cols <= 0) {
printf("Incorrect usage. The parameters you provided are not positive integers");
fflush(stdout); // Ensure the output is flushed
printf("Incorrect usage. The parameters you provided are not positive integers\n");
return 1;
}

// Seed the random number generator with the current time
srand(time(NULL));

// Dynamically allocate memory for the matrix
int **matrix = malloc(rows * sizeof(int *));
if (matrix == NULL) {
Expand Down Expand Up @@ -83,6 +70,7 @@ int main(int argc, char *argv[]) {
fprintf(file, "\n"); // Newline between rows
}
}

fclose(file);

// Free allocated memory
Expand All @@ -93,6 +81,5 @@ int main(int argc, char *argv[]) {

// Confirm successful matrix generation
printf("Matrix of size %d x %d was successfully written to 'matrix.txt'.\n", rows, cols);
fflush(stdout); // Ensure the output is flushed
return 0;
}

0 comments on commit e653c49

Please sign in to comment.