Answers for "how to declare a matrix in cpp"

C++
2

c++ code to write 2d array

#include <iostream>
using namespace std;
int main(){
	int n,m;
	int a[n][m];
	cin >> n >>m;
	for ( int i=0; i<n; i++){
		for (int j=0; j<m; j++){
			cin >> a[i][j];
		}
	}
	
	for ( int x=0; x<n; x++){
		for (int y=0; y<m; y++){
			cout << "a[" << x << "][" << y << "]: ";
			cout << a[x][y] << endl;
		}
	}
	return 0;
}
Posted by: Guest on August-11-2020
0

create matrix cpp

typedef std::vector<std::vector<double> > Matrix;
//with initialization
Matrix matrix1 = { {0.1,1.1,.2},
                 {.4,.5,.6}, 
                 {.8,.9,.10}
                };
//just initiation (3x3)
Matrix matrix2(3, std::vector<double>(3) );
Posted by: Guest on February-12-2021

Browse Popular Code Answers by Language