Using C++ The program should have an array of integers. It will have a function
ID: 3600553 • Letter: U
Question
Using C++ The program should have an array of integers. It will have a function that has two parameters, the integer array and the array’s size. This function will create a new array that is twice the size of the arguments array. The function should copy the contents of the argument array to the new array, and initialize the unused elements of the second array with 0. The function must return a pointer to the new array. The program will then display the contents of the new array.
Program must have the following functions
int* expandArray(int[ ], int)
void showArray(int [ ], int
Explanation / Answer
#include <iostream>
using namespace std;
int* expandArray(int[], int );
void showArray(int [], int);
int main(){
int a[5] = {1,2,3,4,5};
int *newArr = expandArray(a, 5);
showArray(newArr,10);
return 0;
}
int* expandArray(int a[], int size) {
int *newArr = 0;
newArr = new int[size * 2];
int i;
for(i=0;i<size;i++) {
newArr[i] = a[i];
}
for(;i<size*2;i++) {
newArr[i] = 0;
}
return newArr;
}
void showArray(int a[], int size) {
cout<<"Array elements are: "<<endl;
for(int i=0;i<size;i++) {
cout<<a[i]<<" ";
}
cout<<endl;
}
Output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.