Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> การเขียนโปรแกรม C

พิมพ์เมทริกซ์ในรูปแบบแนวทแยง (Diagonal Pattern)

ได้รับอาร์เรย์ 2D ขนาด n*n ต้องหาการจัดเรียงแบบ antispiral ของเมทริกซ์ที่กำหนด

Input: arr[4][4] = {1,2,3,4, 5,6,7,8, 9,10,11,12, 13,14,15,16}
Output: 1 6 11 16 4 7 10 13

อัลกอริทึม

START
Step 1 -> declare start variables as r=4, c=4, i and j
Step 2 -> initialize array as mat[r][c] with elements
Step 3 -> Loop For i=0 and i print \n
Step 5 -> Loop For i=0 and i

ตัวอย่างโค้ด (C++)

#include <iostream>
#include <bits/stdc++.h>
using namespace std;

int main() {
    int R=4, C=4, i, j;
    int mat[R][C] = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}};
    for (i=0; i

ผลลัพธ์

1 6 11 16
4 7 10 13