Problem that i cannot see

There is a bug in the following program of insertion sort, value of 'n' changes during the process of swapping 2 a[] elements in insertion(). I cannot understand why,...
Please help.

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
#include <iostream>
#include <cstdlib>
using namespace std;

class Aray{

	public:
	int a[], n;

	Aray(int n){

		srand(time(0));
		this->n = n;

		for(int i = 0; i < n; i++){
			a[i] = rand()%n + 1;
		}

		cout << "unsorted Array is: " << endl;
		for (int i = 0; i < n; i++) cout << a[i] << "\t";
	}

	~Aray(){}

	void insertion(){
		for(int i = 0; i < n; i++){
			int min = i;

			for(int j = i; j < n; j++){
				if(a[min] > a[j]) min = j;
			}

				int temp = a[i];
				a[i] = a[min];
				a[min] = temp;

		}
	}

	void display(){
		for(int i = 0; i < n; i++){
			cout << a[i] << "\t";
		}
	}

	void tp(){cout << "number of elements: " << n << endl;}

};

int main(){

		int n;

		cout << "How many numbers do you want to sort?" << endl;
		cin >> n;

		Aray *b = new Aray(n);
		b->n = n;

		b->insertion();
		b->tp();

		cout << "\nThe sorted Array is: " << endl;
		b->display();


		return 0;
	}


Please refer to my comments.
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
74
75
#include <iostream>
#include <cstdlib>
#include <time.h>

using namespace std;

class Aray{
	public:
		//int a[] incomplete type is not allowed
		//I suspect that you have to specify the size of the array in class
		//Dynamic variable works fine, though, as I write below
	int n;
	int *a ;
	Aray(int n){
		a = new int[n];
		srand(time(0));
		this->n = n;
		for(int i = 0; i < n; i++)
		{
			a[i] = rand()%n + 1;
		}
		cout << "unsorted Array is: " << endl;
		for (int i = 0; i < n; i++) 
			cout << a[i] << "\t";
}
//Do you need this destructor that does nothing?
	~Aray(){}

	void insertion()
	{
		for(int i = 0; i < n; i++)
		{
			int min = i;
			for(int j = i; j < n; j++)
			{
				if(a[min] > a[j]) 
					min = j;
			}
			int temp = a[i];
			a[i] = a[min];
			a[min] = temp;
		}
	}
	void display()
	{
		for(int i = 0; i < n; i++)
		{
			cout << a[i] << "\t";
		}
	}

	void tp()
	{
		cout << "number of elements: " << n << endl;
	}
};

int main()
{

	int n;

	cout << "How many numbers do you want to sort?" << endl;
	cin >> n;

	Aray *b = new Aray(n);
	//Why do you need this now that you have initialized the Aray with n?
	b->n = n;
	b->insertion();
	b->tp();
	cout << "\nThe sorted Array is: " << endl;
	b->display();

	return 0;
}
I just realised everything worked fine, when i put a[20]. You are right, i cannot put a[]...

Thank you very much for your prompt and perfect reply!
No problem! By the way, please mark this as solved. Thanks!
And remember to delete the array in destructor.
Last edited on
Topic archived. No new replies allowed.