In: Computer Science
Hi. I'm trying to write a program that uses dynamic memory management to create two arrays. splice() must create the third array and return a pointer to main(). Main() must capture the pointer returned from the splice() function.
Sol'n so far, I get the results of the first and second array, but it isn't showing the results of the spliced function:
#include <iostream>
using namespace std;
// Function int* splice() inserts the 2nd array into 1st array
starting at int numOfElements
int* splice(int *array1, int *array2, int size1, int size2, int
numOfElements)
{
int *result = new int[size1 + size2];
for (int i = 0; i < numOfElements; i++)
*(result + i) = *(array1 +
i);
for (int i = 0; i < size2; i++)
*(result + numOfElements + i) =
*(array2 + i);
//Line 13 will copy ith element of the second element
into numOfElements + ith position in the final array.
for (int i = numOfElements; i < size1; i++)
*(result + size2 + i) = *(array1 +
i);
system("pause");
return result;
}
int main()
{
int size1, size2, numOfElements;
cout << "Enter the size of the first array:
";
cin >> size1;
cout << "Enter the size of the second array:
";
cin >> size2;
cout << "Enter the number of elements of the
first array to be copied before splice: ";
cin >> numOfElements;
srand(100);
int *array1 = new int[size1];
int *array2 = new int[size2];
for (int i = 0; i < size1; i++)
*(array1 + i) = rand() %
size1;
for (int i = 0; i < size2; i++)
*(array2 + i) = rand() % size2;
cout << "The contents of the first array is: " <<
endl;
for (int i = 0; i < size1; i++)
{
cout << *(array1 + i)
<< "\t";
if ((i + 1) % 10 == 0)
cout <<
endl;
}
cout << endl << "The contents of the
second array is: " << endl;
for (int i = 0; i < size2; i++)
{
cout << *(array2 + i)
<< "\t";
if ((i + 1) % 10 == 0)
cout <<
endl;
}
int *array = splice(array1, array2, size1, size2,
numOfElements);
cout << "The contents of the spliced array is: " <<
endl;
for (int i = 0; i < size1 + size2; i++)
{
cout << *(array + i) <<
"\t";
if ((i + 1) % 10 == 0)
cout <<
endl;
}
delete[] array1;
delete[] array2;
delete[] array;
//the values are deleted, but the pointers still
exist
}
#include <iostream>
#include<cstdlib>//modified here
using namespace std;
// Function int* splice() inserts the 2nd array into 1st array
starting at int numOfElements
int* splice(int *array1, int *array2, int size1, int size2, int
numOfElements)
{
int *result = new int[size1 + size2];
for (int i = 0; i < numOfElements; i++)
*(result + i) = *(array1 + i);
for (int i = 0; i < size2; i++)
*(result + numOfElements + i) = *(array2 + i);
//Line 13 will copy ith element of the second element into
numOfElements + ith position in the final array.
for (int i = numOfElements; i < size1; i++)
*(result + size2 + i) = *(array1 + i);
//system("pause");
return result;
}
//everything is fine with your code,apart from including header
file
//i provided output screenshot
int main()
{
int size1, size2, numOfElements;
cout << "Enter the size of the first array: ";
cin >> size1;
cout << "Enter the size of the second array: ";
cin >> size2;
cout << "Enter the number of elements of the first array to
be copied before splice: ";
cin >> numOfElements;
srand(100);
int *array1 = new int[size1];
int *array2 = new int[size2];
for (int i = 0; i < size1; i++)
*(array1 + i) = rand() % size1;
for (int i = 0; i < size2; i++)
*(array2 + i) = rand() % size2;
cout << "The contents of the first array is: " <<
endl;
for (int i = 0; i < size1; i++)
{
cout << *(array1 + i) << "\t";
if ((i + 1) % 10 == 0)
cout << endl;
}
cout << endl << "The contents of the second array is: "
<< endl;
for (int i = 0; i < size2; i++)
{
cout << *(array2 + i) << "\t";
if ((i + 1) % 10 == 0)
cout << endl;
}
int *array = splice(array1, array2, size1, size2,
numOfElements);
cout << "\nThe contents of the spliced array is: "
<< endl;
for (int i = 0; i < size1 + size2; i++)
{
cout << *(array + i) << "\t";
if ((i + 1) % 10 == 0)
cout << endl;
}
delete[] array1;
delete[] array2;
delete[] array;
//the values are deleted, but the pointers still exist
}
output: