C++ 在c+中输入一个矩阵+;?
我只是C++ 在c+中输入一个矩阵+;?,c++,matrix,C++,Matrix,我只是C++的初学者,我想写一个程序,输入并显示顺序矩阵I*j。我已经写了以下程序,但它没有工作 请引导我 我认为可能是访问的方法不正确或诸如此类 节目如下: #include <iostream> using namespace std; int main() { int i = 0,j = 0; cout << "Enter no of rows of the matrix"; cin >> i; cout << "Ent
C++
的初学者,我想写一个程序,输入并显示顺序矩阵I*j
。我已经写了以下程序,但它没有工作
请引导我
我认为可能是访问的方法不正确或诸如此类
节目如下:
#include <iostream>
using namespace std;
int main() {
int i = 0,j = 0;
cout << "Enter no of rows of the matrix";
cin >> i;
cout << "Enter no of columns of the matrix";
cin >> j;
float l[i][j];
int p = 0, q = 0;
while (p < i) {
while (q < j) {
cout << "Enter the" << p + 1 << "*" << q + 1 << "entry";
cin >> l[p][q];
q = q + 1;
}
p = p + 1;
q = 0;
}
cout << l;
}
#包括
使用名称空间std;
int main(){
int i=0,j=0;
cout>i;
cout>j;
浮点数l[i][j];
int p=0,q=0;
而(p不能定义长度可变的数组。需要定义动态数组或std::vector
#include<vector>
std::vector<std::vector<int> > l(i, std::vector<int>(j, 0));
我重写了你的代码:
(代替C++在C++中使用新的,使用删除来释放内存)
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
int_tmain()
{
int row,col;
库特罗;
库特尔;
浮动**矩阵=新浮动*[行];
对于(int i=0;i
对于(未签名i=0;i请不要使用代码> STD::vector < /COD>。@ N.M.OK,C++问题有C++答案。我没有说不使用<代码> STD::向量< /代码>。如果可以,请始终使用它。应该是++X(不++i)。。请解释什么不起作用。如果您得到任何意外的输出或错误消息,请将它们复制并粘贴到您的问题中。我没有得到所需的输出。我想在输入后打印矩阵。这是输出0xbfa9f5b0
for(int x = 0; x < i; ++i){
for(int y = 0; y < j; ++y){
cout << l[x][y] << " "
}
cout << "\n";
}
#include "stdafx.h"
#include<iostream>
#include <conio.h>
using namespace std;
int _tmain()
{
int row,col;
cout<<"Enter no of rows of the matrix";
cin>>row;
cout<<"Enter no of columns of the matrix";
cin>>col;
float** matrix = new float*[row];
for(int i = 0; i < row; ++i)
matrix[i] = new float[col];
int p=0,q=0;
for(unsigned i=0;i<row;i++) {
for(unsigned j=0;j<col;j++) {
cout<<"Enter the"<<i+1<<"*"<<j+1<<"entry";
cin>>matrix[i][j];
}
}
for(unsigned i=0;i<row;i++) {
for(unsigned j=0;j<col;j++) {
cout<<matrix[i][j]<<"\t";
}
cout<<endl;
}
getch();
return 0;
}