C ++代码,使用户在多维数组中输入行和列
c++ code which makes the user input the rows and columns in a multidimensional array
im 制作一个程序,显示一个乘法表,用户提示行数和列数,该程序显示表但列数和行数应该相同,如果我输入不同的数字,就会发生错误。
#include <iostream>
using namespace std;
int main()
{
int r,c;
cout<<"How many rows?: ";
cin>>r;
cout<<"How many Columns?: ";
cin>>c;
int table[r][c];
//assigns each element
for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= c; j++)
{
table[i][j] = i * j;
}
}
//prints the table
for (int i = 1; i <= r; i++)
{
for (int j = 1; j <= c; j++)
{
cout << table[i][j] << 't';
}
}
system("pause");
return 0;
}
数组从索引0
开始,如果数组大小r
arr[r]
则无法访问。所以你需要做:
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
table[i][j] = i * j;
}
}
//prints the table
for (int i = 0; i < r; i++)
{
for (int j = 0; j < c; j++)
{
cout << table[i][j] << 't';
}
}
嗨,
就像乔纳森波特说数组在 C/C++ 中从 0 开始。加:
int r,c;
cout<<"How many rows?: ";
cin>>r;
cout<<"How many Columns?: ";
cin>>c;
int table[r][c];
这是一个非常糟糕的做法,你应该避免。实际上,您正在使用非静态变量创建表。
这个问题在这里已经回答了
相关文章:
- 检查TCHAR数组输入是否为带符号整数C++
- C++数组输入不接受一定数量的整数
- 为什么我的代码无法处理大型数组输入(>10000)?
- 我正在努力处理多维数组输入和输出,我管理输入,但是打印无法按计划工作
- 使用无符号字符数组输入十六进制数
- C++用用户输入在循环中填充 char 数组:输入整个字符串时到底发生了什么?
- 如何从用户获取数组输入
- C 2x2数组输入线
- 字符数组输入限制
- 将字符串数组输入到函数C++
- C++:如何将普通话(字符串)分配给用户使用数组输入的数字 &运算符问题
- 数组排序、数组输入、数组输出
- 将字符数组输入stdin
- 如何在 c++ 中为对象数组输入字符串
- 如何在c++ mex文件中访问多维数组输入
- 读取文件并将其作为整型数组输入
- 从c++文件中获取输入(矩阵数组输入)
- 数组输入不工作
- 函数结构数组输入cin问题
- 结束数组输入(c++行业标准)