What is following will surely appear very simple for c coders but I am coding a small program to modelize some game called gomoku. For the user, you have to enter an integer N wich corresponds to a 'N times N' square which consists of 'N times N' integers.
So the code is runnig quite well but I have some simple question : when I enter the 'N times N' integers, I made some
int N;
scanf("%d",&N);
char c[N][N];
while (i<N){
scanf("%s\n",&c[i]);
i++;
}
then I converted the char to int for each c[i]
to make some computation involving c[i][j]
, which is quite unnatural. But if I had to declare int c[N][N]
, it would be impossible to retrive the same integers c[i][j]
like those I inputed when the while-loop is running.
Does anyone has an idea to declare int c[N][N]
, inputing integers, and then computing the same when computing with integers c[i][j]
?
Best, Newben
You don't have to read char and then convert it to int. You can just simply read integeres:
for(int i = 0; i < N; ++i)
scanf("%d", &c[i]); //of course c has to be int** type
And are you sure that you want to read just N integers? Not N N for whole array? In case you want to read N N objects to array, code should look like this:
int N, i, j;
scanf("%d",&N);
int c[N][N];
for(i = 0; i < N; ++i)
{
for(j = 0; j < N; ++j)
{
scanf("%d", &c[i][j]);
/* do something */
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.