Column Minima Method in C++

SOURCE CODE :

//programed by Paras Wadher
MCA
Nagpur university

//program to find trans.cost by col_min


#include<iostream.h>

#include<conio.h>

#include<dos.h>

class c_min

{

public:

int c[10][10],s[10],d[10];

int row,col,i,j;

int ssum,dsum,sum;

void get();

void bal();

void dis();

void cal();

};

void c_min::get()

{

//input r and c

cout<<“\nenter no. of rows:-“;cin>>row;

cout<<“\nenter no. of cols:-“;cin>>col;

//input cost

cout<<“\nenter cost matrix\n”;

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

{

cin>>c[i][j];

}

}

//input supply

cout<<“\nenter supply array\n”;

ssum=0;

for(j=0;j<row;j++)

{

cin>>s[j];

ssum=ssum+s[j];

}

//input demand

cout<<“\nenter demand array\n”;

dsum=0;

for(j=0;j<col;j++)

{

cin>>d[j];

dsum=dsum+d[j];

}

bal();

clrscr();

cout<<“\nTransportation Problem Is\n\n\n”;

dis();

}

void c_min::dis()

{

for(i=0;i<row;i++)

{

for(j=0;j<col;j++)

{

cout<<c[i][j]<<“\t”;

}

cout<<“\t|\t”<<s[i]<<“\n”;

}

cout<<“—————————————\n”;

for(j=0;j<col;j++)

{

cout<<d[j]<<“\t”;

}

for(j=0;j<col-1;j++){cout<<“\t”;}

cout<<“|\t”;

}

void c_min::bal()

{

if(ssum!=dsum)

{

cout<<“\nenter only balanced problem”;

get();

}

else

return;

}

void c_min::cal()

{

int min;

int plc;

sum=0;

cout<<“\n\n\nSOLUTION:-\n”;

start: //delay(10000);

{

//finding min from col

min=1000;

for(i=0;i<row;i++)

{

if(min>=c[i][0])

{

min=c[i][0];

plc=i;

}

else

continue;

}

//for large supply

if(s[plc]>=d[0])

{

//cal of sum

sum=sum+(c[plc][0]*d[0]);

//diff cal

s[plc]=s[plc]-d[0];

//shifting of col and demand

for(i=1;i<row+1;i++)

{

for(j=1;j<col;j++)

{

c[i-1][j-1]=c[i-1][j];

d[i-1]=d[i];

}

}

col–;

if(s[plc]==d[0])

{

for(i=1;i<col+1;i++)

{

for(j=plc+1;j<row;j++)

{

c[j-1][i-1]=c[j][i-1];

}

}

for(j=plc+1;j<row;j++)

s[j-1]=s[j];

row–;

}

if(row<0)

goto end;

}

//for large demand

else if(s[plc]<d[0])

{

//cal of sum

sum=sum+(c[plc][0]*s[plc]);

//diff cal

d[0]=d[0]-s[plc];

//shifting of row and supply

for(i=1;i<col+1;i++)

{

for(j=plc+1;j<row;j++)

{

c[j-1][i-1]=c[j][i-1];

}

}

for(j=plc+1;j<row;j++)

s[j-1]=s[j];

row–;

if(row<0)

goto end;

}

}

goto start;

end:

cout<<“\nTransportation cost is “<<sum<<” units”;

}

void main()

{

c_min c;

clrscr();

c.get();

c.cal();

getch();

}

/******************************************

OUTPUT:

Transportation Problem Is

15 20 25 | 5

30 40 36 | 7

12 18 9 | 7

———————————————–

8 6 5 |

SOLUTION:-

Transportation cost is 439 units

**********************************************/

Leave a Reply

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