bubble sort via pointer

I'm trying to do a basic bubble sort using dynamic memory allocation and pointers within a class. I have tested the getRain() function, it returns 0.26 and 1.1. After I call my sort function and display again it is the same as my unsorted display. Thank you for your consideration.

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
#include <iostream>
#include <iomanip>
#include "rainfall.h"
using namespace std;

void fill(Rainfall *p);
void sort(Rainfall *p);

int main()
{	Rainfall *p = new Rainfall[10];
	fill(p);
	cout << "Rainfall:   Date:\n";
	for(int i = 0; i < 10; i++)
	{	(p + i) ->displayRainfall();
		cout << setw(8) << " ";
		(p + i) ->displayDate();
		cout << endl;
	}

	cout << "\nSort in descending order? (Y/N) ";
	char choice;
	cin >> choice;
	if(choice == 'y' || choice == 'Y')
	{	sort(p);  //********
		cout << "Rainfall:   Date:\n";
		for(int i = 0; i < 10; i++)
		{	(p + i) ->displayRainfall();
			cout << setw(8) << " ";
			(p + i) ->displayDate();
			cout << endl;
		}}
	
	delete [] p;
	p = 0;
	return 0;
}

/******************************************************************/
//setData(rainfall, month, day, year)
void fill(Rainfall *p)
{	p ->setData(0.26, 10, 11, 2011);
	(p + 1) ->setData(1.1, 10, 8, 2011);
	(p + 2) ->setData(0, 10, 7, 2011);
	(p + 3) ->setData(0.61, 10, 6, 2011);
	(p + 4) ->setData(0, 10, 5, 2011);
	(p + 5) ->setData(0.22, 10, 4, 2011);
	(p + 6) ->setData(1.11, 10, 1, 2011);
	(p + 7) ->setData(0, 9, 30, 2011);
	(p + 8) ->setData(0.58, 9, 29, 2011);
	(p + 9) ->setData(0.89, 9, 28, 2011);
}

/******************************************************************/
void sort(Rainfall *p)
{	int num = (10 - 1);
	for(int r = 0; r < num; num--)
		{for(int i = 0; i < num; i++)
			{if(p->getRain() > (p + 1)->getRain())
				{	Rainfall temp = *p;
					*p = *(p + 1);
					*(p + 1) = temp;
				}}}}
Last edited on
[code] "Please use code tags" [/code]
1
2
3
4
5
6
//this two are equivalent
(p+i)->something();
p[i].something();

//if(p->getRain() > (p + 1)->getRain())
if( p[0].getRain() > p[1].getRain() )


When passing arrays to function, use one of the followings:
_A terminate value (that doesn't appear anywhere on the array). By instance '\0' with cstrings.
_Pass the dimension as an extra parameter
_Pass two pointers in range [) (close-open)
One of the stipulations of the assignment was to leave out any array subscripts.
Last edited on
leave out any array subscripts

Oh for the love of...
That is rather stupid in my opinion, but whatever.

Anyway, the problem is in your algorithm. Your comparison only compares p[0] and p[1] ever. I assume you wanted to use p[r] and p[i] in there somewhere?
The problem remains with:
1
2
3
4
5
if((p + i)->getRain() > (p + i + 1)->getRain())
{	Rainfall temp = *p;
	*p = *(p + 1);
	*(p + 1) = temp;
}
Last edited on
1
2
3
4
5
//	Rainfall temp = *p;
//	*p = *(p + 1);
//	*(p + 1) = temp;

swap( p[0], p[1] );
Last edited on
closed account (D80DSL3A)
Make the same changes to lines 2, 3 and 4 that you made to line 1. Note: p and (p+1) are pointing to only the first 2 array elements. Your code just swaps these first 2 elements back and forth repeatedly. You should be working with *(p+i) and *(p+i+1) throughout.
Thanks:
1
2
3
4
5
if((p + i)->getRain() < (p + i + 1)->getRain())
{	Rainfall temp = *(p + i);
	*(p + i) = *(p + i + 1);
	*(p + i + 1) = temp;
}
Topic archived. No new replies allowed.