This program is for finding rank of matrix and solving system of linear equation
Let's try it.
#include<stdio.h>
#include<conio.h>
float a[10][10];
float b[10];
int m,n;
void setorder();
void getmatrix();
int chkzero(int i);
void printrank();
void print();
void rowop(int i);
void changerow(int i, int l);
void main() {
clrscr();
int i=1;
int chk;
setorder();
getmatrix();
clrscr();
print();
for(i=1;i<m;) {
chk = chkzero(i);
if((a[i-1][i-1]==0) && (chk==1) ) {
changerow(i,i);
}
else {
rowop(i);
i++;
}
}
printf("\n\tFinal Matrix Is:\n\n");
print();
printrank();
getch();
}
void setorder() {
printf("Enter Order of Matrix\n");
scanf("%d%d",&m,&n);
clrscr();
}
void getmatrix() {
int x,y,z;
printf("Enter Coefficient Matrix\n");
for(x=0;x<m;x++) {
for(y=0;y<n;y++) {
scanf("%f" ,&a[x][y]);
}
}
clrscr();
printf("Enter Matrix of Constants\n");
for(z=0;z<m;z++) {
scanf("%f",&b[z]);
}
clrscr();
}
void changerow(int i,int l) {
float temp,temp1;
int j;
if(a[i][l-1]==0) {
i++;
changerow(i,l);
}
else {
temp1=b[i-1];
b[i-1]=b[i];
b[i]=temp1;
for(j=0;j<n;j++) {
temp=a[i-1][j];
a[i-1][j]=a[i][j];
a[i][j]=temp;
}
printf("\t\tR%d%d\n\n",i,i+1);
print();
}
}
void rowop(int i) {
float temp1,temp2,temp3;
int j;
int l=1;
int k=i;
int o=i;
temp1=a[o-1][o-1];
// printf("\n%d",i);
for(;i<m;i++,l++) {
temp2=a[i][i-l];
if(temp1!=0) {
b[i]=b[i]-(temp2/temp1)*b[i-l];
for(j=0;j<n;j++) {
a[i][j]=a[i][j]-(temp2/temp1)*a[i-l][j];
}
}
if(temp2!=0) {
printf("\n\tR%d=R%d-(%.3f/%.3f)*R%d\n",i+1,i+1,temp2,temp1,k);
print();
}
}
}
int chkzero(int i) {
int k=i;
int j=0;
for(;j<n;j++) {
if (a[k][j]==0) {
// return 0;
}
else {
// break;
return 1;
}
}
}
void print() {
int x,y;
for(x=0;x<m;x++) {
for(y=0;y<n;y++) {
printf("%10.3f ",a[x][y]);
}
printf("\t%12.3f",b[x]);
printf("\n");
}
printf("\n");
}
void printrank() {
int x,y;
int rank=0;
for(x=0;x<m;x++) {
for(y=0;y<n;y++) {
if (a[x][y]!=0) {
rank++;
break;
}
}
}
printf("\n\t\tRank of given matrix is %d",rank);
}