-
Notifications
You must be signed in to change notification settings - Fork 0
/
diagonaldominant.c
82 lines (72 loc) · 1.73 KB
/
diagonaldominant.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
//to check if a matrix is diagonalisable
#include<stdio.h>
#include<stdlib.h>
#define MAX 50
//diagonally dominant matrix
void main(){
int a[MAX][MAX];
int a1[MAX][MAX];
int b[MAX];
int arows, acolumns;
int i,j,k;
//Matrix A input
printf("Enter number of rows and columns of matrix a -\n");
scanf("%d", &arows);
acolumns = arows;
printf ("Input Matrix A \n");
for(i=0; i<arows; i++){
for(j=0; j<acolumns; j++){
scanf("%d",&a[i][j]);
}
}
//element checking
int sum = 0;
for(i=0; i<arows; i++){
for(j=0; j<acolumns; j++){
for(k=0; k<acolumns; k++){
if(k!=j){
sum = sum + abs(a[i][k]);
}
}
//use the sum here
if(a[i][j]>=sum){
printf("sum = %d and a[i][j] = %d i = %d \n",sum,a[i][j],i);
b[i]= j +1;
}
//sum is reinnitialised
sum =0;
}
}
int test = 0;
for(i=1; i<=arows; i++){
for(j=0; j<arows; j++){
if(b[j]==i){
test = test +1;
}
}
}
for(i=0;i<arows;i++){
printf("b is %d \n",b[i]);
}
printf("Test is %d \n",test);
int n ;
if(test == arows){
printf("Matrix can be diagonalisable \n");
for(i=0;i<arows;i++){
n=b[i]-1;
for(j=0;j<arows;j++){
a1[i][j]=a[n][j];
}
}
//print diagonalised matrix
for(i=0;i<arows;i++){
for(j=0;j<arows;j++){
printf("%d ",a1[i][j]);
}
printf("\n");
}
}
else{
printf("Sorry not possible \n");
}
}