使用数组创建矩阵
我制作了一个数组矩阵并用于循环,问题是它只显示我的最后一个输入。 请参阅下面的示例结果。
#include<iostream>
using namespace std;
int main()
{
int x = 0, y = 0;
int a[x][y], i, j;
cout<<"Enter number of Columns: ";
cin>>y;
cout<<"Enter number of Rows: ";
cin>>x;
cout<<endl;
for(i=0; i<x; i++)
{
cout<<endl;
cout<<"Enter values or row/s: ";
cout<<endl;
for(j=0; j<y; j++)
{
cout<<endl;
cout<<"Row "<< i+1 << ": ";
cin >> a[i][j];
}
}
cout<<endl;
cout<<"Entered Matrix is: ";
cout<<endl;
for(i=0; i<x; i++)
{
for(j=0; j<y; j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
示例结果:
输入列数:3
输入行数:2
输入值或行数:
第 1 行:-1
行1: -2
第 1 行:-3
输入值或行:
第 2 行:4
第 2 行:5
第 2 行:-6
输入的矩阵为:
4 5 -6
4 5 -6
I made an array matrix and used for loops, the problem is that it only displays my last input. Please refer to the sample result below.
#include<iostream>
using namespace std;
int main()
{
int x = 0, y = 0;
int a[x][y], i, j;
cout<<"Enter number of Columns: ";
cin>>y;
cout<<"Enter number of Rows: ";
cin>>x;
cout<<endl;
for(i=0; i<x; i++)
{
cout<<endl;
cout<<"Enter values or row/s: ";
cout<<endl;
for(j=0; j<y; j++)
{
cout<<endl;
cout<<"Row "<< i+1 << ": ";
cin >> a[i][j];
}
}
cout<<endl;
cout<<"Entered Matrix is: ";
cout<<endl;
for(i=0; i<x; i++)
{
for(j=0; j<y; j++)
cout<<a[i][j]<<" ";
cout<<endl;
}
}
SAMPLE RESULT:
Enter number of Columns: 3
Enter number of Rows: 2
Enter values or row/s:
Row 1: -1
Row 1: -2
Row 1: -3
Enter values or row/s:
Row 2: 4
Row 2: 5
Row 2: -6
Entered Matrix is:
4 5 -6
4 5 -6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
只需将数组的声明更改为这样:
int[x][y] 为 int[10][10]
Just change declaration of array like this:
int[x][y] to int[10][10]
这里的问题是您想要使用大小可变的数组。因此,输入“x”和“y”,然后将其用作数组大小。 (
array[x][y]
)这在 C++ 中不起作用。 C++ 中不存在所谓的动态 VLA(可变长度数组)。
有一些方言或编译器扩展可以处理 VLA,但 C++ 不允许 VLA。
这意味着,您不能编写如下内容:
这是无效的 C++ 代码。
C 样式数组必须具有编译时常量值。您可以使用
const
或constexpr
甚至像42
这样的数字文字来定义它。魔法常数为 42 或最差的解决方案。无论如何,你可以写This 在语法上是正确的。
但是,它不会帮助你。因为你需要一些“动态”的东西。 C++ 中惯用的正确方法是使用
std::vector
。请阅读此处了解相关内容。使用 2dstd::vector
的 C++ 解决方案将如下所示:但是,接下来,我们经常听到这样的说法:您还没有了解
std::vector
。然后,您需要使用使用new
和delete
来使用旧式内存分配的方法。这是一种不推荐的方法,但出于教学目的,它仍然经常使用。因此,您首先分配用于保存指向数组(行)的指针的内存,然后为行中的列分配内存。
这将如下所示:
正如所说,经常被教导,但强烈不鼓励。
The problem here is that you want to use arrays with a variable size. So, input "x" and "y" and then use this as the array size. (
array[x][y]
)This does not work in C++. There are no dynamic, so called VLAs (Variable Length Arrays) in C++.
There are some dialects or compiler extensions, which can handle VLA, but C++ does not allow VLAs.
This means, you cannot write somthing like:
This is invalid C++ code.
C-Style arrays must have a compile time constant value. You can define it with
const
orconstexpr
or even a number literal like42
. Magic constants liek 42 or the worst solution. Anyway, you could writeThis is syntactically correct.
But, it will not help you. Because you need something "dynamic". The idiomatic correct approach in C++ is to use a
std::vector
. Please read here about that. And using a 2dstd::vector
the C++ solution would look like that:But, next, we often hear the statement that you did not learn about
std::vector
yet. And then, you need to use the approach to use old style memory allocation usingnew
anddelete
. This is a non-recommend approach, but for teaching purposes, it is still often used.So, you first allocate memory that will hold pointers to
array
(for the rows) and then allocate memory for the columns in the rows.This would then look like the below:
As said, often teached, but strongly discouraged.