0

I am trying to sort three files (I/O) using bubble sort. For this purpose, I write this code

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;

void bubble_Sort_ascending_order(ll n, ll v[]){
    ll i,j;
    for(i=1; i<=n-1; i++)
    {
        for(j=1; j<=n-i; j++)
        {
            if(v[j]>v[j+1])
                swap(v[j],v[j+1]);
        }
    }
}

int bubble_Sort_ascending_input1() {

    freopen("input1.txt","r",stdin);
    freopen("output_File1.txt","w",stdout);

    ll n;
    cin >> n;
    ll t[n];
    for(ll i=1; i<=n; i++)
    {
        cin >> t[i];
    }

    bubble_Sort_ascending_order(n,t);

    for(ll k=1; k<=n; k++)
    {
        cout << t[k] << endl;
    }
}

int bubble_Sort_ascending_input2() {

    freopen("input2.txt","r",stdin);
    freopen("output_File2.txt","w",stdout);

    ll n;
    cin >> n;
    ll r[n];
    for(ll i=1; i<=n; i++)
    {
        cin >> r[i];
    }

    bubble_Sort_ascending_order(n,r);

    for(ll k=1; k<=n; k++)
    {
        cout << r[k] << endl;
    }
}

int bubble_Sort_ascending_input3() {

    freopen("input3.txt","r",stdin);
    freopen("output_File3.txt","w",stdout);

    ll n;
    cin >> n;
    ll v[n];
    for(ll i=1; i<=n; i++)
    {
        cin >> v[i];
    }

    bubble_Sort_ascending_order(n,v);

    for(ll k=1; k<=n; k++)
    {
        cout <<v[k]<<endl;
    }
}


int main(){
    bulble_Sort_ascending_input1();
    bubble_Sort_ascending_input2();    
    bubble_Sort_ascending_input3();
}

First input file function output is working properly. Second and third input function gives garbage value in output file two and three.

But I can't seem to locate any solutions to a similar problem. I'm really having trouble with understanding file I/O for some reason. Thanks in advance for the help.

AP11
  • 617
  • 4
  • 11
  • 3
    Please format your code so it is readable and also see [Why should I not #include ?](https://stackoverflow.com/q/31816095/3422102) – David C. Rankin Dec 10 '20 at 08:14
  • Why are you using `freopen` rather than `std::fstream`? – Alan Birtles Dec 10 '20 at 08:50
  • Read [this C++ reference](https://en.cppreference.com/w/cpp), then a [good C++ programming book](https://www.stroustrup.com/programming.html). Consider using [std::sort](https://en.cppreference.com/w/cpp/algorithm/sort) and [standard C++ containers](https://en.cppreference.com/w/cpp/container). Take inspiration from existing opensource C++ projects like [fish](https://fishshell.com/), [Qt](https://qt.io/), [RefPerSys](http://refpersys.org/). Read wikipage on [quicksort](https://en.wikipedia.org/wiki/Quicksort) – Basile Starynkevitch Dec 10 '20 at 11:48
  • Read also the documentation of your C++ compiler (perhaps [GCC](http://gcc.gnu.org/) ....) and debugger (maybe [GDB](https://www.gnu.org/software/gdb/)...) – Basile Starynkevitch Dec 10 '20 at 11:52

1 Answers1

1

This might not answer your question, but I see some flaws in your bubble sort algorithm. First of all, your indexing has to start at 0, otherwise you'll never read the first element in array, and then you can speed it up a little bit by right conditions in the for loop.

And then you could write bubble_Sort_ascending_input() method as just one with parameters such as std::string input_file_name and std::string output_file_name. Then you would more easily debug the program and see, where the problems are.

And for the love of gods, don't use #include <bits/stdc++.h>.

#include <fstream>

using namespace std;

typedef long long ll;

void bubble_Sort_ascending_order(ll n, ll v[]) {
    ll i,j;
    for(i = 0; i < n-1; i++) {
        for(j = 0; j < n - i -1; j++) {
            if(v[j] > v[j+1])
                swap(v[j], v[j+1]);
        }
    }
}

void bubble_Sort_ascending(const std::string& input_file, const std::string& output_file) {

    fstream input("../"+input_file, fstream::in);
    fstream output("../"+output_file, fstream::out);
    // "../" is just for IDE, if you run it from cmd, delete the path

    ll n;
    if (input.is_open()) {
        input >> n;
        ll arr[n];
        int i = 0;
        while (input >> arr[i]) {
            ++i;
        }
        bubble_Sort_ascending_order(n, arr);
        if (output.is_open()) {
            for (ll& number : arr) {
                output << number << " ";
            }
        }
    } else {
        cout << "input not open";
    }
}

int main() {
   bubble_Sort_ascending("input1.txt", "output1.txt");
   return 0;
}
AP11
  • 617
  • 4
  • 11