使用 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 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告
© . All rights reserved.