1. #include<stdio.h>
#include<math.h>
#include<string.h>
void copy(char a[50],char b[100][10][50],int u,int w);
float toi(char M[100][10][50],int i,int k);
int stcmp(char M[100][10][50],int i,int k,char (*str)[20],int f);
int missing_handle(float d[100][50],float new_d[100][50],int w,int u,int missing_attribute[],int
missing_tuple[],int datatype[]);
float detrm(float[25][25],float [25][25],int);
void cofact(float[25][25],float [25][25],int);
void trans(float[25][25],float[25][25],float [25][25],int);
void inverse(float [25][25],float ver[25][25],int);
void multiply(float [25][25],float [25],float [25][25],int);
void normalization(float d[100][50],int w,int u,int datatype[50]);
float mode(float new_d[100][50],int w,int v);
void missing_check(float d[100][50],int w,int u,int datatype);
int main()
{
FILE *fp;
int m,i=0,j=0,k=0,u=0,v=0,w=0,loc=0,f=0,norm;
char ch,name[50],a[100],M[100][10][50],*NomiValue[50];
int Nattributes,datatype[50],Nomnmber;
char (*str)[20],r=0;
float d[100][50],value;
printf("Enter the total number of attrributes=");
scanf("%d",&Nattributes);
for(i=0;i<Nattributes;i++)
{
printf("Enter the datatype of %dth attribute",i+1);
printf("ntNumerial=0 Nominal=1t=");
scanf("%d",&datatype[i]);
if(datatype[i]==0)
{
continue;
}
else
{
printf("nntEnter the total number of possible values of %dth attribute=",i+1);
scanf("%d",&Nomnmber);
printf("Enter all the possible attribute values ");
str=(char (*)[20])malloc(15*sizeof(*str));
NomiValue[r]=str;
2. r++;
for(j=0;j<Nomnmber;j++)
{
printf("n%d Attribute value=",j+1);
scanf("%s",str[j]);
}
}
}
/*for(k=0;k<2;k++)
{
str=NomiValue[k];
for(i=0;i<2;i++)
{
printf("%st",str[i]);
}
printf("n");
}*/
i=0;
//SEARCHING FOR TEXT FILE
do
{
puts("ntEnter the path of the file(EXTENSION SHOULD BE .txt): ");
gets(name);
//OPENING FILE IN READ MODE
fp=fopen(name,"r");
if(fp=='0')
puts("ntFile not found... Re-enter:");
else
puts("ntFile found");
}while(fp=='0');
//READING FROM FILE CHARACTER BY CHARACTER
while(1)
{
//WHILE BEGINS
ch=fgetc(fp);
if(ch==EOF)
{
printf("world");
break;
}
if(ch==32&&i==0)
9. {
printf("%d attribute contains missing datann ",i+1);
loc=1; //it is correct there is no need to change the value of loc=0
}
}
if(loc==1)
{
printf("Want to ignore missing values or handle using imputation methodn");
printf("Enter 0 for ignoring and 1 for handling=");
scanf("%d",&handle);
}
for(i=0;i<=w;i++)
{
n=0;
if(missing_tuple[i]!=1)
{
for(j=0;j<u;j++)
{
new_d[m][n]=d[i][j];
n++;
}
m++;
}
}
//printf("*******m=%d*********n=%d*********",m,n);
if(handle==1)
{
missing_handle(d,new_d,w,u,missing_attribute,missing_tuple,datatype);
}
else
{
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%ft",new_d[i][j]);
}
printf("n");
}
}
}
10. int missing_handle(float d[100][50],float new_d[100][50],int w,int u,int missing_attribute[],int
missing_tuple[],int datatype[]) //yth attribute contain missing data
{
int i=0,j=0,add=0,m=0,n=0,k=0,c=0,loc=0,v,f,g,z=0;
float sum,N[100][50],A[100][50],variable; //program can handle maximum 50 attributes
float (*p)[50];
int dependent[50],independent[50];
int count=0,r,s,q;
float (*array1)[25],*array2,ver[25][25],mul[25][25];
//calculation of no. of tuples after ignoring missing tuples
for(i=0;i<=w;i++)
{
if(missing_tuple[i]!=1)
count++;
}
printf("nno. of tuples after ignoring missing tuples=%dn",count);
for(i=0;i<u;i++)
{
if(missing_attribute[i]==1){
dependent[m]=i;
m++;
}
else{
independent[n]=i;
n++;
}
}
/* for(i=0;i<n;i++)
printf("%dt",independent[i]);
printf("n");*/
/* for(i=0;i<n;i++)
printf("%dt",independent[i]);
printf("n");*/
//formation of matrix with all independent variables and only one dependent variable
printf("nm=%d n=%d",m,n);
//for(z=0;z<m;z++) //will work depend on number of dependent variable
// {
v=dependent[z];
printf("nv=%d",v);
p=(float (*)[50])malloc(100*sizeof(*p));
printf("vatesh developer");
11. for(k=0;k<count;k++)
{
c=0;
for(j=0;j<n;j++)
{
f=independent[j];
//printf("#####%d",f);
p[k][c]=new_d[k][f];
//printf("%ft",p[k][c]);
c++;
}
p[k][c]=new_d[k][v];
//printf("%ft",p[k][c]);
c++;
//printf("n");
}
printf("ntuples after removaln");
for(r=0;r<count;r++) //no. of tuple without any missing value
{
for(s=0;s<=n;s++)
{
printf("%ft",p[r][s]); //p[][] contains independent variable and one dependent variable
}
printf("n");
}
//concept to be checked 2mrw
for(g=0;g<=n;g++)
{
for(s=g;s<=n;s++)
{
sum=0;
for(i=0;i<count;i++)
{
sum=sum+(p[i][g])*(p[i][s]);
}
printf("%f ",sum);
A[g+1][s+1]=sum;
}
}
N[0][0]=count;
for(j=0;j<=n;j++)
13. {
printf("%ft",N[i][j]);
}
printf("n");
}
//Array is the final array1,take its inverse and multiply by array2 to get final coefficients of equations.
array1=(float (*)[25])malloc(sizeof(*array1)*n);
array2=(float *)malloc(sizeof(float)*n);
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
array1[i][j]=N[i][j];
}
}
printf("nArray1n");
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
printf("%ft",array1[i][j]);
}
printf("n");
}
printf("nArray2n");
for(i=n+1;i<=n+1;i++) //will work only once
{
for(j=0;j<n+1;j++)
{
array2[j]=N[i][j];
printf("%ftn",array2[j]);
}
printf("n");
}
inverse(array1,ver,n+1);
printf("**********************************n");
for(i=0;i<=n;i++)
{
for(j=0;j<=n;j++)
{
14. printf("%ft",ver[i][j]);
}
printf("n");
}
multiply(ver,array2,mul,n+1); //note:-order of matrix is n+1
for(i=0;i<n+1;i++)
{
for(j=0;j<1;j++) //will work only once hence can be eliminated
{
printf("%ft",mul[i][j]);
}
printf("n");
}
for(i=0;i<w+1;i++)
{
sum=0;
if(d[i][v]==-1)
{
for(j=0;j<n;j++)
{
q=independent[j];
//printf("q=%")
sum=sum+d[i][q]*mul[j+1][0];
}
sum=sum+mul[0][0];
if(datatype[v]!=1)
{
d[i][v]=sum;
printf("nd[%d][%d]=%f",i,v,sum);
}
else
{
variable=mode(new_d,w,v);
d[i][v]=variable;
printf("nd[%d][%d]=%f",i,v,d[i][v]);
}
}
15. }
for(i=0;i<25;i++)
{
free((void *)array1[i]);
}
free(array1);
free(array2);
for(i=0;i<100;i++)
{free(p[i]);}
free(p);
missing_check(d,w,u,datatype);
//}
}
/* Inverse of a n by n matrix */
void inverse(float a[25][25],float inv[25][25],int k)
{
float d;
int i,j;
/*printf("in inversen");
for(i=0;i<k;i++)
{
for(j=0;j<k;j++)
{
printf("%ft",a[i][j]);
}
printf("n");
}
printf("k=%d",k);*/
d=detrm(a,inv,k);
printf("THE DETERMINANT IS=%f",d);
if(d==0)
printf("nMATRIX IS NOT INVERSIBLEn");
else