我无法弄清楚为什么我生成的随机数没有正确存储
I can't figure out why my random numbers I'm generating are not storing correctly
我正在制作一个程序,该程序为某人输入的数字生成随机x和y点。由于某种原因,我的随机数没有存储在数组中。我在生成点的循环中输出数组,然后在for循环中输出阵列,而y坐标则不相同:
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
srand((unsigned)time(0));
int mounds =0;
//prompt for number of mounds, uncertainty, and number of sites
cout <<"Please enter the number of mounds at the site: ";
cin >> mounds;
//declaration of an array that will hold the x and y for each mound
int moundArray [mounds][1];
for(int i =0; i < mounds; i++)
{
//generate a random x
moundArray [i][0] = rand()%1331;
cout <<"You just generated an x of: " << moundArray [i][0] << endl;
//sleep(1);
//generate a random y
moundArray [i][1] = rand()%1743;
cout <<"You just generated a y of: " << moundArray [i][1] << endl;
//sleep(1);
}
//for loop to display my results
for(int j =0; j < mounds; j++)
{
cout <<"random x: " << moundArray [j][0] << endl;
cout <<"random y: " << moundArray [j][1] << endl;
}
return 0;
}
我很惊讶您没有在执行时崩溃。这条线在这里应该是您的问题:
int moundArray [mounds][1];
如果第二个索引的大小为[1],则元素[0]是唯一可以访问的有效元素。尝试将其增加到2。
int moundArray [mounds][1];
土丘和1表示元素数,而不是数组中的最后一个索引。
cout <<"You just generated a y of: " << moundArray [i][1] << endl;
上方您使用1个作为第二个字段中的索引,这意味着该数组在该维度中至少具有两个(2)个元素。
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 将字符串存储在c++中的稳定内存中
- 整数不会重复超过随机数
- std::原子加载和存储都需要吗
- C++:将控制台输出存储在宏中更好吗
- 使用QProcess执行命令,并将结果存储在QStringList中
- 访问存储在向量C++中的结构的多态成员
- 如何从存储在std::映射中的std::集中删除元素
- 存储模板类型以强制转换回派生<T>
- 类型总是使用其大小存储在内存中吗
- 当字符串存储在变量中时,如何将字符串转换为wchar_t
- 使用无符号字符数组有效存储内存
- 如何在cpp.中使用协议缓冲区存储大缓冲区/数组(char/int)
- 存储并重新加载随机数生成器状态
- 在 C++ 中生成和存储随机数组
- 我在一个生成随机数的程序工作,我想将这些数字存储在一个数组中
- 我无法弄清楚为什么我生成的随机数没有正确存储
- 如何将升压随机数生成器的状态存储为常量字符 *
- 如何在我存储的两个变量之间生成随机数
- 如何在c++中存储随机数