C++ assignment help, please provide comments!! 1.Write a code snippet that asks
ID: 3822256 • Letter: C
Question
C++ assignment help, please provide comments!!
1.Write a code snippet that asks the user to enter how many scores will be input and allocates an array with the requested capacity. Then ask the user again and allocate a new array using the same variable used for the previous array. What should you do to avoid leaking the memory of the previous array?
2 Write a function named combine that takes two int arrays and their lengths and returns a new array that contains all the elements in arr1 followed by the elements in arr2. Provide client code that calls the function and prints all the elements.
Explanation / Answer
1.
#include <iostream>
using namespace std;
int main()
{
int *arr;
int n;
cout << "Enter numbe rof elements: ";
cin >> n;
arr = new int[n];
delete [] arr;
cout << "Enter numbe rof elements: ";
cin >> n;
arr = new int[n];
delete [] arr;
return 0;
}
before reallocating space to same variable previously allocated space need to be freed.
2.
#include <iostream>
using namespace std;
int* combine(int a[], int b[], int n, int m)
{
int *newArr = new int[n+m];
for(int i = 0; i < n; i++)
{
newArr[i] = a[i];
}
for(int i = 0; i < m; i++)
{
newArr[n+i] = b[i];
}
return newArr;
}
int main()
{
int a[] = {1,2,3,4};
int b[] = {5,6,7};
int *c = combine(a, b, 4, 3);
for(int i = 0; i < 7; i++)
{
cout << c[i] << " ";
}
cout << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.