Order messed up - Pointers

Hello, I posted this earlier today andddd I am now left with one issue: the original sequence is still ordered. No clue how to fix it, please help if you can :)

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
76
77
78
#include <iostream>
	 using namespace std;

	 // Function prototypes
	 void arrSelectSort(double *[], int);
	 void showArray(double [], int);
	 void showArrPtr(double [], int);

	 int main()
	 {
		 int NUM_DONATIONS; 
		 cout << "How many donations?" << endl;
		 cin >> NUM_DONATIONS; 

		 
		 double * donations = new double[NUM_DONATIONS];
		 
		 cout << "Enter the donations: \n";
		 for (int x = 0; x < NUM_DONATIONS; x++)
		 {
			 cout << "Donations #" << (x + 1) << ": ";
			 cin >> donations[x];
		 }


		
		 arrSelectSort(&donations, NUM_DONATIONS);

		 
		 cout << "The donations, sorted in ascending order are: \n";
		 showArrPtr(donations, NUM_DONATIONS);

	
		 cout << "The donations, in their original order, are: \n";
		 showArray(donations, NUM_DONATIONS);
		 system("pause");
		 return 0;
	 }


	 void arrSelectSort(double arr[], int size)
	 {
		  double temp;
		  bool swap;
		  
		 	 do
		  { 
		   swap = false;
		   for (int count = 0; count < size-1; count++)
		 	  {
			   if (arr[count] > arr[count + 1])
		 		  {
				   temp = arr[count];
				   arr[count] = arr[count + 1];
				   arr[count + 1] = temp;
		 		  swap = true;
		 		  }
		 	  }
		   } while (swap);
	 }



	 void showArray(double arr[], int size)
	 {
		 for (int count = 0; count < size; count++)
			 cout << arr[count] << " ";
		 cout << endl;
	 }

	 

	 void showArrPtr(double  arr[], int size)
	 {
		 for (int count = 0; count < size; count++)
			 cout << *(arr+count) << " ";
		 cout << endl;
	 }
This can't be your real code, or you have left out some of it. The declaration of arrSelectSort does not match the definition. The type of the first parameter is different.

You have an array. You sort it. You print it, and then you print it again. Well of course it will be printed in the same order both times because it's the same array that you have not modified in between.

Note that there is no difference in the behaviour between showArray and showArrPtr. They do exactly the same thing.
Last edited on
I don't know what the heck I was on when I posted that, a reminder to all: don't try to code when you're too tired, you'll make the weirdest...mistakes...if you can even call it that.

Here's the fixed code for anyone who's curious:

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
76
77
78
79
80
81
82
83
84
#include <iostream>
using namespace std;

// Function prototypes
void arrSelectSort(int *[], int);
void showArray(int[], int);
void showArrPtr(int *[], int);

int main()
{
	const int NUM_DONATIONS = 15;  // Number of donations

	int * donations = new int[NUM_DONATIONS];


	 for (int count = 0; count < NUM_DONATIONS; count++)
		 {
		 cout << "Donation " << (count + 1) << ": ";
		 cin >> donations[count];
		 }

	// An array of pointers to int.
	int *arrPtr[NUM_DONATIONS];

	// Each element of arrPtr is a pointer to int. Make each
	// element point to an element in the donations array.
	for (int count = 0; count < NUM_DONATIONS; count++)
		arrPtr[count] = &donations[count];

	// Sort the elements of the array of pointers.
	arrSelectSort(arrPtr, NUM_DONATIONS);

	// Display the donations using the array of pointers. This
	// will display them in sorted order.
	cout << "The donations, sorted in ascending order are: \n";
	showArrPtr(arrPtr, NUM_DONATIONS);

	// Display the donations in their original order.
	cout << "The donations, in their original order are: \n";
	showArray(donations, NUM_DONATIONS);
	system("pause");
	return 0;
}


void arrSelectSort(int *array[], int size)
{
	int startScan, minIndex;
	int *minElem;

	for (startScan = 0; startScan < (size - 1); startScan++)
	{
		minIndex = startScan;
		minElem = array[startScan];
		for (int index = startScan + 1; index < size; index++)
		{
			if (*(array[index]) < *minElem)
			{
				minElem = array[index];
				minIndex = index;
			}
		}
		array[minIndex] = array[startScan];
		array[startScan] = minElem;
	}
}



void showArray(int array[], int size)
{
	for (int count = 0; count < size; count++)
		cout << array[count] << " ";
	cout << endl;
}



void showArrPtr(int *array[], int size)
{
	for (int count = 0; count < size; count++)
		cout << *(array[count]) << " ";
	cout << endl;
}
Topic archived. No new replies allowed.