简体   繁体   English

如何使用goto语句打印矩阵

[英]How to print a matrix using goto statement

How to print a matrix using only goto statement?如何仅使用 goto 语句打印矩阵?

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) } { (1, 2, 3), (4, 5, 6), (7, 8, 9) }

PS: Watch what you're doing. PS:看你在做什么。 Is that the way you declare a matrix?这是你声明矩阵的方式吗?

You have an additional statement int j = 0;你有一个额外的声明int j = 0; after printcolumn: , which resets j each loop.printcolumn: ,它会在每个循环中重置j It also shadows the former definition of j .它也掩盖了j的先前定义。

Remove it.去掉它。

You have small mistake there, you are clearing j to zero twice, remove the second one.你有一个小错误,你将j清零两次,删除第二个。

Also newline escape sequence is normal slash instead of the back one...换行符转义序列也是正常的斜线而不是后斜线...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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