首页  >  文章  >  后端开发  >  在C语言中编写一个程序,打印出以Z形状排列的平方矩阵

在C语言中编写一个程序,打印出以Z形状排列的平方矩阵

WBOY
WBOY转载
2023-08-27 10:57:171219浏览

程序描述

以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删除