garbage value in C array -
i trying write c code print pyramid structure on screen, this.
the corresponding code i've written this.
#include <stdio.h> #include <stdlib.h> void printarrayfunc(char arr[9][5]) { int i,j; printf("=========================================\nprinting values\n"); (i = 0; i<5; i++) { (j = 0; j<9;j++) { //printf("arr[%d][%d] = %d\n", i,j, arr[i][j]); if (arr[i][j] == 1) printf("*"); else printf(" "); } printf("\n"); } } int main() { int i,j; char arr[9][5] = {0}; printf("============================\nfilling values\n") ; (i=0;i<5;i++) { (j= 4-i;j<=4+i;j++) { arr[i][j] = 1; // printf("arr[%d][%d]= %d\n",i,j,arr[i][j]); } //printf("\n"); } printarrayfunc(arr); return 0; }
it giving output like
i know i'm doing silly mistake @ moment, i'm not able find going wrong. let me hear comments on this.
in function argument:
char arr[9][5]
in loop:
for (i = 0; i<5; i++) { (j = 0; j<9;j++) { if (arr[i][j] == 1)
you flipped position of i
, j
. i
should go 0 9, j
0 5.
Comments
Post a Comment