Hi all, i am new is C++.
i want to create a global 2D matrix and here is the code is use:
i am doing something wrong but i have no idea how to correct my mistake.Code:#include <iostream> #include <vector> using namespace std; vector<vector<int> >mat; // 2D matrix void func(void){ for(int i=0; i<5; i++) for(int j=0; j<5; j++) mat[i][j] = 0; } int main(void){ int size_x = 5; // X_dimension int size_y = 5; // Y_dimension mat.resize(size_x, vector<string>(size_y)); //resize 2D matrix for(int i=0; i<size_x; i++) for(int j=0; j<size_y; j++) mat[i][j] = i; //print matrix for(int i=0; i<size_x; i++){ cout << "\n"; for(int j=0; j<size_y; j++){ cout << mat[i][j] << " "; } } func(); // create a zero matrix //print zero matrix cout << "\n\n"; for(int i=0; i<size_x; i++){ cout << "\n"; for(int j=0; j<size_y; j++){ cout << mat[i][j] << " "; } } cout << "\n\n"; system("PAUSE"); return 0; }
Any help will be appreciated.




Reply With Quote