使用 C 编程语言打印 Z 字形矩阵元素的程序


程序说明

以 Z 字形打印矩阵元素

方阵是指行数和列数相等的矩阵。一个 N*N 矩阵被称为 N 阶方阵。 

算法

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.

示例

/* 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("%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; }

输出

更新时间:09-Jan-2020

589 次浏览

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告