//North West Corner Method in transportation problem
#include<stdio.h>
main()
{
int res,des,*s,*d,**cell,**cost,i,j,sumreq=0,sumdes=0,c=0,total=0;
printf("Enter number of resources: ");
scanf("%d",&res);
printf("Enter the number of destinations: ");
scanf("%d",&des);
//memory allocation
s=(int *)malloc(res*sizeof(int));
d=(int *)malloc(des*sizeof(int));
cell=(int **)malloc(res*sizeof(int *));
for(i=0;i<res;i++)
cell[i]=(int *)malloc(des*sizeof(int));
cost=(int **)malloc(res*sizeof(int *));
for(i=0;i<res;i++)
cost[i]=(int *)malloc(des*sizeof(int));
for(i=0;i<res;i++)
for(j=0;j<des;j++)
cell[i][j]=-1;
//memory allocated
//data entering
for(i=0;i<res;i++)
{
printf("Enter the %dth resource availability: ",i+1);
scanf("%d",&s[i]);
sumreq+=s[i];
}
for(i=0;i<des;i++)
{
printf("Enter the %dth destination requirement: ",i+1);
scanf("%d",&d[i]);
sumdes+=d[i];
}
for(i=0;i<res;i++)
for(j=0;j<des;j++)
{
printf("Enter the cost %d %d :",i,j );
scanf("%d",&cost[i][j]);
}
//data entry complete
//nwc method starts
if(sumreq==sumdes)
{
i=0;
j=0;
while(i<res&&j<des)
{
if(s[i]<d[j])
{
cell[i][j]=s[i];
d[j]=d[j]-s[i];
s[i]=s[i]-s[i];
i++;
}
else
{
cell[i][j]=d[j];
s[i]=s[i]-d[j];
d[j]=d[j]-d[j];
j++;
}
}
for(i=0;i<res;i++)
for(j=0;j<des;j++)
{
if(cell[i][j]!=-1)
{
printf("x[%d][%d]=%d ",i,j,cell[i][j]);
total+=cell[i][j]*cost[i][j];
}
}
printf("\nTotal transportation cost is %d",total);
}
else
printf("Not a balanced problem");
getch();
}
#include<stdio.h>
main()
{
int res,des,*s,*d,**cell,**cost,i,j,sumreq=0,sumdes=0,c=0,total=0;
printf("Enter number of resources: ");
scanf("%d",&res);
printf("Enter the number of destinations: ");
scanf("%d",&des);
//memory allocation
s=(int *)malloc(res*sizeof(int));
d=(int *)malloc(des*sizeof(int));
cell=(int **)malloc(res*sizeof(int *));
for(i=0;i<res;i++)
cell[i]=(int *)malloc(des*sizeof(int));
cost=(int **)malloc(res*sizeof(int *));
for(i=0;i<res;i++)
cost[i]=(int *)malloc(des*sizeof(int));
for(i=0;i<res;i++)
for(j=0;j<des;j++)
cell[i][j]=-1;
//memory allocated
//data entering
for(i=0;i<res;i++)
{
printf("Enter the %dth resource availability: ",i+1);
scanf("%d",&s[i]);
sumreq+=s[i];
}
for(i=0;i<des;i++)
{
printf("Enter the %dth destination requirement: ",i+1);
scanf("%d",&d[i]);
sumdes+=d[i];
}
for(i=0;i<res;i++)
for(j=0;j<des;j++)
{
printf("Enter the cost %d %d :",i,j );
scanf("%d",&cost[i][j]);
}
//data entry complete
//nwc method starts
if(sumreq==sumdes)
{
i=0;
j=0;
while(i<res&&j<des)
{
if(s[i]<d[j])
{
cell[i][j]=s[i];
d[j]=d[j]-s[i];
s[i]=s[i]-s[i];
i++;
}
else
{
cell[i][j]=d[j];
s[i]=s[i]-d[j];
d[j]=d[j]-d[j];
j++;
}
}
for(i=0;i<res;i++)
for(j=0;j<des;j++)
{
if(cell[i][j]!=-1)
{
printf("x[%d][%d]=%d ",i,j,cell[i][j]);
total+=cell[i][j]*cost[i][j];
}
}
printf("\nTotal transportation cost is %d",total);
}
else
printf("Not a balanced problem");
getch();
}