user5985960
user5985960

Reputation:

C: 2d char array issue

i've a problem with an exercise. The purpose is to print a specific picture after an int input (It is assumed that the input is > 3).

Example:

https://i.sstatic.net/KRl0R.png

This is my code:

#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;

}

The output isn't correct, it's added an extra '*' in mat[1][0]:

https://i.sstatic.net/4Z5Fl.png

Thanks in advance for you help

Upvotes: 1

Views: 85

Answers (3)

sstefan
sstefan

Reputation: 395

According to image, you want your output to have n rows, not n+1. This works correctly:

#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;
}

Upvotes: 1

Absalon Casta&#241;on
Absalon Casta&#241;on

Reputation: 369

I just changed

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

for

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

I also changed the spaces for '-' to make the output clearer.

The problem is the one mentioned in the comments: your indices were off. Remember, in C, indexing is 0 based, so the last element of any array has index n-1. Usually, in C when you go further the pointer just goes back to the beginning.


#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;

}

Upvotes: 0

rationalcoder
rationalcoder

Reputation: 1687

Some other people have already pointed out the issue, so I thought I would just post a cleaned up version for your reference:

#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;
}

Upvotes: 0

Related Questions