write a function called \"toDynamic\" which accept an array ofints and the size
ID: 3618673 • Letter: W
Question
write a function called "toDynamic" which accept an array ofints and the size of that array and returns a pointer to an int.The pointer returned should be a dynamically allocated array withthe same size and contents as the array that is passed in (NOT apointer to the array). In this function, use the dynamicallyallocated when referring to the array that is passed in and pointernotation when referring to the dynamically allocated array. write a function called "toDynamic" which accept an array ofints and the size of that array and returns a pointer to an int.The pointer returned should be a dynamically allocated array withthe same size and contents as the array that is passed in (NOT apointer to the array). In this function, use the dynamicallyallocated when referring to the array that is passed in and pointernotation when referring to the dynamically allocated array.Explanation / Answer
#include<iostream.h> #include<conio.h> int* toDynamic(int array[],int size) { int *ptr=new int[size]; for(int i=0;i<size;i++) { *(ptr) = arr[i]; ptr++; } return ptr; } void main() { int arr[10]; cout<<"Enter the array "; for(int i-0;i<10;i++) { cout<<" Enter the "<<i<<" index value :"; cin>>arr[i]; } int *p; ptr = toDynamic(arr,10); cout<<" Dynamic array is below :"<<endl; for(int i=0;i<10;i++) { cout<<*ptr<<endl; ptr++; }getche();
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.