Matrix Input Redirection

Hi Guys,

I am having trouble with a Matrix problem. I want to only specify the amount of columns before asking the user to input their vectors, and using input redirection to read the values in.

Example:

3 (Columns)
1 2 0
1 5 0
1 4 4

What should my loop look like and how would I store them in their respective places, I am quite confused and new to arrays in general. I know how to do this if I can ask for both columns and rows, but this is more complicated.
You can use two for loops
1
2
3
4
5
for(int i = 0; i < numOfRows; i++){
   for(int j = 0; j < numOfColumns; j++){
   // Fill in the row
   }
}
vector<int> v;
int numrows = 0;
int x;

while(not done reading columns) //check after reading # columns each time
{
cin >> x;
v.push_back(x);

//after reading # columns each time:
numrows++;
}

v[desired_row*numcols+desired_col]; //2d access into a 1d array :)


This will let you make any # of rows the user desires without asking for anything more than the cols.
Last edited on
Topic archived. No new replies allowed.