VAM (Vogel Approximation Method) in C++
March 30, 2009 by Paras
Filed under Seminars & Source Codes
SOURCE CODE :
//programed by Paras Wadher
MCA
Nagpur university
//program for Vogell’s Approximation Method (VAM)
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class VAM
{
private:
int A[100][100],B[100],C[100],i,j,m,n,sum1,sum2,S;
int small1,small2,small,small3,large1,large2,large3,middle1;
int middle2,middle3,large;
int csmall1,csmall2,csmall,csmall3,clarge1,clarge2,clarge3,cmiddle1;
int cmiddle2,cmiddle3,clarge;
int penalty;
public:
void getdata(void);
void display(void);
void calculate(void);
};
void VAM::getdata(void)
{
cout<<”\n\n Enter the size of cost matrix… “;
cout<<”\n\n Number of row’s : “;
cin>>m;
cout<<”\n\n Number of col’s : ” ;
cin>>n;
cout<<”\n\n Enter the element of the matrix…”;
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<”\n\n Enter the element of row “<<i+1<<” : “;
cin>>A[i][j];
}
}
for(i=0;i<n;i++)
{
cout<<”\n\n Enter the supply cost of row “<<i+1<<” : “;
cin>>B[i];
}
for(j=0;j<n;j++)
{
cout<<”\n\n Enter the demand of colomn “<<j+1<<” : “;
cin>>C[j];
}
}
void VAM :: display(void)
{
for(i=0;i<m;i++)
{
for(j=0;j<m;j++)
{
small = A[i][j];
large = A[i][j];
for(j=1;j<m;j++)
{
if(small <= A[i][j])
small = small;
else
small = A[i][j];
if(large >= A[i][j])
large = large;
else
large = A[i][j];
}
}
if(i==0)
{
small1 = small;
large1 = large;
}
if(i==1)
{
small2 = small;
large2 = large;
}
if(i == 2)
{
small3 = small;
large3 = large;
}
}
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
if(i==0)
{
if(A[i][j]>small1)
{
if(A[i][j]<large1)
{
middle1 = A[i][j];
}
}
}
else if(i==1)
{
if(A[i][j]>small2)
{
if(A[i][j]<large2)
{
middle2 = A[i][j];
}
}
}
else if(i==2)
{
if(A[i][j]>small3)
{
if(A[i][j]<large3)
{
middle3 = A[i][j];
}
}
}
}
}
small1 = middle1 – small1;
small2 = middle2 – small2;
small3 = middle3 – small3;
for(j=0;j<m;j++)
{
for(i=0;i<m;i++)
{
csmall = A[i][j];
clarge = A[i][j];
for(i=1;i<m;i++)
{
if(csmall <= A[i][j])
csmall = csmall;
else
csmall = A[i][j];
if(clarge >= A[i][j])
clarge = clarge;
else
clarge = A[i][j];
}
}
if(j==0)
{
csmall1 = csmall;
clarge1 = clarge;
}
if(j==1)
{
csmall2 = csmall;
clarge2 = clarge;
}
if(j == 2)
{
csmall3 = csmall;
clarge3 = clarge;
}
}
for(j=0;j<m;j++)
{
for(i=0;i<n;i++)
{
if(j==0)
{
if(A[i][j]>csmall1)
{
if(A[i][j]<clarge1)
{
cmiddle1 = A[i][j];
}
}
}
else if(j==1)
{
if(A[i][j]>csmall2)
{
if(A[i][j]<clarge2)
{
cmiddle2 = A[i][j];
}
}
}
else if(j==2)
{
if(A[i][j]>csmall3)
{
if(A[i][j]<clarge3)
{
cmiddle3 = A[i][j];
}
}
}
}
}
csmall1 = cmiddle1 – csmall1;
csmall2 = cmiddle2 – csmall2;
csmall3 = cmiddle3 – csmall3;
cout<<”\n\n The entered matrix is…\n\t\t\t\t\t SUPPLY Penalty\n”;
cout<<”\t\t\t———————\n”;
for(i=0;i<m;i++)
{
cout<<”\t\t”;
for(j=0;j<n;j++)
{
cout<<”\t”;
cout<<setw(2)<<A[i][j];
cout<<” |”;
if(j==2)
{
if(i==0)
cout<<” “<<B[i]<<” “<<small1;
if(i==1)
cout<<” “<<B[i]<<” “<<small2;
if(i==2)
cout<<” “<<B[i]<<” “<<small3;
}
}
cout<<”\n\t\t\t———————\n”;
}
cout<<”\n\t\t DEMAND”;
for(j=0;j<n;j++)
{
cout<<”\t”<<C[j];
}
cout<<”\n\n\t\tPenalty “<<csmall1<<” “<<csmall2<<” “<<csmall3;
}
void VAM::calculate(void)
{
int p11,p1,p2,p3,p4,p5,p6,r,s=0,r1,c1,S=0,S1=0,S2=0,S3=0;
p1 = small1;
p2 = small2;
p3 = small3;
p4 = csmall1;
p5 = csmall2;
p6 = csmall3;
if(p1>=p2 && p1>=3 && p1>=p4 && p1>=p5 && p1>=p6)
{
penalty = p1;
r = 1;
}
else if(p2>=p1 && p2>=p3 && p2>=p4 && p2>=p5 && p2>=p6)
{
penalty = p2;
r = 2;
}
else if(p3>=p1 && p3>=p2 && p3>=p4 && p3>=p5 && p3>=p6)
{
penalty = p3;
r = 3;
}
else if(p4>=p1 && p4>=p3 && p4>=p2 && p4>=p5 && p4>=p6)
{
penalty = p4;
r = 4;
}
else if(p5>=p1 && p5>=p3 && p5>=p4 && p5>=p2 && p5>=p6)
{
penalty = p5;
r = 5;
}
else
{
penalty = p6;
r = 6;
}
cout<<”\n\n\t Largest penalty : ” <<penalty;
cout<<”\n\n row : “<<r;
for(i=r-1;i<m;i++)
{
for(j=0;j<n;j++)
{
s = A[i][j];
for(j=1;j<n;j++)
{
if(s<=A[i][j])
{
s = s;
r1 = i;
c1 = j;
}
else
{
s = A[i][j];
r1 = i;
c1 = j;
}
}
}
}
cout<<”Small : “<<s<<” A["<<r1<<"]["<<c1-1<<"]“;
if(B[r]==C[c1])
{
B[r] = B[r]-C[c1];
S = S +(C[c1]*s);
C[c1] = 0;
}
else if(B[r]<C[c1])
{
C[c1] = C[c1]-B[r];
S = S +(B[r]*s);
B[r] = 0;
}
else if(B[r]>C[c1])
{
B[r] = B[r] – C[c1];
S = S + (C[c1]*s);
C[c1] = 0;
}
cout<<”\n\n\t Total transportation cost is : “<<S;
for(i=0;i<m;i++)
{
if((c1-1) == 0)
{
p11 = A[i][c1]-A[i][c1+1];
}
else if((c1-1) == 1)
{
p11 = A[i][c1-2]-A[i][c1];
}
else if((c1-1) == 2)
{
p11 = A[i][c1-3]-A[i][c1-2];
}
if(p11<0)
p11 = -p11;
if(i==0)
{
r = i;
cout<<”\n\n\t new Penalty (row 1) = “<<p11;
cout<<”\n\n\t At row “<<r;
}
if(i==1)
{
r = i;
cout<<”\n\n\t new Penalty (row 2) = “<<p11;
cout<<”\n\n\t At row “<<r;
}
if(i==2)
{
r = i;
cout<<”\n\n\t new Penalty (row 3) = “<<p11;
cout<<”\n\n\t At row “<<r;
}
}
}
void main()
{
clrscr();
VAM vam;
vam.getdata();
clrscr();
vam.display();
vam.calculate();
getch();
}
Cartesian Slope-Intercept Equation
March 27, 2009 by Paras
Filed under Seminars & Source Codes
SOURCE CODE :
//programed by Paras Wadher
MCA
Nagpur university
/*program to draw a line using Cartesian Slope-Intercept Equation
[ Simple Implementation ].*/
# include <iostream.h>
# include <graphics.h>
# include <conio.h>
# include <math.h>
void show_screen( );
void slope_intercept_line(const int,const int,const int,const int);
int main( )
{
int driver=VGA;
int mode=VGAHI;
int x_1=0;
int y_1=0;
int x_2=0;
int y_2=0;
do
{
show_screen( );
gotoxy(8,10);
cout<<”Coordinates of Point-I (x1,y1) :”;
gotoxy(8,11);
cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”;
gotoxy(12,13);
cout<<”Enter the value of x1 = “;
cin>>x_1;
gotoxy(12,14);
cout<<”Enter the value of y1 = “;
cin>>y_1;
gotoxy(8,18);
cout<<”Coordinates of Point-II (x2,y2) :”;
gotoxy(8,19);
cout<<”ÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ”;
gotoxy(12,21);
cout<<”Enter the value of x2 = “;
cin>>x_2;
gotoxy(12,22);
cout<<”Enter the value of y2 = “;
cin>>y_2;
initgraph(&driver,&mode,”..\\Bgi”);
setcolor(15);
slope_intercept_line(x_1,y_1,x_2,y_2);
setcolor(15);
outtextxy(110,460,”Press <Enter> to continue or any other key to exit.”);
int key=int(getch( ));
if(key!=13)
break;
}
while(1);
return 0;
}
void slope_intercept_line(const int x_1,const int y_1,const int x_2,const int y_2)
{
int color=getcolor( );
float x=x_1;
float y=y_1;
float dx=(x_2-x_1);
float dy=(y_2-y_1);
float m=(dy/dx);
float b=(y-(m*x));
float x_inc=((x_2>=x_1)?1:-1);
putpixel(x,y,color);
while((int)(x+0.5)!=x_2)
{
x+=x_inc;
y=((m*x)+b);
putpixel((int)(x+0.5),(int)(y+0.5),color);
}
}
void show_screen( )
{
restorecrtmode( );
textmode(C4350);
textbackground(1);
cprintf(” Cartesian Slope Intercept Equation “);
textbackground(8);
for(int count=0;count<42;count++)
gotoxy(1,46);
gotoxy(8,40);
cout<<”Note :”;
gotoxy(8,41);
cout<<”ÍÍÍÍÍÍ”;
gotoxy(10,43);
cout<<”This program is better for those lines with é<ñ45ø with x-axis.”;
gotoxy(1,2);
}


