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
|
#include <iostream>
using namespace std;
int *duplicateArray(const int *, int);
void displayArray(const int[], int);
void sortArray(int[], int);
void showArray(const int [], int);
int main()
{
const int SIZE = 50;
int numbers[SIZE] = { 888, 552, 642, 191, 935, 584, 959, 495, 945, 27, 240, 637, 712, 608, 924,
630, 712, 234, 275, 258, 918, 15, 320, 144, 365, 305, 696, 783, 114,
434, 925, 462, 266, 985, 318, 728, 285, 79, 337, 115, 662, 798, 970,
433, 758, 213, 284, 154, 126, 543 };
int *dupA = nullptr;
//makes a duplicate of numbers array into array called dupA
dupA = duplicateArray(numbers, SIZE);
//sorts duplicate array
sortArray(dupA, SIZE);
cout << "The address of the original array is " << &numbers << endl;
cout << "The address of the duplicate array is " << &dupA << endl;
//displays original array
cout << "The contents of the numbers array: \n";
showArray(numbers, SIZE);
//displays sorted duplicate array
cout << "The contents of the duplicate array: \n";
showArray(dupA, SIZE);
delete [] dupA;
dupA = nullptr;
system("pause");
return 0;
}
/* This function duplicates the Original array (numbers)
Inputs:
arr - array
size - size of array, numbers in it
Outputs: none
Return: Duplicate array
*/
int *duplicateArray(const int *arr, int size)
{ int *newArray = nullptr;
//validates size. If 0 or less returns null pointer
if (size <= 0)
return nullptr;
//Allocate a new array
newArray = new int[size];
//copies data from numbers array to new array
for (int index = 0; index < size; index++)
newArray[index] = arr[index];
return newArray;
}
/* This function sorts an array
Inputs:
array[] - array
size - numbers in array
Outputs: None
Return: Swaps the numbers in the array from least to greatest
*/
void sortArray(int array[], int size)
{
bool swap;
int temp;
do
{
swap = false;
for (int count = 0; count < (size - 1); count++)
{
if (array[count] > array[count + 1])
{
temp = array[count];
array[count] = array[count + 1];
array[count + 1] = temp;
swap = true;
}
}
} while (swap);
}
void showArray(const int array[], int size)
{
for (int count = 0; count < size; count++)
cout << array[count] << " ";
cout << endl;
}
|