gra sim #include #include #include //#include"quick.cpp" partition(float x[],int lb,int ub) { int up,down; float a; a=x[lb]; up=ub; down=lb; while(downa) up--; if(downub) return(0); int j=partition(x,lb,ub); quick(x,lb,j-1); quick(x,j+1,ub); } void main() { clrscr(); int m,n,i,k,l,j; float a[100][100],b[100],temp,temp2; cout<<"Enter the no of equations : "; cin>>m; cout<<"Enter the no of variables : "; cin>>n; for(i=0;i>a[i][k]; } } for(i=0;i #include #include #include #include printmat(float **,int,int,int,int,int,char **,char **); void LinkFloat(); typedef struct pivot{ int row; int col; float val; }; void main() { FILE *fp; int eqn,var,col,i,j,k,temp_i,temp_j,basic,nonbasic,LOOP=0,choice,less,more,equal,answer=0,FANSI=0,UNBOUND=0; float num,**p,temp,rconst,pvconst,fconst; float *p1,p2; int M=100; struct pivot pelement; char fname[12]; char lessthan[] ="<="; char morethan[] =">="; char equalto[] = "="; char **eqntype; char objtype; char **basicvars,**allvars,k1[5],k0[5]; char *bvar1,bvar2,*avar1,avar2; int turnless=1,turnarti=1,turncol=0; p1=&p2; p2=*p1; p=&p1; bvar1=&bvar2; bvar2=*bvar1; basicvars=&bvar1; avar1=&avar2; avar2=*avar1; allvars=&avar1; clrscr(); LinkFloat(); textcolor(LIGHTBLUE); cprintf(" 1.Create a new file \r\n"); cprintf(" 2.Read from existing file \r\n"); cprintf(" Enter choice:"); textattr(CYAN); scanf("%d",&choice); switch(choice) { case 1: cprintf("\r\n Enter the name of new file:"); scanf("%s",fname); cprintf("\r\n Enter the no. of equations : "); scanf("%d",&eqn); eqn++; cprintf("\r\n Enter the no. of variables : "); scanf("%d",&var); var++; printf("\nEnter the no. of '<=' constraints : "); scanf("%d",&less); printf("\nEnter the no. of '>=' constraints : "); scanf("%d",&more); printf("\nEnter the no. of '=' constraints : "); scanf("%d",&equal); printf("Enter the type of Objective fn.('m' for min / 'M' for max) : "); fflush(stdin); scanf("%c",&objtype); col= var+(less*1)+(more*2)+(equal*1)+1; allvars=(char**)malloc(col*sizeof(char*)); for(i=0;ip[i][j]) { temp=p[i][j]; pelement.col=j; } } if (temp<0) LOOP=1; else { LOOP=0; break; } cprintf("\r\n\r\n Entering variable : %s",allvars[pelement.col+1]); getch(); /****************ratio*****************/ for(i=1,j=pelement.col;ip[i][j] && p[i][j]>0) { temp=p[i][j]; pelement.row=i; } } getch(); cprintf("\r\n\r\n \"RATIO\"\r\n"); printmat(p,eqn,col,var,basic,nonbasic,basicvars,allvars); cprintf("\r\n\r\n Outgoing variable : %s",basicvars[pelement.row]); basicvars[pelement.row]=allvars[pelement.col+1]; getch(); /*********making the pivot element 1***********/ pelement.val=p[pelement.row][pelement.col]; for(j=0,i=pelement.row;j0) LOOP=1; else { LOOP=0; break; } cprintf("\r\n\r\n Entering variable : %s",allvars[pelement.col+1]); getch(); /****************ratio*****************/ for(i=1,j=pelement.col;ip[i][j] && p[i][j]>0) { temp=p[i][j]; pelement.row=i; } } getch(); cprintf("\r\n\r\n \"RATIO\"\r\n"); printmat(p,eqn,col,var,basic,nonbasic,basicvars,allvars); //nonbasic=pelement.row+var-1; cprintf("\r\n\r\n Outgoing variable : %s",basicvars[pelement.row]); basicvars[pelement.row]=allvars[pelement.col+1]; getch(); /*********making the pivot element 1***********/ pelement.val=p[pelement.row][pelement.col]; for(j=0,i=pelement.row;j #include #include #include #define M 10 //function to print the matrix table(float **,int,int,int,struct name *);//change //for changing the basic variables change(struct name *,struct pivot *,int );//ch1 typedef struct pivot{ int row; int col; float val; }; //for storing the basic variables typedef struct name{ char nam; int numb; }; void main() { int eqn,var,col,i,j,temp_i,temp_j,basic,nonbasic,LOOP=0; char b_name; int b_val; float num,**p,temp,rconst,pvconst,fconst; float *p1,p2; struct pivot pelement; struct name na[M]; for(i=0;ip[i][j]) { temp=p[i][j]; pelement.col=j;//CONTAINS COLUMN OF LEAST ELEMENT } } /*TO EXIT THE LOOP IF THERE IS NO NEGATIVE NUMBER IN THE OBJECTIVE FUN*/ if (temp<0) LOOP=1; else { LOOP=0; break; } //ch2 from basic=pelement.col; if(basic%c%d",b_name,b_val); //ch2 to /*ONCE THE COLUMN IS KNOWN IT IS REQUIRED TO FIND THE RATIO */ for(i=1,j=pelement.col;ip[i][j] && p[i][j]>0) { temp=p[i][j]; pelement.row=i; } } //getch(); //ch2 added nonbasic=pelement.row; printf("\n\t\tNON BASIC VARIABLE ==> %c%d",na[nonbasic].nam,na[nonbasic].numb); getch(); /*NOW WE WILL CHANGE THE ROW CONTAINING THE PIVOT ELEMENT,SUCH THAT THE PIVOT ELEMENT IS HAVING VALUE '1'*/ pelement.val=p[pelement.row][pelement.col]; for(j=0,i=pelement.row;j"); for(i=1;inam,na->numb); na++; for(j=0;jcol; j=pelement->row; if(inam='x'; na->numb=i+1; } else { na+=j; na->nam='s'; na->numb=i-(var-1); } } 2phase #include #include #include #include #include printmat(float **,int,int,int,int,int,char **,char **); void LinkFloat(); typedef struct pivot{ int row; int col; float val; }; void main() { FILE *fp; int eqn,var,col,i,j,k,f=0,temp_i,temp_j,basic,nonbasic,LOOP=0,choice,less,more,equal,answer=0,FANSI=0,UNBOUND=0; float num,**p,temp,rconst,pvconst,fconst; float *p1,p2; int M=100; float obj_fn[10]; struct pivot pelement; char fname[12]; char lessthan[] ="<="; char morethan[] =">="; char equalto[] = "="; char **eqntype; char objtype; char **basicvars,**allvars,k1[5],k0[5]; char *bvar1,bvar2,*avar1,avar2; int turnless=1,turnarti=1,turncol=0; p1=&p2; p2=*p1; p=&p1; bvar1=&bvar2; bvar2=*bvar1; basicvars=&bvar1; avar1=&avar2; avar2=*avar1; allvars=&avar1; clrscr(); LinkFloat(); textcolor(LIGHTBLUE); cprintf(" 1.Create a new file \r\n"); cprintf(" 2.Read from existing file \r\n"); cprintf(" Enter choice:"); textattr(CYAN); scanf("%d",&choice); switch(choice) { case 1: cprintf("\r\n Enter the name of new file:"); scanf("%s",fname); cprintf("\r\n Enter the no. of constraints : "); scanf("%d",&eqn); eqn++; cprintf("\r\n Enter the no. of variables : "); scanf("%d",&var); var++; printf("\nEnter the no. of '<=' constraints : "); scanf("%d",&less); printf("\nEnter the no. of '>=' constraints : "); scanf("%d",&more); printf("\nEnter the no. of '=' constraints : "); scanf("%d",&equal); printf("Enter the type of Objective fn.('m' for min / 'M' for max) : "); fflush(stdin); scanf("%c",&objtype); col= var+(less*1)+(more*2)+(equal*1)+1; allvars=(char**)malloc(col*sizeof(char*)); for(i=0;ip[i][j]) { temp=p[i][j]; pelement.col=j; } } if (temp<0) LOOP=1; else { LOOP=0; break; } cprintf("\r\n\r\n Entering variable : %s",allvars[pelement.col+1]); getch(); /****************ratio*****************/ for(i=1,j=pelement.col;ip[i][j] && p[i][j]>0) { temp=p[i][j]; pelement.row=i; } } getch(); cprintf("\r\n\r\n \"RATIO\"\r\n"); printmat(p,eqn,col,var,basic,nonbasic,basicvars,allvars); cprintf("\r\n\r\n Outgoing variable : %s",basicvars[pelement.row]); basicvars[pelement.row]=allvars[pelement.col+1]; getch(); /*********making the pivot element 1***********/ pelement.val=p[pelement.row][pelement.col]; for(j=0,i=pelement.row;j0) LOOP=1; else { LOOP=0; break; } cprintf("\r\n\r\n Entering variable : %s",allvars[pelement.col+1]); getch(); /****************ratio*****************/ for(i=1,j=pelement.col;ip[i][j] && p[i][j]>0) { temp=p[i][j]; pelement.row=i; } } getch(); cprintf("\r\n\r\n \"RATIO\"\r\n"); printmat(p,eqn,col,var,basic,nonbasic,basicvars,allvars); //nonbasic=pelement.row+var-1; cprintf("\r\n\r\n Outgoing variable : %s",basicvars[pelement.row]); basicvars[pelement.row]=allvars[pelement.col+1]; getch(); /*********making the pivot element 1***********/ pelement.val=p[pelement.row][pelement.col]; for(j=0,i=pelement.row;j #include #include #include #include #define TRUE 1 #define FALSE 0 int slack(int i,float arr[][10],int p,int n) { cout<<"Enter coeff. of constraint "<>arr[i][j]; arr[i][p]=1; p=p+1; return p; } int equal(int i,float arr[][10],int p,int n,int w) { cout<<"\nEnter coeff. of constraint "<>arr[i][j]; arr[i][p]=1; arr[1][p]=-1; p=p+1; return p; } int surplus(int i,float arr[][10],int p,int n,int w) { cout<<"\nEnter coeff of constraint "<>arr[i][j]; arr[i][p]=-1; p=p+1; arr[i][p]=1; arr[1][p]=-1; p=p+1; return p; } void display(float arr[][10],int p,int n,char strvar1[][6],char strvar2[][6],char str2[][6],int k) { getch(); for(int i=0;i<=n;i++) cout<number of columns //k-->number of rows //w-->type of objective function int j=1; int i=2; while(i<=k+1) { j=1; while(j<=s) { if(j<=n) { if(strcmp(str2[i-2],strvar1[j])==0) { if(arr[i][j]==1 && arr[1][j]!=0) { int temp=arr[1][j]; for(int r=1;r<=s+1;r++) { arr[1][r]=arr[1][r]-temp*arr[i][r]; if(arr[1][r]>0 && arr[1][r]<=0.009) arr[1][r]=0; } } i++; } else { j++; } } else { if(strcmp(str2[i-2],strvar2[j-3])==0) { if(arr[i][j]==1 && arr[1][j]!=0) { int temp=arr[1][j]; for(int r=1;r<=s+1;r++) { arr[1][r]=arr[1][r]-temp*arr[i][r]; if(arr[1][r]>0 && arr[1][r]<=0.009) arr[1][r]=0; } } i++; } else { j++; } } } } } int min(float arr[][10],int m,int n) { float min=0; for(int j=1;j<=m;j++) { if(min>arr[1][j]) min=arr[1][j]; } int i=1; while(min!=arr[1][i]) { i++; } return i; } int max(float arr[][10],int m,int n) { float max=0; for(int j=1;j<=m;j++) { if(max0) { while(max!=arr[1][i]) { i++; } return i; } else return(-1); } int ratio(float arr[][10],int m,int n,int p) { int j=1; float ratio[50]; for(int i=2;i<=n+1;i++) { if(arr[i][p]>0) { ratio[j]=arr[i][m+1]/arr[i][p]; j++; } else { ratio[j]=32617; j++; } } float min=ratio[1]; for(int k=1;kratio[k]) min=ratio[k]; } int count=2; while(min!=ratio[count-1]) { count++; } return count; } void iteration(float arr[][10],int m,int l,int p,int q,char strvar1[][6],char strvar2[][6],char str2[][6],int n) { if(p<=l) strcpy(str2[q-2],strvar1[p]); else strcpy(str2[q-2],strvar2[p-l-1]); float pivot=arr[q][p]; for(int i=1;i<=m+1;i++) { if(arr[q][i]!=0 && pivot!=0) arr[q][i]=arr[q][i]/pivot; } for(int k=1;k<=n+1;k++) { float temp=arr[k][p]; for(int j=1;j<=m+1;j++) { if(k!=q) { arr[k][j]=arr[k][j]-(temp*arr[q][j]); } } } //cout<<"------------------------second table---------------------------"<0 && arr[1][r]<=0.009) arr[1][r]=0; } display(arr,m,l,strvar1,strvar2,str2,n); } void main() { clrscr(); int m,n,i,j; float arr[50][10],num,temp[10][50]; char strvar1[20][6]={"BASIC","x1","x2","x3","x4","x5","x6","x7"}; char strvar2[20][6]={"s1","s2","s3","s4","s5","s6","s7"}; char str2[20][6]; cout<<"Enter number of equations"; cin>>m; cout<<"Enter number of variables"; cin>>n; for(i=1;i<=10;i++) { for(j=1;j<=10;j++) { arr[i][j]=0; temp[i][j]=0; } } cout<<"enter coeff. of objective function"; for(i=1;i<=n;i++) { cin>>num; //arr[1][i]=-num; temp[1][i]=-num; } int w; cout<<"\nenter the type of objective function"; cout<<"\n1.maximization"; cout<<"\n2.minimization"<>w; int t,p=n+1,k=0; i=2; while(i<=m+1) { cout<<"enter the type of constraint:"; cout<<"\n1.<="; cout<<"\n2.="; cout<<"\n3.>="<>t; switch(t) { case 1: p=slack(i,arr,p,n); // cout<<"p->"<"<>arr[i][p]; } display(arr,p-1,n,strvar1,strvar2,str2,k); compute(arr,p-1,k,w); display(arr,p-1,n,strvar1,strvar2,str2,k); int FLAG=FALSE; int r=1; while(r<=p) { if(arr[1][r]>0) { FLAG=TRUE; break; } else r++; } while(FLAG) { int col=max(arr,p-1,k); if(col!=-1) { int row=ratio(arr,p-1,k,col); iteration(arr,p-1,n,col,row,strvar1,strvar2,str2,k); FLAG=FALSE; r=1; while(r<=p) { if(arr[1][r]>0) { FLAG=TRUE; cout<"<"<"<"<0) { FLAG=TRUE; break; } else r++; } while(FLAG) { int col=max(arr,s-2,k); if(col!=-1) { // cout<<"col-->"<"<0) { FLAG=TRUE; cout<