| | |
2 D array question
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
I wrote this little code to test a 2 dimensional array. Given the array array[row][col] I can find total_columns, but I am at loss to figure out total_rows.
C++ Syntax (Toggle Plain Text)
// 2 D array #include <iostream> using namespace std; int main() { int row, col, total_columns; // two dimensional array in the form array[row][col] int iarray2[3][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9,10, 11,12,13,14,15 }; total_columns = sizeof(iarray2[0])/sizeof(int); // result = 5 //total_rows = ??; for(row = 0; row < 3 ; row++) { for(col = 0; col < total_columns; col++) { cout << row << " " << col << " " << iarray2[row][col] << endl; } cout << endl; } // wait cin.get(); return EXIT_SUCCESS; }
Take the total size and divide it by the size of one row. 

C++ Syntax (Toggle Plain Text)
#include <iostream> int main() { int row, col; int total_columns; int total_rows; int iarray2[3][5] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11,12,13,14,15 }; total_columns = sizeof(iarray2[0])/sizeof(int); // result = 5 total_rows = sizeof iarray2 / sizeof iarray2[0]; // result = 3 for(row = 0; row < total_rows; row++) { for(col = 0; col < total_columns; col++) std::cout<< row <<" "<< col <<" " << iarray2[row][col] <<'\n'; std::cout<<'\n'; } std::cout<<"Total columns: "<< total_columns <<'\n' <<"Total Rows: "<< total_rows <<'\n'; }
![]() |
Similar Threads
- Little Simple array question :) (C)
- Simple array question (C++)
- Simple char array question (C)
- Newbie Constructor / Array question (C++)
- Array Question (C++)
- C++ Array Question (C++)
- array question (C++)
Other Threads in the C++ Forum
- Previous Thread: segmentation fault
- Next Thread: Got the C++, what now?
| Thread Tools | Search this Thread |
api array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamiccharacterarray email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linux list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






