Powered By Blogger

Wednesday 27 April 2011

North West Corner Rule in C


#include<stdio.h>
#include<stdlib.h>
int main(void)
{
int i=0,j=0,k=0,r=0,c=0,max=0,min=0,col=0,row=0;
int **a,flag=0,supply=0,demand=0,sum=0,temp=0,count=0,mr=0,mc=0;
printf("\n************* NORTH WEST CORNER RULE *************\n");
printf("\nPlease enter the order of the first matrix:\n");
scanf("%d",&mr);
printf("X\n");
scanf("%d",&mc);
r=mr+1;
c=mc+1;
a=(int**)malloc(r*sizeof(int*));
for(i=0;i<r;i++)
{
a[i]=(int*)malloc(c*sizeof(int));
}
printf("\nEnter the element of the matrix:\n");
for(i=0;i<mr;i++)
{
for(j=0;j<mc;j++)
{
scanf("%d",&a[i][j]);
}
}

printf("\nEnter the Supply:\n");
for(i=0;i<mr;i++)
{
scanf("%d",&a[i][mc]);
}
printf("\nEnter the Demand:\n");
for(j=0;j<mc;j++)
{
scanf("%d",&a[mr][j]);
}
a[mr][mc]=a[0][mc]+a[1][mc]+a[2][mc];

printf("\n\nThe cost matrix is given bellow::\n\n");



for(i=0;i<r;i++)
{
for(j=0;j<c;j++)
{
printf("%5d",a[i][j]);
}
printf("\n");
}



do
{
count=0;
flag=0;

for(i=0;i<(r-1);i++)
{
for(j=0;j<(c-1);j++)
{
if((a[i][c-1]!=0)&&(a[r-1][j]!=0)&&flag==0)
{
min=a[i][j];
row=i;
col=j;
flag=1;
count++;
}

}
}
supply=a[row][c-1];
demand=a[r-1][col];
if(supply>=demand)
{
temp=supply-demand;
a[row][col]=a[row][col]*demand;
sum=sum+a[row][col];
a[row][c-1]=temp;
a[r-1][col]=0;
}
if(supply<demand)
{
temp=demand-supply;
a[row][col]=a[row][col]*supply;
sum=sum+a[row][col];
a[row][c-1]=0;
a[r-1][col]=temp;
}
}while(count!=0);

printf("\n\nTOTAL COST :: %d\n\n",sum);
getch();
return 0;
}

4 comments:

  1. can you show me the out for this program...........

    ReplyDelete
  2. This comment has been removed by the author.

    ReplyDelete
  3. My code runs fine. Check it out here :
    http://ideone.com/TIgh97

    or code :

    #include
    using namespace std;
    int main()
    {
    int m,n;
    cout<<"Enter No of Rows(Supply) and Columns(Demands) :\n";
    cin>>m;//3
    cin>>n;//4
    int arr[m][n];
    int sum=0;
    int i,j;

    cout<<"Enter the weights in the array (Supply X Demands) :\n";

    for(i=0;i>arr[i][j];
    }
    }


    int s[m],d[n]; //S-->Supply values D-->Demand Values

    for(i=0;i>s[i];
    }


    for(j=0;j>d[j];
    }

    int check=1; //when check becomes zero it is the termination condition
    int tm=0; //temporary value of 'm' that is used to get values from matrix
    int tn=0; //temporary value of 'n' that is used to get values from matrix
    while(check==1)
    {

    if(d[tn]==s[tm])
    {
    if((tm!=m-1)||(tn!=n-1))
    {
    sum+=arr[tm][tn]*d[tn];
    tm++;
    tn++;
    check=1;
    }
    else
    {
    sum+=arr[tm][tn]*d[tn];
    check=0;
    }
    }
    else if(d[tn]s[tm])
    {
    if(tm==m-1&&tn!=tn-1)
    {
    sum+=arr[tm][tn]*s[tm];
    d[tn]=d[tn]-s[tm];
    s[tm]=0;
    tn++;
    check=1;
    }
    else if(tm!=m-1&&tn==tn-1)
    {
    sum+=arr[tm][tn]*s[tm];
    d[tn]=d[tn]-s[tm];
    s[tm]=0;
    tm++;
    check=1;
    }
    else if(tm==m-1&&tn==n-1)
    {
    check=0;
    }
    else
    {
    sum+=arr[tm][tn]*s[tm];
    d[tn]=d[tn]-s[tm];
    s[tm]=0;
    tm++;
    check=1;
    }
    }
    }
    cout<<"\nTotal Cost calculated using North-West Corner rule :"<<sum;
    }


    /*
    Example 1:
    Enter No of Rows(Supply) and Columns(Demands) :
    3 3
    Enter the weights in the array (Supply X Demands) :
    6 8 10
    7 11 11
    4 5 12
    Supply[1] :150
    Supply[2] :175
    Supply[3] :275
    demand[1] :200
    demand[2] :100
    demand[3] :300

    Total Cost calculated using North-West Corner rule :5925

    Example 2:
    Enter No of Rows(Supply) and Columns(Demands) :
    3 3
    Enter the weights in the array (Supply X Demands) :
    4 5 8
    6 2 8
    3 7 9
    Supply[1] :100
    Supply[2] :200
    Supply[3] :200
    demand[1] :200
    demand[2] :200
    demand[3] :100

    Total Cost calculated using North-West Corner rule :2800

    Example 3:
    Enter No of Rows(Supply) and Columns(Demands) :
    3 4
    Enter the weights in the array (Supply X Demands) :
    3 5 7 6
    2 5 8 2
    3 6 9 2
    Supply[1] :50
    Supply[2] :75
    Supply[3] :25
    demand[1] :20
    demand[2] :20
    demand[3] :50
    demand[4] :60

    Total Cost calculated using North-West Corner rule :670

    Example 4:
    Enter No of Rows(Supply) and Columns(Demands) :
    3 4
    Enter the weights in the array (Supply X Demands) :
    12 13 4 6
    6 4 10 11
    10 9 12 4
    Supply[1] :500
    Supply[2] :700
    Supply[3] :800
    demand[1] :400
    demand[2] :900
    demand[3] :200
    demand[4] :500

    Total Cost calculated using North-West Corner rule :14200

    Example 5:
    Enter No of Rows(Supply) and Columns(Demands) :
    3 4
    Enter the weights in the array (Supply X Demands) :
    1 2 1 4
    3 3 2 1
    4 2 5 9
    Supply[1] :30
    Supply[2] :50
    Supply[3] :20
    demand[1] :20
    demand[2] :40
    demand[3] :30
    demand[4] :10

    Total Cost calculated using North-West Corner rule :310
    */

    ReplyDelete
  4. Hello, have any implemented Stepping Stone and MODI method in C? Need the help.

    ReplyDelete