954,184 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Can this be done for 3 dimensions and more

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.

TJW
Newbie Poster
12 posts since May 2004
Reputation Points: 11
Solved Threads: 0
 

>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.

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

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

   // Free memory
   <strong>for</strong> ( std::size_t i = 0; i < x; ++i )
   {
	  <strong>for</strong> ( std::size_t j = 0; j < y; ++j )
	  {
		 <strong>delete</strong>[] array[i][j];
	  }
	  <strong>delete</strong>[] array[i];
   }
   <strong>delete</strong>[] array;
}
 
<strong>int</strong> <strong>main</strong>()
{
   <strong>foo</strong>(2,3,4);
   <strong>return</strong> 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
*/
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

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]

TJW
Newbie Poster
12 posts since May 2004
Reputation Points: 11
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You