ด้วยอาร์เรย์ขนาด 2X2 และความท้าทายคือการพิมพ์ผลรวมขององค์ประกอบมุมทั้งหมดที่จัดเก็บไว้ในอาร์เรย์
สมมติว่าเมทริกซ์ mat[r][c] โดยมีแถว "r" และคอลัมน์ "c" เริ่มต้นแถวและคอลัมน์จาก 0 จากนั้นองค์ประกอบมุมจะเป็น เสื่อ[0][0], เสื่อ[0][c-1], เสื่อ[r-1][0], เสื่อ[r-1][c-1] ตอนนี้งานคือการรับองค์ประกอบมุมเหล่านี้และรวมองค์ประกอบมุมเหล่านั้นเช่น mat[0][0] + mat[0][c-1] + mat[r-1][0] + mat[r-1] [c-1] แล้วพิมพ์ผลลัพธ์บนหน้าจอ
ตัวอย่าง
Input: Enter the matrix elements : 10 2 10 2 3 4 10 4 10 Output: sum of matrix is : 40

อัลกอริทึม
START Step 1-> create macro for rows and column as #define row 3 and #define col 3 Step 2 -> main() Declare int sum=0 and array as a[row][col] and variables int i,j,n Loop For i=0 and i<3 and i++ Loop For j=0 and j<3 and j++ Input a[i][j] End End Print [0][0] + a[0][row-1] +a[col-1][0] + a[col-1][row-1] STOP
ตัวอย่าง
#include<stdio.h>
#define row 3
#define col 3
int main(){
int sum=0,a[row][col],i,j,n;
printf("Enter the matrix elements : ");
for(i=0;i<3;i++){
for(j=0;j<3;j++){
scanf("%d",&a[i][j]);
}
}
printf("sum of matrix is : %d",a[0][0] + a[0][row-1] +a[col-1][0] + a[col-1][row-1] );
return 0;
} ผลลัพธ์
หากเรารันโปรแกรมด้านบน มันจะสร้างผลลัพธ์ดังต่อไปนี้
Enter the matrix elements : 10 2 10 2 3 4 10 4 10 sum of matrix is : 40