Заполнение массива номером из файла [C ++] - PullRequest
0 голосов
/ 19 апреля 2019

Я хочу отсортировать числа с помощью сортировки по основанию.Все работает как надо, когда я объявляю массив и заполняю его.Я хочу иметь возможность заполнить массив числами из текстового файла, отсортировать их и вывести в другой текстовый файл.На данный момент у меня проблемы с заполнением их в массив и я не знаю, что я делаю неправильно.

//max value of an array
int maxValue(int arr[], int n) {
    int max = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > max) {
            max = arr[i];
        }
    }
    return max;
}
//counting sort
void countSort(int arr[], int n, int exp) {
    int output[n];
    int i;
    int count[10] = { 0 };

    // Store count of occurrences in count[]
    for (i = 0; i < n; i++) {
        count[(arr[i] / exp) % 10]++;
    }

    // Change count[i] so that count[i] now contains actual
    //  position of this digit in output[]
    for (i = 1; i < 10; i++) {
        count[i] = count[i] + count[i - 1];
    }

    //Output array
    for (i = n - 1; i >= 0; i--) {
        output[count[(arr[i] / exp) % 10] - 1] = arr[i];
        count[(arr[i] / exp) % 10]--;
    }

    //copy output to array
    for (i = 0; i < n; i++) {
        arr[i] = output[i];
    }
}
void radixsort(int arr[], int n) {
    int max = maxValue(arr, n);

    for (int exp = 1; max / exp > 0; exp = exp * 10) {
        countSort(arr, n, exp);
    }
}
int main() {
    /*
     int arr[] = { 247, 321, 515, 227, 642, 413, 109, 248, 759, 15, 930, 2 };
     int n = sizeof(arr) / sizeof(arr[0]);
     radixsort(arr, n);

     for (int i = 0; i < n; i++) {
     cout << arr[i] << " ";
     }
     */

    /*  Filling array with 10 numbers
     int size = 10;
     int age[size];
     for (int i = 1; i <= size; i++) {
     age[i] = i;
     cout << age[i] << " ";
     }
     */

    const int size = 255; //the number of the integers in the file
    int array[size];
    int temp, i;
    ifstream File;
    File.open("dn1.txt");
    //while (!File.eof()) {
    while (File >> temp) {
        File >> array[i];
        i++;
    }



    for (int i = 0; i < size; i++) {
        cout << array[i] << endl; //" ";
    }

    File.close();

    /*
    if (File.is_open()) {
        // print file:
        char c = File.get();
        while (File.good()) {
            std::cout << c;
            c = File.get();
        }
    } else {
        // show message:
        std::cout << "Error opening file";
    }
    */

    /*
    ofstream myfile;
    //myfile.open("Radix_sort/src/out.txt");
    myfile.open("out.txt");
    myfile << "Writing this to a file.\n";
    myfile << "Writing this to a file.\n";
    myfile.close();
    */


    return 0;
}

Когда я пытаюсь вывести данные в консолиЯ ничего не получаю.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...