Can similar code be written for 3 dimensions:

#include <exception>
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
void display(long double **);
void de_allocate(long double **);

int m = 3;                               // ROWS.
int n = 5;                               // COLUMNS.

int main(void) {
   long double **data;

   try {                                 // EXCEPTIONS.
      data = new long double*[m];        //THE ROWS.
      for (int j = 0; j < m; j++)

          data[j] = new long double[n];  //THE COLUMNS
      }

   catch (std::bad_alloc) {  
      
      cout << "Could not allocate. Bye ...";
      cin >>"";
      }

   for (int i = 0; i < m; i++)
      for (int j = 0; j < n; j++)
          data[i][j] = i + j;            

   display(data);
   de_allocate(data);
   return 0;

   }

void display(long double **data) {
   for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
             cout << data[i][j] << " ";
       cout << "\n" << endl;
       }
       cout <<"Input any character to quit then press [ENTER]"<<endl;
       cin >>" ";
   }

void de_allocate(long double **data) {
   for (int i = 0; i < m;  i++)
       delete[] data[i];                 // DELETE THE COLUMNS

   delete[] data;                        // DELETE THE ROWS

   }

I am working on trying to get it to work for 3 dimensions + but I would like to see how others implement this.
Thanks yet again.

Recommended Answers

All 2 Replies

I am working on trying to get it to work for 3 dimensions + but I would like to see how others implement this.

Just keep following the pattern.

#include <iostream>


void foo(std::size_t x, std::size_t y, std::size_t z)
{int q = 0, ***array;
// Create and initialize dynamic 3-D "array"
array = new int**[x];for ( std::size_t i = 0; i < x; ++i )
{
array = new int *[y];for ( std::size_t j = 0; j < y; ++j )
{
array[j] = new int[z];for ( std::size_t k = 0; k < z; ++k )
{
array[j][k] = ++q; // initialize with something
}
}
}


// Show elementsfor ( std::size_t i = 0; i < x; ++i )
{for ( std::size_t j = 0; j < y; ++j )
{for ( std::size_t k = 0; k < z; ++k )
{
std::cout << "array["[/color] << i << [color=teal]"]["[/color] << j << [color=teal]"][" << k
<< "] = " << array[j][k] << std::endl;
}
}
}


// Free memoryfor ( std::size_t i = 0; i < x; ++i )
{for ( std::size_t j = 0; j < y; ++j )
{delete[] array[j];
}delete[] array;
}delete[] array;
}


int main()
{foo(2,3,4);return 0;
}
/* my output
array[0][0][0] = 1
array[0][0][1] = 2
array[0][0][2] = 3
array[0][0][3] = 4
array[0][1][0] = 5
array[0][1][1] = 6
array[0][1][2] = 7
array[0][1][3] = 8
array[0][2][0] = 9
array[0][2][1] = 10
array[0][2][2] = 11
array[0][2][3] = 12
array[1][0][0] = 13
array[1][0][1] = 14
array[1][0][2] = 15
array[1][0][3] = 16
array[1][1][0] = 17
array[1][1][1] = 18
array[1][1][2] = 19
array[1][1][3] = 20
array[1][2][0] = 21
array[1][2][1] = 22
array[1][2][2] = 23
array[1][2][3] = 24
*/

Thanks--> that is what I thought as this is what I tried:

#include <iostream>
using namespace std;

int scan = 10;
int xyz=3;
int pixels=512;

int main()
{
   int ***array;
   array=new int **[scan];

   for (int i=0; i<scan;i++)
   {
        array[i]=new int*[xyz];
        for(int j=0; j<xyz;j++)
        {
                array[i][j]=new int[pixels];
        }
   }

I wanted: array[scan][xyz][pixels] ie array[10][3]512]

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.