【发布时间】:2021-12-30 23:01:41
【问题描述】:
所以基本上我正在尝试创建一个用随机数填充矩阵的循环。我需要使每一列都有不同的范围,对它来说是独一无二的。
//Variables
int lsx = 3;
int lsy = 10;
int lust[lsy][lsx];
int i = 0;
int l = 0;
int shpp = 7;
//List setup
for (i = 0; i < lsy; i++)
{
for (l = 0; l < lsx; l++)
{
lust[i][l] = 0;
}
}
while (true)
{
//List generator
for (i = 0; i < lsy; i++)
{
for (l = 0; l < lsx; l++)
{
//Column 1
if (i == 0)
{
lust[i][l] = rand() % flx;
cout << lust[i][l] << '\n';
}
//Column 2
if (i == 1)
{
lust[i][l] = rand() % fly;
cout << lust[i][l] << '\n';
}
//Column 3
if (i == 2)
{
lust[i][l] = rand() % shpp;
cout << lust[i][l] << '\n';
}
}
cout << "Endline reached! \n \n";
}
for (i = 0; i < lsy; i++)
{
for (l = 0; l < lsx; l++)
{
cout << lust[i][l] << " ";
}
cout << "\n";
}
}
}
这只会生成 3 行。有没有人知道为什么会发生这种情况? 我尝试改变一些东西,但只得到了更奇怪的结果,无法完全填充数组This is what the program displays when I try and run it
【问题讨论】:
标签: c++ for-loop if-statement multidimensional-array while-loop