In: Computer Science
C++
If I want to ask the user to input the variables in a vector, how
can I do that?
I'm trying to implement this in a program with matrices. So the user should enter how many rows and columns they want, then enter the values
Can you help me out
#include <vector> #include <iostream> #include <string> using namespace std; int main() { int rows, columns, num; vector<vector<int> > matrix; // ask user for how many rows and columns cout << "Enter number of rows: "; cin >> rows; cout << "Enter number of columns: "; cin >> columns; // ask and read matrix for (int i = 0; i < rows; ++i) { vector<int> temp; for (int j = 0; j < columns; ++j) { cout << "Enter element at row " << i + 1 << " and columne " << j + 1 << ": "; cin >> num; temp.push_back(num); } matrix.push_back(temp); } // print matrix for (int i = 0; i < matrix.size(); ++i) { for (int j = 0; j < matrix[i].size(); ++j) { cout << matrix[i][j] << " "; } cout << endl; } cout << endl; return 0; }