diff --git a/lab19/lab19/data.txt b/lab19/lab19/data.txt new file mode 100644 index 0000000..6244a1a --- /dev/null +++ b/lab19/lab19/data.txt @@ -0,0 +1,16 @@ +15 +1 +2 +3 +4 +5 +6 +7 +8 +9 +10 +11 +12 +13 +14 +15 \ No newline at end of file diff --git a/lab19/lab19/main.c b/lab19/lab19/main.c index 3cb3038..07bd5fa 100644 --- a/lab19/lab19/main.c +++ b/lab19/lab19/main.c @@ -1,21 +1,41 @@ #include #include -void load(int arr[]) { - FILE* file = fopen("\\data.txt", "r"); +void printarr(int arr[], int len) { + printf("["); + for (int i = 0; i < len; i++) { + printf("%d, ", arr[i]); + } + printf("\b\b]\n"); +} + +int load(int** parr) { + FILE* file = fopen("data.txt", "r"); if (file == NULL) { puts("Ôàéë íå íàéäåí"); - return; + exit(EXIT_FAILURE); } - int len; + int len = 1; fscanf_s(file, "%d", &len); - for (int i = 0; i < len; i++) { - fscanf_s(file, "%d", &arr[i]); + + *parr = (int*)malloc(sizeof(int) * len); + if (*parr == NULL) { + printf_s("Not enough memory to load data\n"); + exit(EXIT_FAILURE); } + + for (int i = 0; i < len; i++) { + fscanf_s(file, "%d", (*parr + i)); + } + + return len; } int main() { - + int* parr; + int len; + len = load(&parr); + printarr(parr, len); return 0; } \ No newline at end of file