ด้วยขนาดอาร์เรย์ nxn โปรแกรมจะต้องพิมพ์องค์ประกอบของอาร์เรย์ในรูปแบบงูโดยไม่ทำการเปลี่ยนแปลงใดๆ กับตำแหน่งเดิม

ตัวอย่าง
Input: arr[]= 100 99 98 97 93 94 95 96 92 91 90 89 85 86 87 88 Output: 100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85
โปรแกรมจะสำรวจแต่ละแถวของเมทริกซ์และตรวจสอบแถวคู่หรือคี่
-
หากแถวเป็นเลขคู่ มันจะพิมพ์องค์ประกอบของแถวนั้นจากซ้ายไปขวา
-
หากแถวเป็นเลขคี่ มันจะพิมพ์องค์ประกอบของแถวนั้นจากขวาไปซ้าย
อัลกอริทึม
START Step 1 -> create header files for declaring rows and column let’s say of size 4x4 Step 2 -> declare initial variables i and j and array[][] with elements Step 3 -> Loop For i=0 and i<M and i++ IF i%2==0 Loop For j=0 and j<N and j++ Print arr[i][j] End End Else Loop For j=N-1 and j>=0 and j— Print arr[i][j] End End STOP
ตัวอย่าง
#include<stdio.h>
#define M 4
#define N 4
int main() {
int i,j;
int arr[M][N] = {
{ 100, 99, 98, 97 },
{ 93, 94, 95, 96 },
{ 92, 91, 90, 89 },
{ 85, 86, 87, 88 }
};
for (i = 0; i < M; i++) { //for rows
if (i % 2 == 0) {
for (j = 0; j < N; j++) // for column
printf("%d ",arr[i][j]);
} else{
for (j = N - 1; j >= 0; j--)
printf("%d ",arr[i][j]);
}
}
return 0;
} ผลลัพธ์
หากเรารันโปรแกรมข้างต้น มันจะสร้างผลลัพธ์ดังต่อไปนี้
100 99 98 97 96 95 94 93 92 91 90 89 88 87 86 85