please help me. my program is working but not displaying the table but i followed the syntax for the two-dimensional array. how come the program does not display the table? here is the code. :D

#include<stdio.h>
#include<conio.h>

main()
{
      int x[5][5],a,j;
      for (a=0;a<=5;a++)
      for (j=0;j<5;j++)
      x[a][j]=(a*4)+j+1;
      
      for (a=0;a<=5;a++)
      for (j=0;j<5;j++)
      printf("%d\n",x[a][j]);
      getch();
      }

When a=5, you are trying to read and write x[5][0]. This is not your memory and your code segfaults.


Change for (a=0;a<=5;a++) to for (a=0;a<5;a++)

in both sets of loops.

When you ran your code, it should have crashed and you should have seen an error message indicating that there was a segFault.

http://ideone.com/sonvy

Edited 5 Years Ago by Moschops: n/a

i have a question sir/ma'am. My professor wants that the output should be in a table and in a sequence. how can i do that? or that it is not possible?

Edited 5 Years Ago by fullarmorzz: n/a

Change

printf("%d\n",x[a][j]);

to

printf("%d ",x[a][j]);

and only printf the newline when the value of a changes.

i have a question sir/ma'am. My professor wants that the output should be in a table and in a sequence. how can i do that? or that it is not possible?

You mean like this :

for (a=0;a<5;a++){
    for (j=0;j<5;j++)
        printf("%d\t",x[a][j]);
    printf("\n");
}

Edited 5 Years Ago by D33wakar: n/a

This question has already been answered. Start a new discussion instead.