注册 登录
编程论坛 C++教室

cin语句后面加cout<<endl;能换行不?矩阵相乘问题

寻找水的萍 发布于 2010-11-21 17:15, 3950 次点击
for(i=0; i<A_row;i++)
 {for(j=0;j<A_column;j++)
 cin>>A[i][j];
 cout<<endl;
 }
这个语句里的cout不能换行么?
下面是完整的程序,矩阵相乘,没错误,但运行时不是我想要的样子。
#include <iostream>
using namespace std;
int main()
{int A_row,A_column,B_row,B_column;
int A[5][5],B[5][5],C[5][5];
int i,j,k;
cout<<"please input A_row,A_column,B_row,B_column:";
cin>>A_row>>A_column>>B_row>>B_column;
if(A_column==B_row)
{cout<<"input matrix A:"<<endl;
 for(i=0; i<A_row;i++)
 {for(j=0;j<A_column;j++)
 cin>>A[i][j];
 cout<<endl;
 }
cout<<endl;
cout<<"iuput matrix B:"<<endl;
for(i=0;i<B_row;i++)
{for(j=0;j<B_column;j++)
cin>>B[i][j];
cout<<endl;
}
cout<<endl;
cout<<"matrix C:"<<endl;
for(i=0;i<A_row;i++)
{for(j=0;j<B_column;j++)
{for(k=0;k<A_row;k++)
C[i][j]+=A[i][k]*B[k][j];
cout<<C[i][j]<<" ";}
cout<<endl;
}
}
else cout<<"error!";
return 0;
}
我不知道该怎么修改,还是我这种做法本身就是错误的
18 回复
#2
玩出来的代码2010-11-21 17:47
可以,但没道理。
#3
shafeilong2010-11-21 18:12
C[5][5];米有初始化为0
#4
寻找水的萍2010-11-21 18:32
回复 2楼 玩出来的代码
那怎么样才有道理啊?
#5
寻找水的萍2010-11-21 18:33
回复 3楼 shafeilong
恩,这是个问题。
#6
玩出来的代码2010-11-21 18:48
你想要程序运行成什么样子?输入数据时可以手动输入回车,又为何让程序自己输入数据后就换行呢。
#7
寻找水的萍2010-11-21 19:05
回复 6楼 玩出来的代码
2 2 1
3 2 1
0 1 2
我想这么输入,以矩阵的形式,而不是一行
#8
玩出来的代码2010-11-21 19:19
你自己给自己找麻烦。你将cout<<endl;去掉,然后按照你在帖子上输入的那样在程序运行时就这样输入。
#9
pangding2010-11-21 19:27
嗯,其实 cin 不管你怎么输入,能读“行 * 列”个元素就行。
#10
寻找水的萍2010-11-21 21:06
回复 8楼 玩出来的代码
可是我今天运行时,一按enter计算机就说我程序错了
#11
寻找水的萍2010-11-21 21:07
回复 8楼 玩出来的代码
难道是那台电脑有问题?
#12
玩出来的代码2010-11-21 21:22
没问题的啊。这会有什么问题,你怎么会想到是电脑的问题呢。你用的什么编译器。也不该有问题的,你把那错误贴出来。
#13
寻找水的萍2010-11-21 21:34
回复 12楼 玩出来的代码
调试时没错误,但是输入时1 2 2 2 3 。。。不换行,一按回车就提示程序错误,也不换行
#14
玩出来的代码2010-11-21 21:47
你把错误提示贴出来。
#15
寻找水的萍2010-11-21 21:51
我不知道咋贴。。。明天上机运行看看吧
#16
寻找水的萍2010-11-21 21:51
回复 14楼 玩出来的代码
我不知道咋贴。。。明天上机运行看看吧
#17
寻找水的萍2010-11-22 14:47
#include <iostream>
using namespace std;
int main()
{int A_row,A_column,B_row,B_column;
int A[10][10],B[10][10];
int C[10][10]={0};
int i,j,k;
cout<<"please input A_row,A_column,B_row,B_column:";
cin>>A_row>>A_column>>B_row>>B_column;
if(A_column==B_row)
{cout<<"input matrix A:"<<endl;
 for(i=0; i<A_row;i++)
 {for(j=0;j<A_column;j++)
 cin>>A[i][j];
}
cout<<endl;
cout<<"iuput matrix B:"<<endl;
for(i=0;i<B_row;i++)
{for(j=0;j<B_column;j++)
cin>>B[i][j];
}
cout<<endl;
cout<<"matrix C:"<<endl;
for(i=0;i<A_row;i++)
{for(j=0;j<B_column;j++)
{for(k=0;k<A_column;k++)
C[i][j]+=A[i][k]*B[k][j];
cout<<C[i][j]<<" ";}
cout<<endl;
}
}
else cout<<"error!";
return 0;
}
这就可以了。虽然与我理想中的还有差距。
#18
玩出来的代码2010-11-22 15:35
还没到你理想的情况?你不是就是想要这种样子的,
#19
寻找水的萍2010-11-24 23:15
回复 18楼 玩出来的代码
嗯,不是这样的。
我这样的矩阵不能对齐,不好看,这点我现在知道该怎么做了。


        
1