Editorial For SSEC-Coding Contest
Problem Link:
Author: Pavan Kumar
Difficulty: Medium, Hard
PREREQUISITES: Maths, Thomas Algorithm, basic Logic Building
Problem:
Write a code for Sum of Tri-diagonal Matrix and print both the sum and Tri-diagonal Matrix , take input of valid number which does not come in 0th place, and the matrix should be a square matrix. For better understanding take matrix size greater than [5][5] .
Example :-
- Tri-diagonal Matrix
1 2 0 0 0
3 4 5 0 0
0 6 7 8 0
0 0 9 10 11
0 0 0 12 13
- Sum = 91
Sample Input:
5
5
1
2
3
4
5
6
7
8
9
10
11
12
13
Sample Output:
1 2 0 0 0
3 4 5 0 0
0 6 7 8 0
0 0 9 10 11
0 0 0 12 13
91
Solution:
#include<stdio.h> #include<math.h> main() { int a[50][50],i,j,m,n,sum=0; scanf("%d",&m); scanf("%d",&n); for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(abs(i-j)>1) { continue; } else { scanf("%d",&a[i][j]); while(a[i][j]==0) { scanf("%d",&a[i][j]); } } } } for(i=0;i<m;i++) { for(j=0;j<n;j++) { if(abs(i-j)>1) { printf("0\t"); } else { printf("%d\t",a[i][j]); sum=sum+a[i][j]; } } printf("\n"); } printf("\n %d",sum); return 0; } }