Performing a Bubble Sort on a Character Array with Pointer Notation

Good evening all,

I've just developed a program to perform a Bubble Sort on an array of characters using pointers to pass the array through several functions. This is for a homework assignment for one of my programming classes. Thus far, the program runs smoothly and without any errors. The only problem is that I can't seem to be able to properly write the program in pointer notation only, or without brackets ([]) as my instructor wants me to.

Here is the code:

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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
/*
	This program performs a descending order binary search on an array of 10 characters. 
	The characters are stored in a dynamically allocated array. 

*/

#include <iostream>
#include <cctype>

using namespace std;

/* Function Prototypes:  */
void Bubble_Sort(char *[], int);
void Show_Array(char [], int);
void Show_Array_PTR(char *[], int);

int main()
{
	const int NUM_CHARS = 10;   // Number of characters.

	// An array containing the characters.
	char Letters[10];

	for (char &val : Letters)
	{
		cout << "Enter a character: ";
		cin >> val;
	}
	
	// An array of pointers to char.
	char *arrPtr[NUM_CHARS] = { nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr };

	for (int count = 0; count < NUM_CHARS; count++)
	{
		Letters[count] = toupper(Letters[count]);
	}

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

	// Display the characters in their original order.
	cout << "In original order, the characters are:\n\n";
	Show_Array(Letters, NUM_CHARS);

	// Sort the elements in the array of pointers. 
	Bubble_Sort(arrPtr, NUM_CHARS);

	// Display the characters using the array of pointers. 
	// This function call will display them in descending order. 
	cout << "\nIn descending order, the characters are:\n\n";
	Show_Array_PTR(arrPtr, NUM_CHARS);
	cout << "\n\n";

	system("pause");

	return 0;
}

/*	DEFINITION OF FUNCTION BUBBLE_SORT:
	This function uses a bubble sort algorithm
	to analyze and sort the values in the parameter
	'arr[]' -- an array of pointers -- in 
	descending order. Each element of the array
	points to the element of a second array. 
	After the sort, arr will point to the elements
	of the second array in descending order. 
*/

void Bubble_Sort(char *arr[], int size)
{

	bool swap;
	int temp;

	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 Show_Array_PTR(char *arr[], int size)
{
	for (int count = 0; count < size; count++)
	{
		cout << *(arr[count]) << " ";
	}

}

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

	cout << endl;
}


I believe I'm correct in assuming that this--

array[index]

-- is the same as this:

*(array + index)

I attempted to switch all of the bracketed portions of my program with this format, but I'm stumped when it comes to these:

*(arr[count])

Since the dereferencing operator is already to the side of the statement, how do I go about in translating it to pointer notation?

Thank you in advance for any and all assistance.
Last edited on
Replace arr[count] with *(arr+count) as you did before: *(*(arr+count))
Topic archived. No new replies allowed.