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
|
#include <iostream>
#include <iomanip>
#include <string>
#include <vector>
using namespace std;
void reset(const int num[], const int * ptr[],int size)
{
for (int i=0; i < size; i++){
ptr[i] = &num[i];
}
/* 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 displayIntArray (const int num[], int size)
{
//use a loop to display the content of the num array
}
void displayPtrArray (const int * ptr[], int size)
{
for (int i = 0; i < size; i ++){
cout << *ptr[i] << endl;
}
}
void displayPtrArrayDeref (const int * ptr[], int size)
{
for (int i = 0; i < size; i ++){
cout << *ptr[i] << endl;
}
}
void BubbleSort(const int *ptr [], int size)
{
bool swap;
//int temp;
const int * temp = 0;
do
{
swap = false;
for (int count = 0; count < (size-1); count++)
{
//cout<< "Test part 1: " << *ptr[count] << endl;
if (*ptr[count] > *ptr[count + 1])
{
ptr[count] = ptr[count + 1];
ptr[count + 1] = temp;
swap = true;
displayPtrArray(ptr,size);
}
}
}while (swap);
/*Modify the BubbleSort algorithm on page 482 OF YOUR BOOK.
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 displayPtrArray. No extra variables, loops, if/else,
function arguments, etc.*/
}
void SelectionSort(const int *ptr [], int size)
{
// The code is provided for you....you don't need to do anything for this function
int startScan, maxIndex;
const int * maxValue;
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;
displayPtrArray (ptr, size);
}
}
void InsertionSort(const int *ptr [], int size)
{
/*Modify the Insertion Sort algorithm from page 470.
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 displayPtrArray.
No extra variables, loops, if/else,
function arguments, etc.*/
}
int main ()
{
const int SIZE =4;
const int numbers[SIZE] = {20, 40, 10, 30};
const int * ptr[SIZE];
for (int i =0; i < 3; i++)
{
reset(numbers, ptr, SIZE);
cout<<"\nHere is the content of the array in the ORIGINAL order: \n";
//call displayIntArray
cout<<"\nHere is the content of the array of pointers in the ORIGINAL order: \n";
displayPtrArray(ptr,SIZE);
switch (i)
{
case 0:
cout<<"\nSORTING - Bubble Sort\n";
BubbleSort(ptr,SIZE);
break;
case 1:
cout<<"\nSORTING - Selection Sort\n";
SelectionSort(ptr,SIZE);
break;
case 2:
cout<<"\nSORTING - Insertion Sort\n";
//call InsertionSort
break;
}
cout<<"\nDONE\n";
cout<<"\n\nHere is the content of the array: \n";
//call displayIntArray
cout<<"\n\nHere is the content of the sorted array of pointers: \n";
displayPtrArray(ptr,SIZE);
cout<<"\n\nHere is the content of the sorted array of pointers dereferenced: \n";
// call displayPtrArrayDeref
cout <<endl<<endl<<endl;
cout <<"-----------------------------------------------------------------------\n";
}
system("pause");
return 0;
}
|