The Array Expander is an illustration of arrays and functions.
- Arrays are variables that stores multiple values
- Functions are named statements that are executed when called
<h3>The
Array Expander program </h3>
The Array Expander program written in C++, where comments are used to explain each action is as follows:
#include <iostream>
using namespace std;
//This declares the Array Expander function
int* ArrayExpander(int *oldArr, int size){
//This declares the new array
int *newArr = new int[size * 2];
//This iteration sets values into the new array
for (int i = 0; i < size * 2; i++) {
if(i < size){
*(newArr+i) = *(oldArr+i);
}
else{
*(newArr+i) = 0;
}
}
//This returns a pointer to the new array
return newArr;
}
//The main method begins here
int main(){
//This declares the length of the array, N
int N;
//This gets input for N
cin>>N;
int initArr[N];
//If N is between 1 and 50
if(N > 0 && N <=50){
//This iteration gets values for the array
for(int i = 0; i<N; i++){
cin>>initArr[i];
}
//This calls the Array Expander function
int *ptr = ArrayExpander(initArr, N);
//This iteration prints the elements of the new array
for (int i = 0; i < N*2; i++) {
cout << ptr[i] << " ";
}
}
return 0;
}
Read more abou C++ programs at:
brainly.com/question/27246607