Computer >> Máy Tính >  >> Lập trình >> lập trình C

Chương trình In Ma trận Bình phương ở dạng Z trong C

Mô tả chương trình

In các phần tử của ma trận bình phương ở dạng Z

Ma trận vuông là ma trận có cùng số hàng và số cột. Ma trận n-x được gọi là ma trận vuông có bậc

Thuật toán

Chương trình In Ma trận Bình phương ở dạng Z trong C

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.

Ví dụ

/* 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("\n");
   printf("Please enter the number of columns for the Square matrix: ");
   scanf("%d", &cols);
   printf("\n");
   printf("Please enter the elements for the Matrix: \n");
   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("\n");
   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;
}

Đầu ra

Chương trình In Ma trận Bình phương ở dạng Z trong C