Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Input Two Dimensional Array

Status
Not open for further replies.

mwhamilton

Technical User
Jan 28, 2002
11
US
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 << &quot;Input the N x N dimension of the matrix: &quot;;
cin >> cols;
rows = cols;
cout << &quot;Input the N x N matrix by rows:&quot; << endl;
for(int i=0; i<rows; i++){
for(int j=0; j<cols; j++){
cin >> current_array[j];
}
}
}

Thanks so much!
 
so you might try to use this:

char *firstline = new char[10];
int i = 0;
int COLS = 0;
cin.geline( firstline, 11 ); // gets the first line of the matrix from the user

// counting the number of rows of the matrix
while( firstline != '\0' ) {
if( firstline == ' ' )
COLS++;

i++;
}
ROWS = COLS;
.....
...
delete firstline;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top