mwhamilton
Technical User
Hi,
I am trying to input a two dimensional array from the standard input. Basically, I have a two dimensional array allocated as
float array[max][max]; (it is guaranteed to be a square matrix less than 10x10). I want to input the floating point values from the stdin by line:
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
I can input this n x n matrix by asking the user for the dimensions of the array, but I am trying to avoid having to do that. I want to be able to cin the first line, and be able to determine the dimensions of the array, and input the rest of the values. Here is the code that I am currently using to input the coefficients(note that the arrays were allocated as globals):
void inputMatrix(){
cols=0;
rows=0;
cout << "Input the N x N dimension of the matrix: ";
cin >> cols;
rows = cols;
cout << "Input the N x N matrix by rows:" << endl;
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
cin >> current_array[j];
}
}
}
Thanks so much!
I am trying to input a two dimensional array from the standard input. Basically, I have a two dimensional array allocated as
float array[max][max]; (it is guaranteed to be a square matrix less than 10x10). I want to input the floating point values from the stdin by line:
11 12 13 14
21 22 23 24
31 32 33 34
41 42 43 44
I can input this n x n matrix by asking the user for the dimensions of the array, but I am trying to avoid having to do that. I want to be able to cin the first line, and be able to determine the dimensions of the array, and input the rest of the values. Here is the code that I am currently using to input the coefficients(note that the arrays were allocated as globals):
void inputMatrix(){
cols=0;
rows=0;
cout << "Input the N x N dimension of the matrix: ";
cin >> cols;
rows = cols;
cout << "Input the N x N matrix by rows:" << endl;
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
cin >> current_array[j];
}
}
}
Thanks so much!