C++ 如何阻止阵列在运行时重置?
我有一个2D数组,我正在使用它作为数独游戏的模板,它是一个9x9数组,我打算在获得用户输入后用数字填充它 到目前为止,我还没有尝试太多,因为我迷路了,没有找到有用的资源C++ 如何阻止阵列在运行时重置?,c++,arrays,C++,Arrays,我有一个2D数组,我正在使用它作为数独游戏的模板,它是一个9x9数组,我打算在获得用户输入后用数字填充它 到目前为止,我还没有尝试太多,因为我迷路了,没有找到有用的资源 #include <cstdlib> #include <iostream> #include <stdio.h> /* printf, scanf, puts, NULL */ #include <stdlib.h> /* srand, rand */ #inc
#include <cstdlib>
#include <iostream>
#include <stdio.h> /* printf, scanf, puts, NULL */
#include <stdlib.h> /* srand, rand */
#include <time.h> /* time */
#include <cstdio>
#include <cstring>
#include <fstream>
using namespace std;
#define N rand() % 10
/* #define N2 rand() % 10 */
int main(){
for (int i = 0; i < 10 ; i++){
srand (time(NULL));
int c1;
int c2;
int c3;
cout << "Insert number to fill: " ;
cin >> c3;
/*c1 = N ;
c2 = N ;*/
/*cin >> c1;
cin >> c2;*/
/* cout << N << '\n'; /* << N2 << '\n'; */
int sudoku[][9] = { {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 0, 0, 0, 0, 0, 0, 0, 0},};
int width = 9, height = 9;
sudoku[N][N] = c3;
cout << sudoku << '\n';
/* cout << rand()%10;*/
for (int i = 0; i < height; ++i)
{
for (int j = 0; j < width; ++j)
{
cout << sudoku[i][j] << ' ';
}
cout << endl;
}
}
return 0;
}
#包括
#包括
#include/*printf、scanf、put、NULL*/
#包括/*srand,兰特*/
#包括/*时间*/
#包括
#包括
#包括
使用名称空间std;
#定义N rand()%10
/*#定义N2 rand()%10*/
int main(){
对于(int i=0;i<10;i++){
srand(时间(空));
int c1;
int c2;
int c3;
cout>c3;
/*c1=N;
c2=N*/
/*cin>>c1;
cin>>c2*/
/*cout在for循环的每次迭代中,您都要重新初始化您的数独
数组,因此每次循环时,每个值都会再次设置为0。将初始化移到循环之外:
int sudoku[9][9] = { 0 };
for (int i = 0; i < 10 ; i++){
...
}
int数独[9][9]={0};
对于(int i=0;i<10;i++){
...
}
您可以使用一个零将2D数组中的所有值初始化为0(注意,这对非零初始值不起作用,请参阅以获取有关该值的信息)这显然是一个精简的数组,但它让人理解了这个想法。--不,它没有让人理解这个想法。你到底想做什么?用上一次运行的程序的结果启动一个程序?好了,我不认为我必须添加其余的代码,我错误地认为我的解释已经足够了。我是CPP的新手,还没有在我以前的CPP计划中有任何问题,所以请原谅我这次寻求帮助。谢谢,这很有效。CPP还是新手,所以我甚至没有想到这一点。