Man, I had a long-ish post typed up and then my computer randomly decided to blue-screen on me while I was proofreading it.
But anyways, here goes again....
I don't get the output you're getting.
I'd make sure that
Text.txt is in the same directory as the program (which it probably already is, considering that you didn't get the error message) and that it contains only numbers.
Other than that, some comments:
8 9 10 11 12 13 14 15
|
class bubble
{
public:
unsigned* arr;
int n;
//Constructor
bubble(const int& size) : n(size) { this->arr = new unsigned[n]; }
|
Since you're being asked to print out only the positive numbers, then presumably, there will be some negative numbers in the input file, so
arr should be
int*
, not
unsigned*
(
unsigned
doesn't "stop" negative numbers from being input; it just turns them into (very big) positive numbers).
In reality, it would probably be best if you used
std::vector instead of a raw array, since then you won't have to worry about
delete[]
ing the memory afterwards (which, by the way, you haven't done in this code) or specify the size beforehand (
std::vector can automatically expand as necessary).
For types as small as
int
(and, in general, for any fundamental type), there's no need to pass by
const
reference -- you won't get any speed benefit by doing that.
Your bubble sort currently sorts in ascending order.
To fix that, flip your comparison around. (I'll let you figure that one out yourself.)
Your
display member function should exit the loop when it runs into a non-positive number.