Write a program that creates a pointer to a dynamic array of integers of size 10
ID: 3804767 • Letter: W
Question
Write a program that creates a pointer to a dynamic array of integers of size 10. The program will load the array with values from 1 to 10 then:
Using the following functions:
A function that accepts the pointer to the integer array and the array size as a arguments, creates a pointer to a new copy of the array, except the values will be reversed in the copy. The function should return the pointer to the new array.
A function that accepts the integer array, and the array size as an argument. The function should create a new array that is twice the size of the original array. It should copy the original array information in and then initialize the unused elements to 0. It should return a pointer to the new array.
A function that accepts the integer array, and the array size as an argument. The function should create a new vector object that is twice the size of the original array. It should copy only the even values of the original array information into the new vector and then initialize the unused elements to 0. It should return a pointer to the new vector.
A function that accepts the integer array and the array size as a arguments. It should print the array's data with spaces between the numbers
Any other function you need.
In main,
print the array
call the reverse function,
print the reversed array,
call the doubling function
print the doubled array
call the vector creator function
print the vector.
Print a header before each array print to know what it is doing.
Explanation / Answer
/* DynamicArray.CPP */
#include<iostream>
#include<vector>
using namespace std;
int* reverse_array(int*,int);
int* double_array(int*,int);
vector<int>* vector_creator(int*,int);
void print_array(int*,int);
int main()
{
int i,size=10;
int *array = new int[size];//dynamic array creation
for(i=0;i<size;i++)
array[i] = i+1;
int *rev = reverse_array(array,size);
cout<<"Reverse of an array:"<<endl;
for(i=0;i<size;i++){
cout<<rev[i]<<" ";
}
int *dou_arr = double_array(array,size);
cout<<" Double of an array:"<<endl;
for(i=0;i<2*size;i++){
cout<<dou_arr[i]<<" ";
}
std::vector<int> *vec_ptr = vector_creator(array,size);
print_array(array,size);
}
int* reverse_array(int *arr,int size){
int *new_ptr = new int[size];
int k = 0;
for(int i=size-1;i>=0;i--){
new_ptr[k] = arr[i];
k++;
}
return new_ptr;
}
int* double_array(int *arr,int size){
int do_size = 2*size;
int *dou_ptr = new int[do_size];
for(int i=0;i<do_size;i++){
if(i<size)
dou_ptr[i] = arr[i];
else
dou_ptr[i] = 0;
}
return dou_ptr;
}
vector<int>* vector_creator(int *arr,int size){
std::vector<int> vec;
std::vector<int> *vec_ptr;
cout<<" Vector Elements:"<<endl;
for(int i=0;i<size;i++){
if(arr[i]%2 == 0){
int num = arr[i];
vec.push_back(num);
}
}
for(int i=0;i<size;i++){
if(i>=vec.size())
vec.push_back(0);
cout<<vec[i]<<" ";
}
vec_ptr = &vec;
return vec_ptr;
}
void print_array(int *arr,int size){
cout<<" Array Elements:"<<endl;
for(int i=0;i<size;i++){
cout<<arr[i]<<" ";
}
}
/* Sample Input and Output */
Reverse of an array:
10 9 8 7 6 5 4 3 2 1
Double of an array:
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0
Vector Elements:
2 4 6 8 10 0 0 0 0 0
Array Elements:
1 2 3 4 5 6 7 8 9 10
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.