Home > OS >  Reading digits into an array and sorting it
Reading digits into an array and sorting it

Time:04-07

The task is as follows: to fill the file with numbers using the generation() function - an array cannot be used here. Then read the digits into the array by the read_file() function into the array and sort it using the quick sort method. Output to the console.

I wrote the code as I logically understood it myself, but a set of values is output to the console after sorting, as it is output in case of an error with a lack of memory, random numbers with cons sometimes. Maybe it's worth filling in a dynamic array? And why, if I know a specific number of elements?

Please help me figure it out.

#include <stdio.h>
#include <stdlib.h>
#define N 10
#define A -25000
#define B 25000

void generation();
void read_file();
void method_simple_sort();
int main(void) {
    generation();
    method_simple_sort();
    return EXIT_SUCCESS;
}

void generation() {
    FILE *file;
    int i;
    file = fopen("index.txt", "w");
    srand(time(0));
    for (int i = 0; i < N; i  ) {
        fprintf(file, "%d\n", A   rand() % (A - B   1));
    }
    fclose(file);
}

void read_file() {
    FILE *file;
    int i, a[N] = { 0 };
    file = fopen("index.txt", "r");
    for (i = 0; i < N; i  ) {
        fscanf(file, "%d ", &a[i]);
    }
    fclose(file);
    for (int i = 0; i < N; i  )
        printf("%d ", a[i]);
}

void method_simple_sort() {
    int a[N], i = 0, k = 0, ind_max = 0, temp = 0;
    read_file();
    for (k = 0; k < N - 1; k  ) {
        ind_max = k;
        for (i = 1   k; i < N; i  ) {
            if (a[i] > a[ind_max]) {
                ind_max = i;
            }
        }
        temp = a[k];
        a[k] = a[ind_max];
        a[ind_max] = temp;
    }
    // вывод результатов в файл
    printf("\n\nПростого выбора: ");
    for (int i = 0; i < N; i  ) {
        printf("%d ", a[i]);
    }
    printf("\n\n\n\n");
}

CodePudding user response:

The function method_simple_sort defines a local array variable int a[N]. It then calls read_file(), but read_file() does not fill this local array variable defined in method_simple_sort. Instead it fill its own local array variable (called also int a[N], but it's a different one than the one in method_simple_sort).

The bottom line is that when method_simple_sort attempts to sort a it sorts an array containing uninitialized data.

Hence the "garbaged" values that you see printed at the end.

CodePudding user response:

The array used by the methods read_file and method_simple_sort aren't the same. When you declare anything inside a function it is translated at low level in space to allocate on the stack when you call the function.

You should transform read_file(void) into read_file(int *a) without declaring another instance of an array inside read_file. By doing that you can pass a reference to the array declared inside method_simple_sort.

In particular:

void read_file(int *a) {
    FILE *file;
    int i;
    /* a[N] = { 0 };*/
    ...
}

void method_simple_sort() {
    int a[N], i = 0, k = 0, ind_max = 0, temp = 0;
    read_file(a);
    ...
}
  • Related