Question
#include using namespace std; void merge(int a1[], int a2[], int n1, int n2) { int i, j, k; int n = n1 + n2; int
#include
void merge(int a1[], int a2[], int n1, int n2) { int i, j, k; int n = n1 + n2; int arr[n]; int arr1[n1 + 1], arr2[n2 + 1];
for (i = 0; i < n1; i++) { arr1[i] = a1[i]; } arr1[i] = INT_MAX; for (j = 0; j < n2; j++) { arr2[j] = a2[j]; } arr2[j] = INT_MAX; i = 0; j = 0; for (k = 0; k < n; k++) {
if (arr1[i] < arr2[j]) { arr[k] = arr1[i]; i++; } else { arr[k] = arr2[j]; j++; } } for (k = 0; k < n; k++) {
cout << arr[k] << " "; } cout << " "; return arr; }
int main() { // testing two lists with multiple elements int a1[] = { 2,4,7,9,11 }; int a2[] = { 3,5,6,12,15 }; int n1 = sizeof(a1) / sizeof(int); int n2 = sizeof(a2) / sizeof(int); cout << "Merging a1,a2: "; merge(a1, a2, n1, n2); // testing two empty lists int b1[] = {}; int b2[] = {}; n1 = sizeof(b1) / sizeof(int); n2 = sizeof(b2) / sizeof(int); cout << "Merging b1,b2: "; merge(b1, b2, n1, n2); // this will just print a blank line due to printf(" "); at line number 41 of code // testing one empty list and another list containing multiple elements int c1[] = {}; int c2[] = { 1,5,7,9 }; n1 = sizeof(c1) / sizeof(int); n2 = sizeof(c2) / sizeof(int); cout << "Merging c1,c2: "; merge(c1, c2, n1, n2); // testing with 1 element each in both lists int d1[] = { 5 }; int d2[] = { 3 }; n1 = sizeof(d1) / sizeof(int); n2 = sizeof(d2) / sizeof(int); cout << "Merging d1,d2: "; merge(d1, d2, n1, n2); return 0; }
Using C++ I need help using the new keyword to allocate the memory on the heap. Visual Studios showing that I have 6 errors.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started