簡體   English   中英

打印二維數組

[英]Printing a 2D array

當我在 main() 中調用此函數並將轉置矩陣作為參數傳遞時,它會拋出錯誤的值。

void mostrarMatriz(int matriz[filas][columnas], int filas, int columnas){
    int i, j;

    for(i = 0; i < filas; i++){
        for(j = 0; j < columnas; j++){
            printf("[%d]", matriz[i][j]);
        }
        printf("\n");
    }
}

我在打印功能中替換了 i,j

    #include <stdio.h>
    #define filas 3
    #define columnas 3

      void mostrarMatriz(int matriz[filas][columnas]){
          int i, j;
    
          for(i = 0; i < filas; i++){
          for(j = 0; j < columnas; j++){
              printf("[%d]", matriz[j][i]);
          }
          printf("\n");
      }
    }

    int main()
    {
      int matrix[filas][columnas];
      int i,j;
      int num=1;
      for(i=0;i<filas;i++)
      {
          for(j=0;j<columnas;j++)
          {
              matrix[i][j]=num;
              num=num+1;
          }
      }
      mostrarMatriz(matrix);
    
      return 0;
    }

這很好用,你可以在這里查看https://onlinegdb.com/HkHcNqOVv

輸出:

[1][4][7]

[2][5][8]

[3][6][9]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM