使用 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;
}輸出

廣告
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
JavaScript
PHP