简体   繁体   中英

How to print a matrix using goto statement

How to print a matrix using only goto statement?

I tried but my code is not working .

#include <stdio.h>

int main()
{
   //int i=0,j=0;
   int arr[3][3] = { (1, 2, 3), (4, 5, 6), (7, 8, 9) };

   printf("The matrix is");

   int i = 0;
   printrow:
   {
      int j = 0;
      printcolumn:
      {
         int j = 0;
         if (j < 3)
         {
            printf("%d", arr[i][j]);
            j++;
            goto printcolumn;
         }
         if (i < 2)
         {
            printf("/n");
            i++;
            goto printrow;
         }
      }
   }   

   return 0;
}

Here you go.

#include <stdio.h>

int main()
{
   //int i=0,j=0;
   int arr[3][3] = { {1, 2, 3}, {4, 5, 6}, {7, 8, 9} };

   printf("The matrix is\n");

   int i = 0;

   printrow:
   {
      int j = 0;
      printcolumn:
      {
         if (j < 3)
         {
            printf("%d ", arr[i][j]);
            j++;
            goto printcolumn;
         }
      }
      if (i < 2)
     {
        printf("\n");
        i++;
        goto printrow;
     }
   }   

   return 0;
}

{ (1, 2, 3), (4, 5, 6), (7, 8, 9) }

PS: Watch what you're doing. Is that the way you declare a matrix?

You have an additional statement int j = 0; after printcolumn: , which resets j each loop. It also shadows the former definition of j .

Remove it.

You have small mistake there, you are clearing j to zero twice, remove the second one.

Also newline escape sequence is normal slash instead of the back one...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM