Matrix

a program that will input an n-by-n matrix A by rows from the keyboard in the following way. Each line should input a row of the matrix where (i) one or more spaces separates each row entry and (ii) a return character follows the last digit of the last entry of each row. size parameter for the matrix should be declared as
const int maximumSize = 10;
and the matrix should be declared as
float A[maximumSize][maximumSize];but the program should NOT prompt the user for the matrix size. The program should count how many columns the user enters before hitting "Enter" and how many rows.

this is the first part of my the first c++ program and i do not know how i do this part. Please help.

Last edited on
1
2
3
4
5
6
7
8
9
std::string line;
int row = 0, col = 0;
while( std::getline( std::cin, line ) ){
   col=0;
   std::istringstream input(line);
   while( input>>matrix[row][col] )
      ++col;
   ++row;
}
in line 5, there is an error like that: variable 'std::istringstream input' has initializer but incomplete type.

i do not understand exactly why this error occur. After yor answer i realize that i did not write: include the iostream and math libraries in the program.<sorry about that :( > Is it possible the reason of the error? Thank you, for your reply.
you need to #include <sstream>
i think i can only include iostream and math libraries. Is there a solution without <sstream> and only with iostream and math libraries? math is actually for the second part of the program, so only with iostream.
Last edited on
suppositions:
_ the matrix to be read is not empty
_ the input ends with a line break
1
2
3
4
5
6
7
8
9
10
11
int row=0, col=0;

do{
   col=0;
   while( std::cin.peek() not_eq '\n' ){ //you could also check for EOF here
      std::cin >> matrix[row][col];
      ++col;
   }
   ++row;
   std::cin.ignore(); //discard the '\n' in the input buffer
}while( std::cin.peek() not_eq EOF );
Last edited on
Again thank you for your attention. your suppositions are correct. But, is your usage of do...while loop correct? I ask this question as complier expect ';' after while( std::cin.peek() not_eq '\n' ). If ı put ';' after while, what happens line 6 and 7(i think you write line 6 and 7 in while)? Also what happens line 9 and 10?
Last edited on
I've got a type, it is corrected now.
ı really novice in c++, so i'm sorry, ı don't understand how it is corrected. :(
Thanks to your code, i did this:

while( row <= col ){

while( cin.peek() not_eq '\n' ){

cin >> A[row][col];
cin.ignore(24, '\n');
if(row == 0){
col++;
}
}
row++;
}

but in this, the number of row is two point more than the right, the number of col is one point more. for example for 3x3 matrix, col = 3; row = 4. Where did i mistake?
I do not understand your code, try to explain it.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
int main(){
	const int maximumSize = 10;
	float matrix[maximumSize][maximumSize];

	int row=0, col=0;

	do{
		col=0;
		while( std::cin.peek() not_eq '\n' ){ //you could also check for EOF here
			std::cin >> matrix[row][col];
			++col;
		}
		++row;
		std::cin.ignore(); //discard the '\n' in the input buffer
	}while( std::cin.peek() not_eq EOF );

	std::cout << "rows: " << row << '\n';
	std::cout << "cols: " << col << '\n';
}
input
1 2 3
4 5 6
7 8 9

output
rows: 3
cols: 3


However if the input were (note the extra line break)
1 2 3
4 5 6
7 8 9

output
rows: 4
cols: 0


to correct that, line 15 could be changed by
1
2
		while( isspace(std::cin.peek()) )
				std::cin.ignore(); //discard the '\n' in the input buffer 
The problem is solved whit this code:

int row = 0, col = 1;
const int maximumSize = 10;
float A[maximumSize][maximumSize];
int n, l;

while(row < col){
col=0;
while( cin.peek() not_eq '\n' ){
cin >> A[row][col];
col++;
}
cin.ignore();
++row;
}


Thank you so much for your time and attention.
Topic archived. No new replies allowed.