C++ Program Write a function that accepts an int array and the array’s size as a
ID: 3824441 • Letter: C
Question
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
Explanation / Answer
#include <iostream>
#include <fstream>
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 :"<<" "<<" ";
cout<<"old array size : " <<size<<" ";
cout<<"old array values " <<" ";
for (int i = 0; i < size; i++){
cout << *(arr1 + i) << endl;
}
cout<<"new array size: "<<size+1<<" ";
cout<<"new array values"<<" ";
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<size+1; ++i) {
p[i] = m[i-1];
}
return p;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.