Question
Lab - C++ In this lab, you will sort an array of constant integers in place using three different techniques. 1. reset function will be
Lab - C++
In this lab, you will sort an array of constant integers in place using three different techniques.
1. reset function will be used to set the array to the original numbers (5 points)
2. displayIntArray will be used to printout the content of the sorted array. (5 points)
3. displayPtrArray will be used to printout the pointer addresses of each of the elements of the pointer array. This must be done before and after the each sort is done (no text should be displayed in this function). (5 points)
4. Bubble sort function will be called which will use the bubble sort techinque 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 ascending sorted order. Everytime you change the content of the elements of the pointer array, print out the entire pointer array by calling the void displayPtrArrayDeref function. But only display the contents when the elements in the aray have changed. (10 points)
5. The Selection sort (the code for the function is given) function will be called which will use the selection sort techinque 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 ascending sorted order. Everytime you change the content of the elements of the pointer array, print out the entire pointer array by calling the void displayPtrArrayDeref function. But only display the contents when the elements in the aray have changed. (10 points)
6. The Insertion sort function will be called which will use the insertion sort techinque 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 ascending sorted order. Everytime 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 aray have changed. (9 points)
7. The last line will contain the phrase Sorting Complete and your name. (1 point)
8. Your output should match the following pages.
Here is the starter code:
/* Name Lab 3 */
#include
void reset(const int [], const int *ptr[], int); void displayIntArray(const int num[], int size); void displayPtrArray(const int *ptr[], int size); void displayPtrArrayDeref(const int *ptr[], int size); void BubbleSort(const int *ptr[], int size); void SelectionSort(const int *ptr[], int size); void InsertionSort(const int *ptr[], int size);
int main () { const int SIZE = 4; const int numbers[SIZE] = {20, 40, 10, 30}; const int *ptr[SIZE];
for (int i =0; i
cout
cout
/* 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(const int start_num[], const int *ptr[], int size) {
}
/* 2. use a loop to display the content of the num array */ void displayIntArray(const int num[], int size) {
}
void displayPtrArray(const int *ptr[], int size) { /* use a loop to display the content of the ptr array (showing addresses) */
}
void displayPtrArrayDeref(const int *ptr[], int size) { /* use a loop to display the content of the ptr array dereferenced (showing values) */ }
/* 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(const 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
bool swap; const int *temp = 0;
do { swap = false; for (int count = 0; count *ptr[count + 1]) { temp = ptr[count]; ptr[count] = ptr[count + 1]; ptr[count + 1] = temp; swap = true; } } } 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(const 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; const int *maxValue = nullptr;
for (startScan = 0; startScan size; index++) { if (*ptr[index] > *maxValue) { maxValue = ptr[index]; maxIndex = index; } } ptr[maxIndex] = ptr[startScan]; ptr[startScan] = maxValue; } }
/* 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(const 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
for (int i = 1; i
for (j = i - 1; j >= 0 && *ptr[j] Here is the content of the array in the ORIGINAL order: 40 10 Here is the content of the array of pointers in the ORIGINAL order: 0025FED8 0025FEDC 0025FEEO 0025FEE4 SORTING Bubble Sort 20 20 10 10 10 20 40 30 30 40 40 DONE Sorting Here is the content of the sorted array of pointers: Here is the content of the sorted array of pointers dereferenced: 0025FEEO 0025FED8 0025FEE4 0025FEDC 10 20 30 40 Here is the content of the array in the ORIGINAL order: 40 10 30 Here is the content of the array of pointers in the ORIGINAL order: 0025FED8 0025FEDC 0025FEEO 0025FEE4 SORTING-Selection Sort 10 10 10 40 20 40 30 30 20 40 DONE sorting Here is the content of the sorted array of pointers: Here is the content of the sorted array of pointers dereferenced: 0025FEEO 0025FED8 0025FEE4 0025FEDC 10 20 30 40 Here is the content of the array in the ORIGINAL order: 40 10 Here is the content of the array of pointers in the ORIGINAL order: 0025FED8 0025FEDC 0025FEEO 0025FEE4 SORTING Insertion Sort 20 40 40 20 20 10 10 10 40 40 40 40 30 20 20 40 DONE sorting Here is the content of the sorted array of pointers: Here is the content of the sorted array of pointers dereferenced: 0025FEEO 0025FED8 0025FEE4 0025FEDC 10 20 30 40 Done by Clem Kadiddlehopper Press any key to continue . . . _ Here is the content of the array in the ORIGINAL order: 40 10 Here is the content of the array of pointers in the ORIGINAL order: 0025FED8 0025FEDC 0025FEEO 0025FEE4 SORTING Bubble Sort 20 20 10 10 10 20 40 30 30 40 40 DONE Sorting Here is the content of the sorted array of pointers: Here is the content of the sorted array of pointers dereferenced: 0025FEEO 0025FED8 0025FEE4 0025FEDC 10 20 30 40 Here is the content of the array in the ORIGINAL order: 40 10 30 Here is the content of the array of pointers in the ORIGINAL order: 0025FED8 0025FEDC 0025FEEO 0025FEE4 SORTING-Selection Sort 10 10 10 40 20 40 30 30 20 40 DONE sorting Here is the content of the sorted array of pointers: Here is the content of the sorted array of pointers dereferenced: 0025FEEO 0025FED8 0025FEE4 0025FEDC 10 20 30 40 Here is the content of the array in the ORIGINAL order: 40 10 Here is the content of the array of pointers in the ORIGINAL order: 0025FED8 0025FEDC 0025FEEO 0025FEE4 SORTING Insertion Sort 20 40 40 20 20 10 10 10 40 40 40 40 30 20 20 40 DONE sorting Here is the content of the sorted array of pointers: Here is the content of the sorted array of pointers dereferenced: 0025FEEO 0025FED8 0025FEE4 0025FEDC 10 20 30 40 Done by Clem Kadiddlehopper Press any key to continue . . . _
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started