943,725 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 3641
  • C++ RSS
Jun 1st, 2004
0

Can this be done for 3 dimensions and more

Expand Post »
Can similar code be written for 3 dimensions:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
TJW
Reputation Points: 11
Solved Threads: 0
Newbie Poster
TJW is offline Offline
12 posts
since May 2004
Jun 1st, 2004
0

Re: Can this be done for 3 dimensions and more

>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
*/
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Jun 1st, 2004
0

Re: Can this be done for 3 dimensions and more

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

C++ Syntax (Toggle Plain Text)
  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]
TJW
Reputation Points: 11
Solved Threads: 0
Newbie Poster
TJW is offline Offline
12 posts
since May 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Bloodshed C++ IDE
Next Thread in C++ Forum Timeline: a program about fractions





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC