I am trying to sort an array using a bubble up sort, I can't get the sorted array to print. Yes I know a bubble up sort isn't the best sort but it is what I'm most comfortable with.
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <new>
usingnamespace std;
#define Length 150
ifstream in;
int Anumber[Length];
int main()
{
string file;
cout << "Please enter the location of the file" << endl;
cout << "An example would be C:/my file.txt" << endl;
getline(cin, file);
in.open(file);
if (!in)
{
cerr << "Error file not found" << endl;
exit(1);
}
if (in.eof())
{
cerr << "The file is blank, The program will now exit" << endl;
exit(1);
}
if (in.is_open())
{
cout << "The file opended, and here is the file data" << endl;
}
int A, X;
A = Length;
for (A = 0; A < Length; A++)
{
in >> Anumber[A];
X = Anumber[A];
cout << X << endl;
}
system("pause");
cout << endl;
cout << "The numbers sorted smallest to largest are " << endl;
cout << endl;
for (A = 0; A < Length - 1; A++)
{
for (A = X + 1; X <= Length; X++)
{
int temp = -1;
if (Anumber[X] > Anumber[A])
{
temp = Anumber[A];
Anumber[A] = Anumber[X];
Anumber[X] = temp;
}
}
}
for (A = 0; A <= Length; A++)
{
cout << Anumber[A] << endl;
}
return 0;
};
You don't have to understand how std::sort works to use it - that's the beauty of generic programming. All you need to know is how to use it, and you don't need to worry about the details.