user3388579
user3388579

Reputation: 33

Printing out a 2d array in a specific format in C

So basically when i print my array it looks like this:

      P00       P01       P02      
      P10       P11       P12       
      P20       P21       P22 

I want it to look like this:

             M[0][0]   M[0][1]   M[0][2]   
      M[0][0] P00       P01       P02       
      M[1][0] P10       P11       P12      
      M[2][0] P20       P21       P22      

My code:

 for (z=0; z<N; z++){

              for (c=0; c<N;c++){
                printf("\t%p", &M[z][c]);
    }
    printf("\n");
          }

Note that Pxx is the pointer address

Upvotes: 1

Views: 55

Answers (2)

Vinod
Vinod

Reputation: 294

above outer loop :

printf("\t\tM[0][0]\tM[0][1]\tM[0][2]\n");

and then just below outer loop :

printf("M[%d][0]\t",i);

Look like this:

printf("\t\t");

for (z=0; z<N; z++)
  printf("\tM[0][%d]",z);

printf("\n");

for ( z=0; z<N; z++){
   printf("M[%d][0]\t",z);
   for(c=0; c<N; c++){
      printf("\t%p",&M[z][c]);
   }
   printf("\n");
 }

Upvotes: 1

orestiss
orestiss

Reputation: 2283

Maybe something like this:

for(i=0;i<N;i++){
        printf("\tM[0][%d]",i);
    }        
    printf("\n");
for (z=0; z<N; z++){
    printf("M[%d][0]",z);
    for (c=0; c<N;c++){

        printf(" %p\t", &M[z][c]);
    }
printf("\n");
}

Upvotes: 0

Related Questions