C++ 二维数组
c++ 2 dimensional array
int main ()
{
int line,column,i,j;
int sortir = 1;
float nbr;
cout << "The number of column :";
cin >> column;
cout << "The number of lines: ";
cin >> line;
float matrice [column][line]; //creating my 2d array
for(i=0;i <= line;i++) //asking the user to put the numbers he wants in the 2d array
for(j=0;j <= column;j++){
cout << "Enter a number";
cin >> nbr;
matrice[j][i] = nbr;
}
system("PAUSE");
return 0;
}
假设我做了一个行 = 1 和列 = 1 的数组,这使得(内存区域)[0,0] [0,1] [1,0] [1,1]。假设用户输入以下数字:
[0,0]=1
[0,1]=2
[1,0]=3
[1,1]=4
当我想向用户显示他在程序结束时输入的内容时:区域 [0][1] 和 [1][0] 显示相同的数字?
cout << matrice[0][0] = 1
cout << matrice[0][1] = 3 <-- why the f***
cout << matrice[1][0] = 3 <--His my for loop good?
cout << matrice[1][1] = 4
您正在越界访问数组。你循环访问了太多元素。你需要
for(i=0;i < line;i++) {
for(j=0;j < column;j++){
除此之外,您的代码不符合标准C++,并且依赖于称为可变长度数组 (VLA) 的扩展。不能声明在运行时确定大小的自动存储阵列。
int i;
std::cin >> i;
int a[i]; // ERROR (non-standard VLA extension)
const int j = 42;
int b[j]; // OK
int c[42]; // OK
这不是有效的语法。 float matrice[column][line]
是非法的。 C++中不能有可变大小的数组。 编译器正在提供诸如扩展之类的扩展。
相关文章:
- 将值从二维数组输出到文本文件
- 在二维数组中查找最小值和最大值?
- 移动二维数组中的字符
- 如何正确填充在堆上分配的二维数组?
- 传递二维数组时出现问题
- 具有随机数的二维数组不会更改
- 如何在C++中获取二维数组中最少的一列数?
- 如何使用用户输入变量制作二维数组?
- C++ 中动态二维数组的访问冲突
- 遍历二维数组的所有子数组
- 将二维数组的所有元素插入到一维数组中
- 在函数中传递二维数组
- 为什么指针在对二维数组进行排序时无法正常工作?
- 创建 std::string 的二维数组的最佳做法
- 如何将文本文件读取到二维数组中并以 c++ 打印
- 元素在二维数组 c++ 中的出现次数
- 执行 c++ 二维数组
- C++ 中的二维数组初始化为一个值
- 在C++中初始化第一维大小未知的二维数组
- 如何在二维数组中存储值?