| | |
Can this be done for 3 dimensions and more
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: May 2004
Posts: 12
Reputation:
Solved Threads: 0
Can similar code be written for 3 dimensions:
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.
C++ Syntax (Toggle Plain Text)
#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.
>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.
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 */
•
•
Join Date: May 2004
Posts: 12
Reputation:
Solved Threads: 0
Thanks--> that is what I thought as this is what I tried:
I wanted: array[scan][xyz][pixels] ie array[10][3]512]
C++ Syntax (Toggle Plain Text)
#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]
![]() |
Similar Threads
- Vector Dimensions (C++)
- Element. Dimensions.js -- i m getting problem (JavaScript / DHTML / AJAX)
- Find the dimensions of a button (C++)
- How to put the random integer between 1-100 into the 2 dimensions array (5x5 metrix) (C++)
- module to get video file dimensions (Python)
Other Threads in the C++ Forum
- Previous Thread: Bloodshed C++ IDE
- Next Thread: a program about fractions
Views: 3148 | Replies: 2
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api array arrays based beginner binary bmp c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete deploy dll download dynamic encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library lines linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number output pointer problem program programming project python random read recursion recursive reference return simple sort spoonfeeding stream string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






