卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章16333本站已运行3317

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

程序描述

以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("

"); printf("Please enter the number of columns for the Square matrix: "); scanf("%d", &cols); printf("

"); printf("Please enter the elements for the Matrix:

"); 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("

"); for(r = 0; r < rows; r++){ printf("%dt ", matrix[0][r]); } for (r=1,c=cols-2;r<cols && c>=0;r++,c--){ printf("%dt ", matrix[r][c]); } for (r = 1; r < cols; r++){ printf("%dt ", matrix[cols-1][r]); } getch(); return 0; }

输出

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

卓越飞翔博客
上一篇: 分析Go语言网站访问速度优化的实战案例
下一篇: PHP中的php_strip_whitespace()函数
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏