행렬이 주어지면 행렬의 경계 요소를 인쇄하고 합을 표시해야 합니다.
예시
아래 주어진 매트릭스를 참조하십시오 -
주어진 매트릭스
1 2 3 4 5 6 7 8 9
경계 매트릭스
1 2 3 4 6 7 8 9
경계 요소의 합:1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 =40
경계 행렬의 합을 찾는 논리 다음과 같습니다 -
for(i = 0; i<m; i++){
for(j = 0; j<n; j++){
if (i == 0 || j == 0 || i == n – 1 || j == n – 1){
printf("%d ", mat[i][j]);
sum = sum + mat[i][j];
}
else
printf(" ");
}
printf("\n");
} 프로그램
다음은 행렬의 경계 요소의 합을 인쇄하는 C 프로그램입니다. -
#include<stdio.h>
#include<limits.h>
int main(){
int m, n, sum = 0;
printf("\nEnter the order of the matrix : ");
scanf("%d %d",&m,&n);
int i, j;
int mat[m][n];
printf("\nInput the matrix elements\n");
for(i = 0; i<m; i++){
for(j = 0; j<n; j++)
scanf("%d",&mat[i][j]);
}
printf("\nBoundary Matrix\n");
for(i = 0; i<m; i++){
for(j = 0; j<n; j++){
if (i == 0 || j == 0 || i == n – 1 || j == n – 1){
printf("%d ", mat[i][j]);
sum = sum + mat[i][j];
}
else
printf(" ");
}
printf("\n");
}
printf("\nSum of boundary is %d", sum);
} 출력
위의 프로그램이 실행되면 다음과 같은 결과가 생성됩니다 -
Enter the order of the matrix : 3 3 Input the matrix elements : 1 2 3 4 5 6 7 8 9 Boundary Matrix : 1 2 3 4 6 7 8 9 Sum of boundary is 40