VAM (Vogel Approximation Method) in C++

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();
}

4 thoughts on “VAM (Vogel Approximation Method) in C++

Leave a Reply

Your email address will not be published. Required fields are marked *