+ 5

How to show matrix in c++?

17th Jun 2017, 6:17 PM
Harshit Kumar
42 Respuestas
+ 3
#include <iostream> using namespace std; int main() { int r,c; cin >> r; cin >> c; int (*matrix)[c] = (int(*)[c]) new int[r*c]; // r rows c columns for(int i = 0; i < r; i++) for(int j = 0; j < c; j++) *((int*)(matrix+i)+j) = i*10 + j; for(int i = 0; i < r; i++) { cout << endl; for(int j = 0; j < c; j++) cout << *((int*)(matrix+i)+j) << " "; } delete [] matrix; return 0; }
18th Jun 2017, 5:40 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 7
What a HOT debate guys!
18th Jun 2017, 6:10 PM
Babak
Babak - avatar
+ 7
Thank you Andrea and NeutronStar. I've got some value out of your analyses.
18th Jun 2017, 7:18 PM
Babak
Babak - avatar
+ 6
First of all, it depends to the dementionality of your matrix. But for a simple "m * n" matrix you need to declare a 2D array and after which two for loop to access to every single element. int arr[2][3] = { fill with some integer }; for (int i = 0; i < 2; ++i) for (int j = 0; j < 3; ++j) cout << arr[i][j]; // print out elements There's another elegant way to do so, using STL vector. std::vector<int> v(2, 75);// fill with 75 std::vector<std::vector<int> > v2d(3, v); for (size_t i = 0; i < v2d.size(); ++i) for (size_t j = 0; j < v2d[i].size(); ++j) cout << v2d[i][j];
17th Jun 2017, 6:32 PM
Babak
Babak - avatar
+ 4
Hi Andrea. Great snippet, but you need to add 1 into the right hand side of condition. https://code.sololearn.com/c9OB8knZHGE3/?ref=app
18th Jun 2017, 10:02 AM
Babak
Babak - avatar
+ 4
Thank you Andrea.
18th Jun 2017, 11:05 AM
Babak
Babak - avatar
+ 3
You can also do this with only one loop and one pointer: int matrix[30][50]; //fill your matrix int *p = matrix[0]; while(p < &matrix[29][49]) cout << *p++;
18th Jun 2017, 8:29 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
yes because i forgot =
18th Jun 2017, 10:06 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
try p<= without +1
18th Jun 2017, 10:06 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
also with only one line of code for(int *p = matrix[0]; p <= &matrix[1][2] ; p++) cout << *p;
18th Jun 2017, 10:12 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
😉😉😉
18th Jun 2017, 11:17 AM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
Pointer version #include <iostream> using namespace std; /* This code demonstrate implementation of 2D array as array of array */ int main() { int const rows = 4; int const columns = 5; // allocate memory for rows int** m = new int*[rows]; // for each row allocate memory for columns for(int r =0; r<rows; r++) { *(m+r) = new int[columns]; } // fill array with values for(int r =0; r<rows; r++) { for(int c=0; c<columns; c++) { *(*(m+r)+c) = r+c; } } // display the stored values for(int r =0; r<rows; r++) { for(int c=0; c<columns; c++) { cout << *(*(m+r)+c) << " "; } cout << endl; } // for each row free the memory allocated for columns for(int r = 0; r<rows;r++) { delete [] m[r]; } // now free memory allocated for rows delete [] m; return 0; }
18th Jun 2017, 3:44 PM
NeutronStar
NeutronStar - avatar
+ 2
too much lines and things NeutronStar...learn about pointer-to-array
18th Jun 2017, 4:11 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
#include <iostream> using namespace std; int main() { int (*matrix)[10] = (int(*)[10]) new int[5*10]; // 5 rows 10 columns for(int i = 0; i < 5; i++) for(int j = 0; j < 10; j++) matrix[i][j] = i*10 + j; for(int i = 0; i < 5; i++) { cout << endl; for(int j = 0; j < 10; j++) cout << matrix[i][j] << " "; } delete [] matrix; return 0; }
18th Jun 2017, 5:05 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
why use array of array? greater is the matrix greater is memory wasted to store all pointers to each rows
18th Jun 2017, 5:44 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
18th Jun 2017, 6:02 PM
Harshit Kumar
+ 2
I know that arrays decay into pointers and so one...I think that implement a matrix like a pointer to pointer is not the optimized way to implement a matrix.
18th Jun 2017, 6:05 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
No? This app has a purpose: teach something! I know...learn how to optimize a code may be useless if you create a program to add two number having available 2Gb of RAM. But try to create something more important with few kbytes in a embedded system like Arduino and so on. Then with your method you need: 1)more memory to store all pointers 2)much time to dereference them And think with 3d, 4d, 5d arrays... You dereference and dereference and... I get an andress (matrix[0], tensor[0], 5d[0]) then I add at this address some values...then I dereference only one time. At the end, indipendently of array's dimension. Without waste memory for array of array of matrix of array of pointers and so on...
18th Jun 2017, 6:34 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 2
My *((int*)(matrix+i)+j) is faster than your *(*(matrix+i)+j) because your matrix is a pointer to pointer my matrix is a pointer to array you dereference your matrix then you "move" it i "move" my matrix
18th Jun 2017, 6:40 PM
Andrea Simone Costa
Andrea Simone Costa - avatar
+ 1
The last way would be to define a class for Matrix as well, which will allow you to do something like this easily. Matrix a; //Define a matrix object... cin>>a; // Input the matrix like you do with any other integer, character, etc... cout<<2*a; //Print scalar multiples of a matrix as easily as this... Matrix b; cin>>b; cout<<a+b<<"\n"<<a*b<<endl; //Print sum and products of matrices easily...
18th Jun 2017, 4:16 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar