C:2d char数组问题

时间:2017-05-14 21:49:43

标签: c

我的运动有问题。目的是在int输入后打印特定图片(假设输入为> 3)。

示例:

https://i.stack.imgur.com/KRl0R.png

这是我的代码:

#include <stdio.h>

int main(void){

int i, j, n, simbolo;   
printf("Inserire un numero: ");
scanf("%d", &n);    

char mat[n + 1][n + 2];
simbolo = n+2;

//initialization with blank space and setting 3 * diagonal
for(i = 0; i < n + 1; i ++){
    for(j = 0; j < n + 2; j++){
        mat[i][j] = ' ';
        mat[n - 2][0] = '*';
        mat[n - 1][1] = '*';
        mat[n][2] = '*';
    }
}

//Add * diagonal of n length
for(i = 0; i < n + 1; i++){
    mat[i][simbolo] = '*';
    for(int x = i, y = 0; y < n + 2; y++){ //Print current line
        printf("%c", mat[x][y]);
    }   
    printf("\n");
    simbolo--;
}

return 0;

}

输出不正确,它增加了额外的&#39; *&#39;在mat [1] [0]中:

https://i.stack.imgur.com/4Z5Fl.png

先谢谢你的帮助

3 个答案:

答案 0 :(得分:1)

根据图片,您希望输出包含n行,而不是n+1。这可以正常工作:

#include <stdio.h>

int main(void){

    int i, j, n, simbolo;   
    printf("Inserire un numero: ");
    scanf("%d", &n);    

    char mat[n][n + 2];
    simbolo = n+1;

    //initialization with blank space and setting 3 * diagonal
    for(i = 0; i < n; i ++){
        for(j = 0; j < n + 2; j++){
            mat[i][j] = ' ';
        }
    }

    mat[n - 3][0] = '*';
    mat[n - 2][1] = '*';
    mat[n - 1][2] = '*';

    //Add * diagonal of n length
    for(i = 0; i < n; i++){
        if(i<(n-1))
            mat[i][simbolo] = '*';
        printf("%.*s\n", n+2, mat[i]);
        simbolo--;
    }

    return 0;
}

答案 1 :(得分:0)

我刚刚改变了

mat[i][simbolo] = '*'; 

mat[i+1][simbolo-1] = '*';

我还更改了“ - ”的空格以使输出更清晰。

问题是评论中提到的问题:你的指数已经关闭。 请记住,在C中,索引是基于0的,因此任何数组的最后一个元素都具有索引n-1通常,在C中,当你走得更远时,指针才会回到开头。

#include <stdio.h>

int main(void){

int i, j, n, simbolo;   
printf("Inserire un numero: ");
scanf("%d", &n);    

char mat[n + 1][n + 2];
simbolo = n+2;
//printf("Simbolo: %d",simbolo);
//initialization with blank space and setting 3 * diagonal
for(i = 0; i < n + 1; i ++){
    for(j = 0; j < n + 2; j++){
        mat[i][j] = '-';
        mat[n - 2][0] = '*';
        mat[n - 1][1] = '*';
        mat[n][2] = '*';
    }
}

//Add * diagonal of n length
for(i = 0; i < n+1 ; i++){
    mat[i+1][simbolo-1] = '*';
    for(int y = 0; y < n + 2; y++){ //Print current line
        //printf(",x< %d, y: %d",i,y);
        printf("%c", mat[i][y]);
    }   
    printf("\n");
    //printf("Simbolo: %d",simbolo);

    simbolo--;
}

return 0;

}

答案 2 :(得分:0)

其他一些人已经指出了这个问题,所以我想我会发布一个清理过的版本供你参考:

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

int main(void)
{
    printf("n: ");

    int n = 0;
    if (scanf("%d", &n) != 1) {
        fprintf(stderr, "ERROR: couldn't read 'n'\n");
        return EXIT_FAILURE;
    }

    char mat[n][n + 2];

    // Initialize with spaces.
    int i;
    int j;
    for (i = 0; i < n; i++)
        for (j = 0; j < n + 2; j++)
            mat[i][j] = ' ';

    // Draw the first 2 * on the left.
    mat[n - 3][0] = '*';
    mat[n - 2][1] = '*';

    // Draw the diagonal on the right.
    j = n + 1;
    for (i = 0; j >= 2; i++, j--)
        mat[i][j] = '*';

    // Print out the result.
    for (i = 0; i < n; i++) {
        for (j = 0; j < n + 2; j++)
            printf("%c", mat[i][j]);
        printf("\n");
    }

    return EXIT_SUCCESS;
}