I have an assignment involving functions and pointers.
It's better to just paste what I am supposed to do:
a. Create a function called resize that can be used to increase the size of integer arrays dynamically. The function takes three parameters. The first parameter is the original array, the second parameter is the size of this array, and the third parameter is the size of the larger array to be created by this function. Make sure that you allocate memory from the heap inside this function. After allocating memory for the second array the function must copy the elements from the first array into the larger array. Finally, the function must return a pointer to the new array.
b. In main, allocate an array on the heap that is just large enough to store the integers 5, 7, 3, and 1.
c. Resize the array to store 10 integers by calling the resize function created in step a. Remove the old (smaller) array from the heap. Add the numbers 4, 2, and 8 to the end of the new array.
d. Write a sort function that sorts any integer array in increasing order.
e. Use the sort function to sort the array of numbers in c above. Display the sorted numbers.
So, below is what I have so far. First of all, I'm not sure if I am doing it correctly. It probably looks messy. I don't have a lot of experience, so I am not trying to do anything advanced. It took me a while to comprehend stack and heap. I admit, I am a slow learner, and I've spent about two days on this. But the problem I have is with part c. Specifically, adding the numbers 4, 2, and 8 at the end of the new array.
It says to resize the array to store 10 integers. I did that in the function reSIZE. Then it says to remove the old array, which I call "original", from the heap. I think I did that correctly when I typed "delete[] original;". Then it says to add the numbers 4, 2, and 8 at the end of the new array. I'm just wondering if I need to add another array to do this? Or am I supposed to be using the one I have in main? Keep in mind that there is not supposed to be any input. I'm just lost when it comes to adding those numbers. I will probably figure it out eventually, but this part is bothering me.
Thanks.
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
|
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdlib>
using namespace std;
void reSIZE(int *&original, int &SIZE, const int &maxSIZE); //function prototype resize
void sortFUNC(int *&original, int &SIZE, const int &maxSIZE); //function prototype sortFUNC
int main()
{
int SIZE = 4; //size of current array
int maxSIZE = 10; //size of final array
int *original = new int[SIZE] {5, 7, 3, 1}; //old(current) array
cout << "Elements in array: "; //test output
reSIZE(original, SIZE, maxSIZE); //call function resize
cout << endl << endl; //blank line
cout << "Elements in array in increasing order: "; //test output
sortFUNC(original, SIZE, maxSIZE); //call function sortFUNC
cout << endl << endl;
return 0;
}
void reSIZE(int *&original, int &SIZE, const int &maxSIZE)//function definition
{
int *temporiginal = new int[SIZE + 3]; //(final)new array
for (int i = 0; i < SIZE; i++) //copy old array to new array
{
temporiginal[i] = original[i];
cout << original[i] << setw(3);
}
delete[] original; //delete old array
original = temporiginal; //point old array to new array
}
void sortFUNC(int *&original, int &SIZE, const int &maxSIZE)
{
for (int i = 0; i < SIZE; i++)
{
int smallest = original[i];
int smallestINDEX = i;
for (int m = i; m < SIZE; m++)
{
if (original[m] < smallest)
{
smallest = original[m];
smallestINDEX = m;
}
}
swap(original[i], original[smallestINDEX]);
}
int *temporiginal = new int[SIZE + 3];
for (int i = 0; i < SIZE; i++)
{
temporiginal[i] = original[i];
cout << original[i] << setw(3);
}
delete[] original;
original = temporiginal;
}
|