Can this be done for 3 dimensions and more

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: May 2004
Posts: 12
Reputation: TJW is an unknown quantity at this point 
Solved Threads: 0
TJW TJW is offline Offline
Newbie Poster

Can this be done for 3 dimensions and more

 
0
  #1
Jun 1st, 2004
Can similar code be written for 3 dimensions:

  1. #include <exception>
  2. #include <iostream>
  3. using std::cin;
  4. using std::cout;
  5. using std::endl;
  6. void display(long double **);
  7. void de_allocate(long double **);
  8.  
  9. int m = 3; // ROWS.
  10. int n = 5; // COLUMNS.
  11.  
  12. int main(void) {
  13. long double **data;
  14.  
  15. try { // EXCEPTIONS.
  16. data = new long double*[m]; //THE ROWS.
  17. for (int j = 0; j < m; j++)
  18.  
  19. data[j] = new long double[n]; //THE COLUMNS
  20. }
  21.  
  22. catch (std::bad_alloc) {
  23.  
  24. cout << "Could not allocate. Bye ...";
  25. cin >>"";
  26. }
  27.  
  28. for (int i = 0; i < m; i++)
  29. for (int j = 0; j < n; j++)
  30. data[i][j] = i + j;
  31.  
  32. display(data);
  33. de_allocate(data);
  34. return 0;
  35.  
  36. }
  37.  
  38. void display(long double **data) {
  39. for (int i = 0; i < m; i++) {
  40. for (int j = 0; j < n; j++)
  41. cout << data[i][j] << " ";
  42. cout << "\n" << endl;
  43. }
  44. cout <<"Input any character to quit then press [ENTER]"<<endl;
  45. cin >>" ";
  46. }
  47.  
  48. void de_allocate(long double **data) {
  49. for (int i = 0; i < m; i++)
  50. delete[] data[i]; // DELETE THE COLUMNS
  51.  
  52. delete[] data; // DELETE THE ROWS
  53.  
  54. }



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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,461
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 254
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: Can this be done for 3 dimensions and more

 
0
  #2
Jun 1st, 2004
>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[i] = new int *[y];
	  for ( std::size_t j = 0; j < y; ++j )
	  {
		 array[i][j] = new int[z];
		 for ( std::size_t k = 0; k < z; ++k )
		 {
			array[i][j][k] = ++q; // initialize with something
		 }
	  }
   }

   // Show elements
   for ( 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[" << i << "][" << j << "][" << k
					  << "] = " << array[i][j][k] << std::endl;
		 }
	  }
   }

   // Free memory
   for ( std::size_t i = 0; i < x; ++i )
   {
	  for ( std::size_t j = 0; j < y; ++j )
	  {
		 delete[] array[i][j];
	  }
	  delete[] array[i];
   }
   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
*/
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 12
Reputation: TJW is an unknown quantity at this point 
Solved Threads: 0
TJW TJW is offline Offline
Newbie Poster

Re: Can this be done for 3 dimensions and more

 
0
  #3
Jun 1st, 2004
Thanks--> that is what I thought as this is what I tried:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. int scan = 10;
  5. int xyz=3;
  6. int pixels=512;
  7.  
  8. int main()
  9. {
  10. int ***array;
  11. array=new int **[scan];
  12.  
  13. for (int i=0; i<scan;i++)
  14. {
  15. array[i]=new int*[xyz];
  16. for(int j=0; j<xyz;j++)
  17. {
  18. array[i][j]=new int[pixels];
  19. }
  20. }

I wanted: array[scan][xyz][pixels] ie array[10][3]512]
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 3148 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC