Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 执行此程序时,我的4*4矩阵中的8会自动变为0_C_Multidimensional Array_Syntax_Puzzle - Fatal编程技术网

C 执行此程序时,我的4*4矩阵中的8会自动变为0

C 执行此程序时,我的4*4矩阵中的8会自动变为0,c,multidimensional-array,syntax,puzzle,C,Multidimensional Array,Syntax,Puzzle,这是我用已经给我们的初始配置进行编程的第一步。 在这里,我认为这个洞是数字0。 注意:我已经做了评论中给出的更改,但仍然存在两次打印的问题。 我修正了数组的边界,用相应的字符替换了ASCII值,并生成了函数使其更紧凑,但我不明白为什么要打印两次。。 再次感谢你的帮助 #include <stdio.h> #include <stdlib.h> void print(int x[][4], int, int); void func(int z[][4], int*, in

这是我用已经给我们的初始配置进行编程的第一步。 在这里,我认为这个洞是数字0。 注意:我已经做了评论中给出的更改,但仍然存在两次打印的问题。 我修正了数组的边界,用相应的字符替换了ASCII值,并生成了函数使其更紧凑,但我不明白为什么要打印两次。。 再次感谢你的帮助

#include <stdio.h>
#include <stdlib.h>

void print(int x[][4], int, int);
void func(int z[][4], int*, int, int*, int);

void main() {
    int a[4][4] = {  1,  4, 15,  7,
                     8, 10,  2, 11,
                    14,  3,  6, 13,
                    12,  9,  5,  0 };
    int b[4][4];
    int n, temp, i, j, x_coord = 3, y_coord = 3, count = 0;

    while (count != 15) {
        print(a, 4, 4);
        count = 0;

        n = getch();
        if (n == 'H') {
            if (x_coord != 3)
                func(a, &x_coord, 1, &y_coord, 0);
            else
                printf("\nWrong move!\n");
        }
        if (n == 'P') {
            if (x_coord != 0)
                func(a, &x_coord, -1, &y_coord, 0);
            else
                printf("\nWrong move!\n");
        }
        if (n == 'K') {
            if (y_coord != 3)
                func(a, &x_coord, 0, &y_coord, 1);
            else
                printf("\nWrong move!\n");  
        }
        if (n == 'M') {
            if (y_coord != 0)
                func(a, &x_coord, 0, &y_coord, -1);
            else
                printf("\nWrong move!\n");
        }
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                b[i][j] = a[i][j];
            }
        }
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                if (b[i][j] == 0) {
                    temp = b[i][j];
                    b[i][j] = b[3][3];
                    b[3][3] = temp;
                }
            }
        }
        for (i = 0; i < 4; i++) {
            for (j = 0; j < 4; j++) {
                if (i == 4 && j == 4)
                    continue;
                if (b[i][j] == 4 * i + j + 1)
                    count++;
            }
        }
    }
}

void print(int x[][4], int r, int c) {
    int i, j;
    for (i = 0; i < r; i++) {
        for (j = 0; j < c; j++)
            printf("%d      ", x[i][j]);
        printf("\n\n");
    }
}

void func(int z[][4], int *x, int x1, int *y, int y1) {
    z[*x][*y] = z[*x + x1][*y + y1];
    z[*x + x1][*y + y1] = 0;
    *x = *x + x1;
    *y = *y + y1;
}
#包括
#包括
无效打印(整数x[][4],整数,整数);
void func(int z[][4],int*,int,int*,int);
void main(){
int a[4][4]={1,4,15,7,
8, 10,  2, 11,
14,  3,  6, 13,
12,  9,  5,  0 };
int b[4][4];;
int n,temp,i,j,x_坐标=3,y_坐标=3,计数=0;
而(计数!=15){
印刷品(a、4、4);
计数=0;
n=getch();
如果(n='H'){
如果(x_坐标!=3)
func(a,&x_-coord,1,&y_-coord,0);
其他的
printf(“\n移动!\n”);
}
如果(n='P'){
如果(x_坐标!=0)
func(a,&x_-coord,-1,&y_-coord,0);
其他的
printf(“\n移动!\n”);
}
如果(n='K'){
如果(y_坐标!=3)
func(a,x,0,y,1);
其他的
printf(“\n移动!\n”);
}
如果(n='M'){
如果(y_坐标!=0)
func(a,&x_-coord,0,&y_-coord,-1);
其他的
printf(“\n移动!\n”);
}
对于(i=0;i<4;i++){
对于(j=0;j<4;j++){
b[i][j]=a[i][j];
}
}
对于(i=0;i<4;i++){
对于(j=0;j<4;j++){
如果(b[i][j]==0){
温度=b[i][j];
b[i][j]=b[3][3];
b[3][3]=温度;
}
}
}
对于(i=0;i<4;i++){
对于(j=0;j<4;j++){
如果(i==4&&j==4)
继续;
如果(b[i][j]==4*i+j+1)
计数++;
}
}
}
}
无效打印(整数x[][4],整数r,整数c){
int i,j;
对于(i=0;i
请将ASCII码替换为字符,例如,
72
应为
'H'
。箭头键发送两个字符,转义符后跟一个字母。在每个字符后重新打印电路板。您应该将数组打印代码放入一个函数中,或者只需将其放在循环的顶部一次(但函数可能是更好的选择)。有一个记忆法:干-不要重复你自己。避免多次编写同一代码。改用函数。如果第一个条目是
K
,您将访问数组外部的
a[3][4]
。你需要检查边界。
b[i][j]=b[4][4]正在访问阵列外部。每个索引的最大值为3。请将ASCII码替换为字符,例如,
72
应为
'H'
。箭头键发送两个字符,转义符后跟一个字母。在每个字符后重新打印电路板。您应该将数组打印代码放入一个函数中,或者只需将其放在循环的顶部一次(但函数可能是更好的选择)。有一个记忆法:干-不要重复你自己。避免多次编写同一代码。改用函数。如果第一个条目是
K
,您将访问数组外部的
a[3][4]
。你需要检查边界。
b[i][j]=b[4][4]正在访问阵列外部。每个指标的最大值为3。