1

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

3
  • Please don't post images of text output; embed the data directly in the question. We should not have to go offsite to gaze at the screen colour schemes you like and we dislike. Copy'n'paste the material from your screen into the edit box. Then select that output and use the {} button above the edit box to indent it as 'code'. If you feel like being fancy, add <!-- language: lang-none --> as an HTML comment on a line with blank lines above and below before the displayed data. Commented May 14, 2017 at 22:11
  • 1
    Since you define char mat[n + 1][n + 2]; simbolo = n+2; and later assign mat[i][simbolo] = '*'; you are writing out of bounds of the array. The indexes for each row only go up to simbolo-1. After that, all bets about what happens are off, but you might get a * at the start of the next line of the array, if there is a next line for the array. Commented May 14, 2017 at 22:12
  • Thanks Jonathan for the tip, I've just noticed the embarrassing mistake ... and thanks also for the advice regarding the right way to ask questions and post images; sorry for the disguise, it wasn't intentional Commented May 14, 2017 at 22:31

3 Answers 3

1

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

The three assignments mat[n - 3][0] = '*'; mat[n - 2][1] = '*'; mat[n - 1][2] = '*'; could be done once, outside the loops. They don't depend on the loop variables. You could then consider memset(mat, ' ', sizeof(mat)); to set the matrix to all blanks, or you can leave the loops and put the 3 assignments after the loops. You could use printf("%.*s\n", n+2, mat[x]); to replace the 4 lines of printing (3 lines of loop plus 1 line after the loop). I prefer putchar('\n'); to printf("\n"); but I'm old fashioned.
@JonathanLeffler I agree with you. There is nothing wrong with old fashioned. Perhaps if i remove the for loop, it is printf("%.*s\n", n+2, mat[i]); ? I'll edit. Would you consider upwoting?
0

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

Comments

0

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;

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.