If you sort an array, you must physically \"move/swap\" the data from its origin
ID: 3749568 • Letter: I
Question
If you sort an array, you must physically "move/swap" the data from its original location to other location. Small arrays not a real problem, the larger the number of items, the more I/O verhead involved. What if we need to keep that array in its original order? Creating duplicate arrays is not a solution. How about using memory pointers? What if we use a "shadow array of pointers to each index location in the original array"? The pointer addresses are swapped instead of the actual data values. Load an integer array (called it "mumbers") of size 15 with random integer values ranging from 1 to 100 Create two additional arrays of pointers, aim the array index pointers to the integer array Pass the first array of pointers to a sort function to "sort ascending order the pointer values based on the value that these pointers are "pointing" to in the "numbers" array. The pointer values will be swapped within the pointer array Pass the second array of pointers to a function to sort descending order" the pointer values based on the value that these pointers are "pointing to in the numbers" array The pointer values will be swapped within the pointer array Display the contents of the numbers array Display the numeric values that the ascending pointer array and the descending pointer array are pointing" at in the numbers array You will need to code an ascending and descending sort (simplest is a "bubble") DefaultExplanation / Answer
ScreenShot
----------------------------------------
Program
//Header files
#include<iostream>
#include<string>
using namespace std;
//Function prototype
void sortAsc(int *p);
void sortDesc(int *p);
int main()
{
//original array
int arr[15];
//Initialization of the array
for (int i = 0; i < 15; i++) {
arr[i] = rand() % 100 + 1;
}
//First shadow array
int *ptrArray1 = new int[15];
for (int i = 0; i < 15; i++) {
ptrArray1[i] =arr[i];
}
//Second Shadow array
int *ptrArray2 = new int[15];
for (int i = 0; i < 15; i++) {
ptrArray2[i] = arr[i];
}
//Display array elements
cout << "Display the contents of the array:" << endl;
for (int i = 0; i <15 ; i++) {
cout << "Index #" << i << " " << arr[i] << endl;
}
//Display shadow pointer elements
cout << "Display the contents of the array via shadow pointer:" << endl;
for (int i = 0; i < 15; i++) {
cout << "Index #" << i << " " << *(ptrArray1 + i) << " @Memory Location 0x" << ptrArray1+i << endl;
}
//Call ascending sort
sortAsc(ptrArray1);
//Display ascending order
cout << "Display the contents of the array via shadow pointer after ascending order" << endl;
for (int i = 0; i < 15; i++) {
cout << "Index #" << i << " " << ptrArray1 [i] << " @Memory Location 0x" << ptrArray1 + i << endl;
}
//call descending order
sortDesc(ptrArray2);
//Display descending order
cout << "Display the contents of the array via shadow pointer after descending order" << endl;
for (int i = 0; i < 15; i++) {
cout << "Index #" << i << " " << ptrArray2[i] << " @Memory Location 0x" << ptrArray2 + i << endl;
}
//Display original array
cout << "Display the contents of the array:" << endl;
for (int i = 0; i <15; i++) {
cout << "Index #" << i << " " << arr[i] << endl;
}
return 0;
}
//sort Asecnd definition
void sortAsc(int *p) {
for (int i = 0; i<(15 - 1); i++)
{
for (int j = 0; j<(15 - i - 1); j++)
{
if (p[j]>p[j + 1])
{
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
//sort descend definition
void sortDesc(int *p) {
for (int i = 0; i<(15 - 1); i++)
{
for (int j = 0; j<(15 - i - 1); j++)
{
if (p[j]<p[j + 1])
{
int temp = p[j];
p[j] = p[j + 1];
p[j + 1] = temp;
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.