Skip to content

Commit

Permalink
Finish assignment
Browse files Browse the repository at this point in the history
  • Loading branch information
GAMihov18 committed Dec 1, 2024
1 parent 1ba131f commit 9e2059b
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 2 deletions.
28 changes: 28 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"tasks": [
{
"type": "cppbuild",
"label": "C/C++: gcc build active file",
"command": "/usr/bin/gcc",
"args": [
"-fdiagnostics-color=always",
"-g",
"${file}",
"-o",
"${fileDirname}/${fileBasenameNoExtension}"
],
"options": {
"cwd": "${fileDirname}"
},
"problemMatcher": [
"$gcc"
],
"group": {
"kind": "build",
"isDefault": true
},
"detail": "Task generated by Debugger."
}
],
"version": "2.0.0"
}
Binary file added assignment
Binary file not shown.
55 changes: 53 additions & 2 deletions assignment.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,60 @@ int main(int argc, char *argv[]) {
srand(time(NULL));
int minrand = 1;
int maxrand = 100;
size_t arraySize[2];

// WRITE YOUR CODE HERE

if (argc != 3)
{
printf("Incorrect usage. You provided n arguments. The correct number of arguments is 2\n");
return 1;
}
int converted;
for (size_t i = 1; i < (size_t)argc; i++)
{
converted = atoi(argv[i]);
if (converted<=0)
{
printf("Incorrect usage. The parameters you provided are not positive integers\n");
return 1;
}
arraySize[i-1] = converted;
}

int **array = (int **)malloc(arraySize[0] * sizeof(int *));
if (array == NULL)
{
printf("Memory allocation failed\n");
return 1;
}

for (size_t i = 0; i < arraySize[0]; i++)
{
array[i] = (int *)malloc(arraySize[1] * sizeof(int));
if (array[i] == NULL)
{
printf("Memory allocation failed\n");
return 1;
}
for (size_t j = 0; j < arraySize[1]; j++)
{
array[i][j] = (rand() % (maxrand - minrand + 1)) + minrand;
}
}
FILE *file = fopen("matrix.txt", "w");
if (file == NULL) {
printf("Error: Could not open file.\n");
return 1;
}
for (size_t i = 0; i < arraySize[0]; i++)
{
for (size_t j = 0; j < arraySize[1]; j++)
{
fprintf(file, "%d ", array[i][j]);
}
fprintf(file, "\n");
free(array[i]);
}
free(array);
fclose(file);
return 0;
}
5 changes: 5 additions & 0 deletions matrix.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
67 24 92 82 28 45 64 4 37 33
47 79 39 22 81 44 32 18 27 65
38 57 38 88 84 74 62 64 16 69
80 82 44 23 16 72 19 79 75 7
63 74 85 2 95 17 97 78 86 24
Binary file added test
Binary file not shown.

0 comments on commit 9e2059b

Please sign in to comment.