Friday, January 9, 2015

Implement Mathematical Equation from your Codes in C

#include<stdio.h>
void main()
{
int a[10][10],c[10],x[10],b[10],cj[10],y[10][10];
int m[2],n,i,j,k,l,w[10],f,s,q=0,r,ef=1,t,ab=0,u,bc[10],cd=0,jk,kl,lm,mn;
printf("enter max equation variable no.");
scanf("%d",&n);
printf("enter max z=c1 x1 + c2 x2 + c3 x3 + ...... cn xn\n");
for(i=0;i<n;i++)
{
printf("enter c%d ",i+1);
scanf("%d",&c[i]);
}
printf("enter subject to constrain equation and variable no. ");
scanf("%d%d",&m[0],&m[1]);
printf("enter subject to constrain equations\n");
for(j=0;j<m[0];j++)
{
printf("enter equation%d\n",j+1);
printf("a%d1 x1 + a%d2 x2 + a%d3 x3   + a%dn xn <=/>= b%d  \n",j+1,j+1,j+1,j+1,j+1);
for(k=0;k<m[1];k++)
{
printf("enter a%d%d ",j,k);
scanf("%d",&a[j][k]);
}
printf("enter 1 for >= end press 2 for <= press 3 for = ");
scanf("%d",&f);
if(f==1)
w[j]=f;
else if(f==2)
w[j]=f;
else if(f==3)
w[j]=f;
else
{
printf("error\n");
break;
}
printf("enter b%d ",j+1);
scanf("%d",&b[j]);
}
for(j=0;j<m[0];j++)
{
for(k=0;k<m[1];k++)
{
printf("%d x%d  ",a[j][k],k+1);
if(k<(m[1]-1))
printf("+ ");
}
printf("= ");
printf("%d\n",b[j]);
}
for(l=0;l<m[0];l++)
{
if(w[l]==1||w[l]==2||w[l]==3)
{
i++;
m[1]++;
}
}
for(l=n;l<i;l++,n++)
{
c[l]=0;
}
for(l=0,s=k;l<m[0];l++)
{
for(f=k;f<m[1];f++)
{
a[l][f]=0;
}
if(w[l]==1)
{
a[l][s]=-1;
s++;
}
if(w[l]==2)
{
a[l][s]=1;
s++;
}
if(w[l]==3)
{
a[l][s]=0;
s++;
}
}
printf("after introducing slack & surplus variables\n");
for(j=0;j<m[0];j++)
{
for(k=0;k<m[1];k++)
{
printf("%d x%d  ",a[j][k],k+1);
if(k<(m[1]-1))
printf("+ ");
}
printf("= ");
printf("%d\n",b[j]);
}
for(l=0;l<m[0];l++)
{
if(w[l]==1)
{
i++;
m[1]++;
}
}
for(l=n;l<i;l++)
{
c[l]=-1;
n++;
}
for(l=0,s=k;l<m[0];l++)
{
for(f=k;f<m[1];f++)
{
a[l][f]=0;
}
if(w[l]==1)
{
a[l][s]=1;
s++;
q++;
}
}
printf("after introducing artificial variables\n");
for(j=0;j<m[0];j++)
{
for(k=0;k<m[1];k++)
{
printf("%d x%d  ",a[j][k],k+1);
if(k<(m[1]-1))
printf("+ ");
}
printf("= ");
printf("%d\n",b[j]);
}
printf("\nnow we solve it by two phase method\n");
printf("Ist PHASE\n");
for(i=0;i<m[1];i++)
{
cj[i]=0;
if(i>=m[1]-q)
{
cj[i]=c[i];
}
}
for(f=0;f<m[0];f++,ef++)
{
for(r=0;r<m[1];r++)
{
ab=0;
if(a[f][r]==1)
{
for(t=0;t<f;t++)
{
if(a[t][r]==0)
{
ab=1;
}
}
for(u=f+1;u<m[0];u++)
{
if(a[u][r]==0)
{
ab=1;
}
}
}
if(ab==1)
{
bc[cd]=r;
cd++;
}
}
}
for(jk=0;jk<m[0];jk++)
{
x[bc[jk]]=b[i];

}
for(kl=0,jk=0;kl<m[0];kl++,jk++)
{
for(lm=0;lm<m[1];lm++)
{
y[kl][lm]=a[bc[jk]][lm];
}
}
printf(" Cj------->");
for(mn=0;mn<m[1];mn++)
{
printf("%7d",cj[mn]);
}
printf("\nCb B   Xb=b   ");
for(kl=0;kl<m[1];kl++)
{
printf("  y%d",kl+1);
}
printf("\n");
for(i=0;i<m[0];i++)
{
printf("%2d   a[%d]   x[%d]=%d",cj[bc[i]],bc[i]+1,bc[i]+1,b[i]);
bc[i]=bc[i]-2;
for(j=0;j<m[1];j++)
{
printf("%5d",a[i][j]);

}
printf("\n");
}
}


for more Learning Codes Visit to http://codesofprogramming.blogspot.in/

No comments:

Post a Comment