函数在我的矩阵中自动分配值
Function auto-assigns values in my matrix
我正在尝试制作像2048这样的游戏,但我遇到了一个问题。
#include <iostream>
#include <conio.h>
#include <windows.h>
#include <time.h>
using namespace std;
void random();
void randomCoord();
int gt[3][3];
int r,coordi,coordj;
void initiate()
{
for(int i=0;i<=3;i++)
for(int j=0;j<=3;j++)
gt[i][j]=0;
random();
// randomCoord();
// gt[coordi][coordj]=r;
// randomN();
// randomCoord();
// gt[coordi][coordj]=r;
for(int i=0;i<=3;i++)
{
cout<<endl;
for(int j=0;j<=3;j++)
cout<<gt[i][j]<<" ";
}
}
void random()
{
srand (time(NULL));
do
{
r = rand() % 4 + 2;
if(r==2 || r==4)
break;
}
while(1);
}
void randomCoord()
{
srand (time(NULL));
coordi = rand() % 4;
coordj = rand() % 4;
}
int main()
{
initiate();
return 0;
}
我希望当我在屏幕上打印矩阵以获得充满零的矩阵时......但是由于某种原因,这些值gt[2][3]
,gt[3][0]
得到r
的值。我不明白原因。
感谢您的帮助!
如果你想用gt[2][3]
和gt[3][0]
写,你的gt
必须是
int gt[4][4];
如果您的gt
是gt[3][3]
,当您在位置3
(对于第一个或第二个索引(访问时,您(据我所知(处于未定义行为状态。
相关文章:
- 在c++中为我自己的基于指针的数组分配内存的正确方法
- 我可以为unordered_map分配特定的模组值吗?
- 为什么在为其分配编号后无法显示我的字符串值?
- 为什么 valgrind 报告两个内存分配,而我的代码只请求一个?
- 将静态 IP 分配给我的 ESP32 服务器后不再响应
- 为什么STL优先级队列错误地分配了我的类对象
- 我的 ArrayList 类重新分配中的分段错误
- 对我的 c++ 金字塔分配进行小调整?我应该怎么做?
- 我的移动分配操作程序重载的分段错误
- 我的程序运行良好,可以复制对象,但是当我使用复制分配(=)时,它仍然可以正常运行.为什么不给错误
- 为什么我不能使用已删除或私有析构函数分配类的数组?
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 为什么我的循环完整收集数据不在动态分配的数组中
- 每次我分配新的位置时,都会隐式地称为destuructor
- 函数在我的矩阵中自动分配值
- 为什么我的指针数组分配不起作用
- 如何动态分配我的数组,重新分配给两次,等等
- 当我释放由CFFI生成的DLL分配的char*时,为什么我的应用程序会崩溃
- 为什么在这种情况下不调用我的移动构造函数?(首先分配 r 值引用并创建对象)
- 将STL Deque重新分配我的元素(C )