Sei sulla pagina 1di 5

//Made by Kumar Divij BITS Pilani,Pilani

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<math.h>
void sortRow(int cr,int cc,int row,int col,double arr[row][col],int truth);
void printArray(int row,int col,double a[30][30]);
void getArray(int row,int col,double a[30][30]);
void createOneZero(int cr,int cc,int row,int col,double a[30][30]);
void createRREF(int row,int col,double a[30][30]);
void main()
{
int row,col;
row=4;
col=3;
srand(time(NULL));
double a[30][30];
getArray(row,col,a);
createOneZero(0,0,row,col,a);
createRREF(row,col,a);
//sortRow(1,0,row,col,a,1);
//printArray(row,col,a);
}
void createOneZero(int cr,int cc,int row,int col,double a[30][30])//cc current row; cr
currnet column
{
sortRow(cr,cc,row,col,a,1);// sorts array from CURRENT ROW(cr),acc to
CURRENT COL(cc),in DECREASING ORDER of absolute value, down the row.
int i,j; // This sorting is to accomplish all zeroes at the bottom. General
convection that rows with zeroes lie
double temp; // down the row.
for(i=cr;i<row;i++)
{
if(a[i][cc]!=0) // Division operation if the first element of a given cr-cc
coombination is non zero.
{
temp=a[i][cc]; // Creating a copy of first element as, the first
element itself would change, after division.
for(j=0;j<col;j++)
{
a[i][j]=a[i][j]/(temp*1.0); // Same old division. (temp!=0)
}
}
} //successfully created a 1 in each row,
at a specific columns starting from col-0 to col-COL-1,
//printArray(row,col,a); //The CC would look like 1,1,1,. . .0,0,0.
double tempCr[col];
for(i=0;i<col;i++) //Now this piece creates a Copy of the
CR(current row) elements. I think its is redundant see
{ // comment in line 52 which can be
used ALTERNATIVELY.
tempCr[i]=a[cr][i];
}
for(i=cr+1;i<row;i++)
{
//if(i!=cr && a[i][cc]!=0) //I changed the init. of the above for loop from
i=0 to i=cr+1 soo i!=cr is no longer needed.
if(a[i][cc]!=0) //We dont want our PRECIOUS ZEROS to
be converted to non zeroes. :-). Conservative Mentality.
for(j=0;j<col;j++)
{
a[i][j]=a[i][j]-tempCr[j];
//a[i][j]-=a[cr][j]; // refer to comment in line no 42
}
}
//printArray(row,col,a); // Used for debugging my program. Yeah I dont
like debugging but its my program so I am cool.
if(cr==row-1 || cc==col-1) // For a rectangle a, 45 degree line wrt any side
intersects opp side. The square formed by this
{ // diagonal is the minimum and
maximum area, of the rectangle(here our matrix)that should be a-
//printArray(row,col,a);// -UNIT MATRIX. as cr or cc approaches row or
col resp. our line approaches the side.
return;
}
createOneZero(cr+1,cc+1,row,col,a); // MUCH AWAITED RECURSION

}
void sortRow(int cr,int cc,int row,int col,double arr[30][30],int truth) // I like my sort
function more than anything in this program.
{ //The "truth" (variable) value if
put =0 sorts rows of a matrix in ASCENDING order.
int i=0,j=0; // and if truth =1 in DESCENDING order.
double temp=0;
int k=0;
for(k=cr;k<row;k++)
{
for(i=cr;i<row-1-(k-cr);i++)
{
if(abs(arr[i+truth][cc]) >= abs(arr[i+1-truth][cc])) // heres how it works. "i+truth"
and "i+1-truth" swap values for 0,1
{
for(j=0;j<col;j++)
{
temp=arr[i][j];
arr[i][j]=arr[i+1][j];
arr[i+1][j]=temp;
}
}
}
}
}
void createRREF(int row,int col,double a[30][30])// RREF gets simpler when we
create REF form.
{
int i,j,k;
double temp;
int lim=(row+col-(abs(row-col)))/2;
for(i=0;i<lim-1;i++)
{
for(j=lim-1-i;j>0;j--)
{
temp=a[j-1][lim-1-i];
for(k=0;k<col;k++)
{
a[j-1][k]=a[j-1][k]-temp*a[lim-i-1][k];
}
}
}
printArray(row,col,a);
}
void printArray(int row,int col,double a[30][30])
{
int i,j;
printf("\nArray=\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
if(a[i][j]<0 || a[i][j]==-0.0)
printf("%f\t",a[i][j]);
else if(a[i][j]>0 || a[i][j]==0.0)
printf(" %f\t",a[i][j]);
}
printf("\n");
}
}
void getArray(int row,int col,double a[30][30])
{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
{
//a[i][j]=pow(-1,1+rand()%9)*(1+rand()%9+0.1*(rand()%9)+0.01*(rand()%9));
a[i][j]=(1+rand()%9);
}
}
printArray(row,col,a);
}

Potrebbero piacerti anche