Question

In: Computer Science

Hi. I'm trying to write a program that uses dynamic memory management to create two arrays....

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
}

Solutions

Expert Solution

#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:


Related Solutions

Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an...
Write C program Multidimensional Arrays Design a program which uses two two-dimensional arrays as follows: an array which can store up to 50 student names where a name is up to 25 characters long an array which can store marks for 5 courses for up to 50 students The program should first obtain student names and their corresponding marks for a requested number of students from the user. Please note that the program should reject any number of students that...
MIPS Program I'm trying to write a program that will take in two numbers from the...
MIPS Program I'm trying to write a program that will take in two numbers from the user and output the sum at the end. However, I keep getting very wrong answers. For example, I add 2 and 2 and get 268501000. Help would be appreciated. .data #starts data use userIn1:    .word 4 #sets aside space for input    userIn2:    .word 4 #sets aside space for input total:    .word 4 #sets space aside for input    request:   ...
I'm trying to write a feet to meters and centimeters program and I'm looking for a...
I'm trying to write a feet to meters and centimeters program and I'm looking for a little help so I can see how close I got. It should do the following: Write a convertToMetric class that has the following field: standard - holds a double, standard length value in feet. The class should have the following methods: Constructor - that accepts a length in feet (as a double) and stores it in the standard field. setStandard - accepts a standard...
Write program numsODD that uses the method to create arrays of odd nums starting 1 of...
Write program numsODD that uses the method to create arrays of odd nums starting 1 of the given length: public static int[] getnumsOdd(int length) {} int[] numbers = getnumsOdd(5); System.out.println(Arrays.toString(numbers)); output: [1, 3, 5, 7, 9]
Write a complete program called EvenNums that uses a method to create arrays of even numbers...
Write a complete program called EvenNums that uses a method to create arrays of even numbers starting 1 of the given length: public static int[] getEvenNums(int length) {} EX: int[] nums = getEvenNumbers(6); System.out.println(Arrays.toString(nums)); expected output: [2, 4, 6, 8, 10, 12]
I'm trying to get the union of two arrays and I tried making a loop that...
I'm trying to get the union of two arrays and I tried making a loop that does so. I tried also making the loop give me the size of the array as well from the union of the two arrays. Please check my loop to see what is wrong with it because it is not doing what I want it to do. I'm also not sure how to get the correct size of the array after the two arrays have...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two...
Write a multithreaded program in C using the pthread library and dynamic memory(malloc) that multiplies two matrices together. The numbers in the matrices must be read in from a text file. The program should also check if the two matrices are capable of being multiplied together. The amount of threads used has to be dynamic. The user should be able to choose how many threads they wish to use using the command line. Finally, the result must be stored in...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program the declares and uses two parallel arrays. One array for storing the names...
Write a program the declares and uses two parallel arrays. One array for storing the names of countries and a second array for storing the populations of those countries. As you can see per the following the Country name and it's corresponding Population are stored at the same element index in each array. China 1367960000 India 1262670000 United States 319111000 Indonesia 252164800 Brazil 203462000 Pakistan 188172000 Nigeria 178517000 Bangladesh 157339000 Russia 146149200 Japan 127090000 In the main method write a...
Write a program that uses two identical arrays of at least 25 integers. It should call...
Write a program that uses two identical arrays of at least 25 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in descending order. The function should keep a count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other array. It should also keep count of the number of exchanges it makes. Display these values on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT