Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

********************************** The app we were taught using is visual studio

ID: 3857848 • Letter: #

Question

**********************************   The app we were taught using is visual studios 2015 with the format following:

#include <iostream>;

using namespace std;

int main() {

system("pause");

return 0; } ****************************

Objective Exercise creating, accessing and printing from dynamically allocated array Task Given array in main() int array[ ]=(1,2,3,4,5,6,7,8,9,10); Write a code that fulfills the following requirements Declare the given array[in main int array[ ]={1,2,3,4,5,6,7,8,9,10); * Place the following functions below main Create a pointer & dynamically create array for this pointer in createArray() function Copy this array (that is declared in main)) into dynamically allocated array that was created in createArray() in copyArray() function - pass parameters appropriately Create a function, freeMemory(), to free up memory make sure to pass the array correctly Now, print dynamically created array but make sure to parse through using pointer notation in printArray) function o (px+i) reference the value contained in a[i Now, call these above created functions in given order o createArray() o copyArray() o printArray() o freeMemory()

Explanation / Answer

#include <iostream>
using namespace std;
int* createArray(int size) {
int *p= new int[size];
}
void copyArray(int array[], int *p, int size){
for(int i=0;i<size; i++) {
*(p+i)=array[i];
}
}
void printArray(int *p, int size){
for(int i=0;i<size; i++) {
cout<<*(p+i)<<" ";
}
cout<<endl;
}
void freeMemory(int *p) {
delete p;
}
int main() {
int size = 10;
int array[] = {1,2,3,4,5,6,7,8,9,10};
int *p = createArray(size);
copyArray(array, p, size);
printArray(p,size);
freeMemory(p);
return 0;
  
}

Output:

sh-4.2$ g++ -o main *.cpp                                                                                                                                                                            

sh-4.2$ main                                                                                                                                                                                         

1 2 3 4 5 6 7 8 9 10