Array Expander Write a function that accepts an int array and the array ’s size
ID: 3761035 • Letter: A
Question
Array Expander
Write a function that accepts an int array and the array ’s size as arguments . The function should create a new array that is twice the size of the argument 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 should return a pointer to the new array . Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50) from standard input and then reads N integers from a file named data into an array . The program then passes the array to your array expander function, and prints the values of the new expanded array on standard output , onevalue per line. You may assume that the file data has at least N values .
Prompts And Output Labels. There are no prompts for the integer and no labels for the reversed array that is printed out.
Input Validation. If the integer read in from standard input exceeds 50 or is less than 0 the program terminates silently
Explanation / Answer
Demonstrate the function by using it in a main program that reads an integer N (that is not more than 50)
from standard input and then reads N integers from a file named data into an array .
The program then passes the array to your array expander function, and prints the values
of the new expanded array on standard output , onevalue per line. You may assume that the file data has at least N values .
<<For inputting the array by reading from a file>>
#include <iostream>
using namespace std;
int* doubleArray (int*, int);
int main(int argc, char *argv[])
{
if ( argc != 2 )
cout<<"Error ";
else {
int SIZE = argv[0];
int myArray [SIZE] = <<read from your respective File>>;
int* value=myArray;
for(int position=0;position<SIZE;position++)
cout<<value[position]<<endl;
value=doubleArray(myArray, SIZE);
for(int position=0;position<SIZE*2;position++)
cout<<value[position]<<endl;
delete[] value;
value = 0;
return 0;
}
}
int* doubleArray(int* myArray, int size)
{
int* doubleTheArray=new int[size * 2];
for (int position=0;position<size;position++)
doubleTheArray[position]=myArray[position];
for (int position=size;position<size*2;position++)
doubleTheArray[position]=0;
return doubleTheArray;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.