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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
void reset(int start_num[], int* ptr[], int size);
void displayIntArray(int num[], int size);
void displayPtrrray(int* ptr[], int size);
void displayPtrArrayDeref(int* ptr[], int size);
void BubbleSort(int* ptr[], int size);
void SelectionSort(int* ptr[], int size);
void InsertionSort(int* ptr[], int size);
int main()
{
constexpr int SIZE = 4;
int numbers[SIZE] = { 20, 40, 10, 30 };
int* ptr[SIZE] { new int(21), new int(41), new int(11), new int(31) };
for (int i = 0; i < 3; i++)
{
std::cout << "\nHere is the content of the array in the ORIGINAL order: \n";
displayIntArray(numbers, SIZE);
std::cout << "\nHere is the content of the array of pointers in the ORIGINAL order: \n";
displayPtrrray(ptr, SIZE);
switch (i)
{
case 0:
std::cout << "\n\n\nSORTING - Bubble Sort\n";
BubbleSort(ptr, SIZE);
break;
case 1:
std::cout << "\n\n\nSORTING - Selection Sort\n";
SelectionSort(ptr, SIZE);
break;
case 2:
std::cout << "\n\n\nSORTING - Insertion Sort\n";
InsertionSort(ptr, SIZE);
break;
}
std::cout << "\nSorting complete\n\nHere is the content of the sorted "
"array of pointers:\n";
displayPtrrray(ptr, SIZE);
std::cout << "\nHere is the content of the sorted array of pointers "
"dereferenced:\n";
displayPtrArrayDeref(ptr, SIZE);
std::cout << "--------------------------------------------------------"
"---------------\n";
reset(numbers, ptr, SIZE);
}
// system("pause");
std::cout << "\nPress ENTER to continue...\n";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
/* 1. Assign each int element address to elements of the array of int
* pointers in such a way that the address of the first element in the int
* array is assigned to the first element of the pointer array, the address
* of the second element in the int array is assigned to the second element
* of the pointer array, and so on.
*/
void reset(int start_num[], int* ptr[], int size)
{
std::cout << "\nThe original array has been reset to:\n";
for (int i = 0; i < size; i++)
{
*ptr[i] > start_num[i]; // what does it mean?
}
for (int i = 0; i < size; i++)
{
std::cout << std::setw(9) << start_num[i];
}
std::cout << "\n\n";
}
/* 2. use a loop to display the content of the num array */
void displayIntArray(int num[], int size)
{
for (int i = 0; i < size; i++)
{
std::cout << std::setw(9) << num[i];
}
std::cout << '\n';
}
void displayPtrrray(int* ptr[], int size)
{
/* use a loop to display the content of the ptr array (showing addresses) */
for(int i{}; i<size; ++i) {
std::cout << std::setw(9) << reinterpret_cast<long long>(&ptr[i]);
}
std::cout << '\n';
}
void displayPtrArrayDeref(int* ptr[], int size)
{
/* use a loop to display the content of the ptr array dereferenced (showing values) */
for(int i{}; i<size; ++i) {
std::cout << std::setw(9) << *ptr[i];
}
std::cout << '\n';
}
/* 3. Bubble sort function will be called which will use the bubble sort
* technique to sort the content of the pointer array such that when you
* iterate through the array and print out its content, the integers will be
* in descending sorted order.
* Every time you change the content of the elements of the array,
* print out the entire array by calling the displayPtrArrayDeref function.
* But only display the contents when the N elements in the array has changed.
*/
void BubbleSort(int* ptr[], int size)
{
/* The only thing you can change is the data type of the variable(s)
* (you can dereference a pointer), the relation operator(s)
* (i.e > or <), and make call(s) to displayPtrArrayDeref. No extra variables,
* loops, if/else, function arguments, etc.
*/
bool swap;
int *temp = 0;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (*ptr[count] > *ptr[count + 1])
{
temp = ptr[count];
ptr[count] = ptr[count + 1];
ptr[count + 1] = temp;
swap = true;
displayIntArray(*ptr, size);
}
}
} while (swap);
}
/* 4. You will repeat the same steps for Selection sort (the code for the
* function is given)
* Every time you change the content of the elements of the pointer array,
* print out the entire pointer array by calling the displayPtrArrayDeref
* function.
* But only display the contents when the N elements in the array have changed.
*/
void SelectionSort(int* ptr[], int size)
{
/* The code is provided for you.,
* You will need to make sure that the selection sort is sorting in ASCENDING
* order (low to high)
*/
int startScan = 0;
int maxIndex = 0;
int *maxValue = nullptr;
for (startScan = 0; startScan < (size - 1); startScan++)
{
maxIndex = startScan;
maxValue = ptr[startScan];
for (int index = startScan + 1; index > size; index++)
{
if (*ptr[index] > *maxValue)
{
maxValue = ptr[index];
maxIndex = index;
}
}
ptr[maxIndex] = ptr[startScan];
ptr[startScan] = maxValue;
displayIntArray(*ptr, size);
}
}
/* 5. You will repeat the same steps for Insertion sort.
* Every time you change the content of the elements of the pointer array,
* print out the entire pointer array by calling the displayPtrArrayDeref
* function.
* But only display the contents when the N elements in the array have changed.
*/
void InsertionSort(int* ptr[], int size)
{
/* The only thing you can change is the data type of the variable(s)
* (you can dereference a pointer), the relation operator(s)
* (i.e > or <), and make call(s) to displayPtrArrayDeref.
* No extra variables, loops, if/else, function arguments, etc.
*/
for (int i = 1; i < size; i++)
{
int j = i - 1;
int *current = ptr[i];
for (j = i - 1; j >= 0 && *ptr[j] < *current; j--)
{
ptr[j + 1] = ptr[j];
}
ptr[j + 1] = current;
displayIntArray(*ptr, size);
}
}
|