0

I have to open a txt file that have a 100,000 number so first I thought that the problem is my RAM but after checking on other labtop it's not my RAM can anyone know what is the problem also the problem shows when it's reach this function (void readfile(long int array1[])) that is have to read the file and start counting the number with array.

#include<iostream>
#include<ctime>
#include<cstdlib>
#include<fstream>
using namespace std;
unsigned long int my_rand(unsigned long int Min, unsigned long int Max)
{
    static int first = 0;
    if (first == 0)
    {
        srand(time(NULL)); //initialize generator of random number
        first = 1;
    }
    return ((unsigned long int)(rand() * (Max + 1 - Min) / RAND_MAX + Min));
}
void selectionsort1(long int array1[], long int n);
void selectionsort2(long int array1[], long int n);
double execution_stepin(long int array1[], long int n, int x);
void readfile(long int array1[]);
int main()
{
    ofstream ofile;
    ofile.open("SortedArray.txt");
    long int i;
    const long int n = 10000;// array size
    for (i = 0; i < n; i++)
    {
        ofile << my_rand(0, 999999) << "\n";
    }
    ofile.close();

    long int array1[n];
    double total_time = 0;
    cout << "starting selection sort with random array values version1" << endl;
    cout << "---------------------------------------------------------" << endl;
    readfile(array1);
    total_time += execution_stepin(array1, n, 0);
    readfile(array1);
    total_time += execution_stepin(array1, n, 1);
    readfile(array1);
    total_time += execution_stepin(array1, n, 2);
    cout << "The average execution time is " << total_time / 3.0 << endl;
    cout << "                              ---------------" << endl;
    total_time = 0;

    cout << "starting selection sort with random array values version2" << endl;
    cout << "----------------------------------------------------------" << endl;

    readfile(array1);
    total_time += execution_stepin(array1, n, 3);
    readfile(array1);
    total_time += execution_stepin(array1, n, 4);
    readfile(array1);
    total_time += execution_stepin(array1, n, 5);
    cout << "The average execution time is " << total_time / 3.0 << endl;
    cout << "                              ---------------" << endl;
    ofstream ofile2;
    ofile2.open("selection_ascending version1.txt");
    for (i = 0; i < n; i++)
    {
        ofile2 << array1[i] << "\n";
    }
    ofile2.close();


}
void readfile(long int array1[])
{
    ifstream ifile;
    long int next;
    ifile.open("SortedArray.txt");
    long int i = 0;
    while (!ifile.eof())
    {
        ifile >> next;
        array1[i] = next;
        i++;

    }
    ifile.close();
}
double execution_stepin(long int array1[], long int n, int x)
{
    time_t time1, time2;
    clock_t start, end;
    double dif_sec;

    time(&time1);
    start = clock();
    if (x < 3)
        selectionsort1(array1, n);
    else
        selectionsort2(array1, n);
    time(&time2);
    end = clock();
    dif_sec = difftime(time2, time1);
    cout << " Execution no " << (x % 3) + 1 << endl;
    cout << " -------------------" << endl;
    cout << "\nIt took  " << dif_sec << " seconds to sort using selection sort" << endl;
    cout << "\ntime_t: start time " << time1 << " " << " end time " << time2 << " " << "Difference " << time2 - time1 << endl;
    cout << "\nclock_t:  Execution time " << start << " " << end << " " << "time in seconds " << double(end - start) / CLOCKS_PER_SEC << endl;
    return (double(end - start) / CLOCKS_PER_SEC);
}

void selectionsort1(long int array2[], long int n)
{
    long int i, j, temp, min_index;
    for (i = 0; i < n - 1; i++)
    {
        min_index = i;
        for (j = i + 1; j < n; j++)
        {
            if (array2[j] < array2[min_index])
            {
                min_index = j;
            }
        }
        temp = array2[i];
        array2[i] = array2[min_index];
        array2[min_index] = temp;
    }
}
void selectionsort2(long int array2[], long int n)
{
    long int i, j, temp, min_index, max_index;
    for (i = 0; i < n - 1; i++)
    {
        min_index = i;
        max_index = n - (i + 1);
        for (j = i + 1; j < n / 2; j++)
        {
            if (array2[j] < array2[min_index])
            {
                min_index = j;
            }
            else if (array2[j] > array2[max_index])
            {
                max_index = j;
            }
        }
        temp = array2[i];
        array2[i] = array2[min_index];
        array2[min_index] = temp;
        temp = array2[n - (i + 1)];
        array2[n - (i + 1)] = array2[max_index];
        array2[max_index] = temp;
    }
}
Ted Lyngmo
  • 37,764
  • 5
  • 23
  • 50
alkadri24
  • 1
  • 2
  • You say that the text file contains 100,000 numbers, but in your code you allocate space for only 10,000. Is this a typo? – Dillon Apr 21 '21 at 06:06
  • I was trying to play with it, it's will not work with any of the number. – alkadri24 Apr 21 '21 at 06:08
  • I imagine you're reading `n+1` values from the file, see https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons – Alan Birtles Apr 21 '21 at 06:23
  • 1
    If you find the duplicate hard to read: It's because of the loop in `readfile`. Replace that loop with `while(ifile >> next) { array1[i++] = next; }` or `while(ifile >> array1[i]) ++i;`. The complete function could be simplified into `void readfile(long int array1[]) { ifstream ifile("SortedArray.txt"); long int i = 0; while(ifile >> array1[i]) ++i; }` – Ted Lyngmo Apr 21 '21 at 06:28
  • @TedLyngmo actually it's worked thank you so much <3. – alkadri24 Apr 21 '21 at 06:44
  • @alkadri24 Great! You're welcome. I hope you'll never use `while (!ifile.eof())` again after this :-) – Ted Lyngmo Apr 21 '21 at 06:45

0 Answers0