首頁  >  文章  >  後端開發  >  在C語言中寫一個程序,印出以Z形狀排列的平方矩陣

在C語言中寫一個程序,印出以Z形狀排列的平方矩陣

WBOY
WBOY轉載
2023-08-27 10:57:171221瀏覽

程式描述

以Z形式列印平方矩陣的元素

一個方陣是行數和列數相同的矩陣。一個n×n的矩陣稱為n階方陣 

演算法

在C語言中寫一個程序,印出以Z形狀排列的平方矩陣

#
To print the elements of the Square Matrix in Z form
We need to print the first row of matrix then diagonal and then last row of the square matrix.

Example

的中文翻譯為:

範例

/* Program to print a square matrix in Z form */
#include<stdio.h>
int main(){
   int rows, cols, r, c, matrix[10][10];
   clrscr(); /*Clears the Screen*/
   printf("Please enter the number of rows for the Square matrix: ");
   scanf("%d", &rows);
   printf("</p><p>");
   printf("Please enter the number of columns for the Square matrix: ");
   scanf("%d", &cols);
   printf("</p><p>");
   printf("Please enter the elements for the Matrix: </p><p>");
   for(r = 0; r < rows; r++){
      for(c = 0;c < cols;c++){
         scanf("%d", &matrix[r][c]);
      }
   }
   printf("The Z Form Elements in the Square Matrix are: ");
   printf("</p><p>");
   for(r = 0; r < rows; r++){
      printf("%d\t ", matrix[0][r]);
   }
   for (r=1,c=cols-2;r<cols && c>=0;r++,c--){
      printf("%d\t ", matrix[r][c]);
   }
   for (r = 1; r < cols; r++){
      printf("%d\t ", matrix[cols-1][r]);
   }
   getch();
   return 0;
}

輸出

在C語言中寫一個程序,印出以Z形狀排列的平方矩陣

以上是在C語言中寫一個程序,印出以Z形狀排列的平方矩陣的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除