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
{}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.char mat[n + 1][n + 2]; simbolo = n+2;and later assignmat[i][simbolo] = '*';you are writing out of bounds of the array. The indexes for each row only go up tosimbolo-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.