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

C++ Program Create a function that returns a pointer to a 2 dimensional dynamic

ID: 3812477 • Letter: C

Question

C++ Program

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.

Use the function in a program that lets the user decide how large the array is and then deletes the array.

Here's my code so far:

#include <iostream>
using namespace std;

typedef int* IntPointer;

int* arrayReturnFunction();
IntPointer *array();

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 * array()
{
  
}

Explanation / Answer

#include<iostream>

using namespace std;

int **createArray(int row, int col) {
   int **arr = new int*[row]; /*create an array of pointers*/
   for (int i=0; i<row; i++) /* now for each ponter we create an array*/
       arr[i] = new int[col];
   return arr;
}

int main() {
   int index1Size, index2Size;
   cout << "Array size 1: ";
   cin >> index1Size;
   cout << "Array size 2: ";
   cin >> index2Size;

   int **dynamicArray;
   dynamicArray = createArray(index1Size,index2Size);
   /* We now have a 2D array of customized size */
   /* Now its time to delete the array */

   /* Deletion must occur exactly the opposite way we create the the array */

   /* we need to delete the internal arrays*/
   for (int i=0; i<index1Size; i++)
       delete[] dynamicArray[i];
   /* now we need to delete the external array of pointers*/
   delete[] dynamicArray;
   /*Hurrah! we are done :) */
}

According to your specification, this is the solution you should opt for. Your code doesnt seem to be going the correct way. I commented the code for your help. If you face any trouble regarding the solution I provided, get back to me through comments.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote