In: Computer Science
C++ Program
Write a function that accepts an int array and the array’s size as arguments. The function should create a new array that is one element larger than the argument array. The first element of the new array should be set to 0. Element 0 of the argument array should be copied to element 1 of the new array, element 1 of the argument array should be copied to element 2 of the new array, and so forth. The function should return a pointer to the new array. Take your input data from the file Gradelist.txt. Your program should display: • Display your name. • Display the original array; • Display the size of the original array; • Display the new array that your function generates; • Display the size of the new array
#include
#include
using namespace std;
int *display(int arr1[5],int size);
int main()
{
int arr1[] = {};
fstream File;
File.open("integers.txt");
int n=0;
//reading a file and storing in a array
while(!File.eof())
{
File >> arr1[n];
n++;
}
File.close();
int size=sizeof(arr1)/sizeof(arr1[0]);
cout<<" name :"<<" "<<"\n";
cout<<"old array size : " <
for (int i = 0; i < size; i++){
cout << *(arr1 + i) << endl;
}
cout<<"new array size: "<
int *p=display(arr1,size);
for (int i = 0; i < size+1; i++){
cout << *(p + i) << endl;
}
return 0;
}
int *display(int m[5],int size)
{
int* array = new int[size+1];
int* p = array;
p[0]=0;
for(int i=1; i
}
return p;
}