please help me correct this...


i'm trying to write a program that reads numbers from a text file( 21 12 44 21 -5 63 0 ) to an array and bubble sort them in an descending order, and printing out only positive numbers. i have been trying for a while but what is displayed is not what i expect.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <conio.h>

using namespace std;
class bubble
{
public:
	unsigned* arr;
	int n;

	//Constructor
	bubble(const int& size) : n(size) { this->arr = new unsigned[n]; }

	//function to read from file
	void inputvf(ifstream &f)
	{
		//check if file is open

		if (f.fail()) {
			cout << "\n Error in openning file!!!";
			exit(1);
		}
		for (int i = 0; i < n; i++)
			f >> arr[i];
		//close file
		f.close();
	}


	//Bubble sort function
	void bubblesort()
	{
		for (int i = 1; i<n; i++)//for n-1 passes
		{
	
			for (int j = 0; j<n - 1; j++)
			{
				if (arr[j] > arr[j + 1])
				{
					int temp;
					temp = arr[j];
					arr[j] = arr[j + 1];
					arr[j + 1] = temp;
				}
			}
		}
	}
	void display()
	{
		cout << endl;
		cout << "----------------------\n";
		cout << "Sorted array elements \n";
		cout << "----------------------\n";
		for (int j = 0; j<n; j++)
			cout << arr[j] << endl;
	}
};


int _tmain(int argc, _TCHAR* argv[])
{
	bubble list(7);
	ifstream file("Text.txt");
	list.inputvf(file);
	list.bubblesort();
	list.display();

	_getch();
	return 0;
}


the result:


3452816845
3452816845
3452816845
3452816845
3452816845
3452816845
3452816845


what am i doing wrong?

the contents of Text.txt are:
 21 12 44 21 -5 63 0
Last edited on
Conio.h is a non-standard header and using it is deprecated:
https://en.wikipedia.org/wiki/Conio.h

Can you post the contents of Text.txt?

Also, you should never explicitly call .close() on file streams - you should let the destructor do that for you. Your inputvf member function should take a std::istream and not a std::ifstream so that it is not limited to only files.
Last edited on
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.
Topic archived. No new replies allowed.