Create a function that returns a pointer to a 2 dimensional dynamic array of int
ID: 3814521 • Letter: C
Question
Create a function that returns a pointer to a 2 dimensional dynamic array of integer elements. The function should have 2 parameters that correspond to the size of each of the dimensions.
Here's my code so far and I just need to write the function that have 2 parameters that correspond to the size of each of the dimensions and then use the function to let the user decide how large the array is and then deletes the array.
#include <iostream>
using namespace std;
typedef int* IntPointer;
int* arrayReturnFunction();
IntPointer *arraySize();
int main()
{
int index1Size, index2Size;
cout << "Array size 1: ";
cin >> index1Size;
cout << "Array size 2: ";
cin >> index2Size;
IntPointer *dynamicArray;
dynamicArray = new IntPointer[index1Size];
for (int i = 0; i < index2Size; i++)
{
dynamicArray[i] = new int[index2Size];
}
int* p;
p = arrayReturnFunction();
p[3];
//delete[] dynamicArray;
return 0;
}
int * arrayReturnFunction()
{
int* arr = new int[5];
return arr;
}
IntPointer * arraySize()
{
}
Explanation / Answer
HI, Please find my implementation.
#include <iostream>
using namespace std;
typedef int* IntPointer;
int* arrayReturnFunction(int n);
IntPointer *arraySize(int r, int c);
int main()
{
int index1Size, index2Size;
cout << "Array size 1: ";
cin >> index1Size;
cout << "Array size 2: ";
cin >> index2Size;
IntPointer *dynamicArray = arraySize(index1Size, index2Size);
for(int i=0; i<index1Size; i++)
delete [] dynamicArray[i];
delete[] dynamicArray;
return 0;
}
int* arrayReturnFunction(int n){
int *arr = new int[n];
return arr;
}
int **arraySize(int r, int c){
IntPointer *two_d = new IntPointer[r];
for(int i=0; i<r; i++)
two_d[i] = arrayReturnFunction(c);
return two_d;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.