Wednesday, December 5, 2018

C code review implement Operation Research :Transportation Problem

This C code shows how to implement Operation Research :Transportation Problem. And code is helpful while user is in between any algorithm-like manipulation task.

code:

//Tested OK on Code::Blocks IDE 17.12 & Cygwin+GCC tools.
/*
Operation Research :Transportation Problem
This transportation Problem will give the estimate the cost of the
transportation.It consists of the supply and Demand.There are two
conditions
1)if supply greater than demand then element values * demand and
supply will be subtracted from demand
2)If suppply less than demand then element values * supply and demand
will be subtracted from supply.
Finally cost matrix is printed and the Sum of the allocated Matrix can
be also calculated.The final sum is the Transportation Cost
*/

/* Look last of coding for SAMPLE INPUT*/
#include<stdio.h>
//#include<conio.h>

void main()
{
    int flag=0,flag1=0;
    int s[10],d[10],sn,eop=1,dm,a[10][10];
    int i,j,sum=0,min,x[10][10],k,fa,fb;

//    clrscr();
    /* Getting The Input For the Problem*/

    printf("\nEnter the number of Supply : ");
    scanf("%d",&sn);
    printf("\nEnter the number of Demand : ");
    scanf("%d",&dm);
    printf("\nEnter the Supply Values    : ");
    for(i=0; i<sn; i++)
        scanf("%d",&s[i]);
    printf("\nEnter the Demand Values    : ");
    for(j=0; j<sn; j++)
        scanf("%d",&d[j]);
    printf("\nEnter the elements of the array \n");
    for(i=0; i<sn; i++)
    {
        for(j=0; j<dm; j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    /* Calculation For the Transportation */
    i=0;
    j=0;
    for(i=0,j=0; i<sn,j<dm;)
    {
        if(s[i]<d[j])          // Check supply less than demand
        {
            x[i][j]=a[i][j]*s[i];  // Calculate  amount * supply
            d[j]=d[j]-s[i];        // Calculate demand - supply
            i++;  // Increment i for the deletion of the row or column
        }
        //Check the supply greater than equal to demand
        else if(s[i]>=d[j])
        {
            x[i][j]=a[i][j]*d[j];      // Calculate  amount * demand
            s[i]=s[i]-d[j];           // Calculate supply - demand
            j++;  // Increment j for the deletion of the row or column
        }

    }
    /* The  Cost Matrix is Estimated here */
    printf("\nGiven Cost Matrix is :\n");
    for(fa=0; fa<sn; fa++)
    {
        for(fb=0; fb<dm; fb++)
        {
            printf("%d    ",a[fa][fb]);
        }
        printf(" ");
    }
    /*  The Allocated Cost Matrix is */

    printf("\nAllocated Cost Matrix is \n");
    for(fa=0; fa<sn; fa++)
    {
        for(fb=0; fb<dm; fb++)
        {
            printf("%d    ",x[fa][fb]);
            sum=sum+x[fa][fb];
        }
        printf(" ");
    }
    /* Transportation Cost Estimated and Sum is Printed*/
    printf("\nThe Transportation cost:%d \n\n",sum);
    getchar();
}

/* SAMPLE INPUT
11
13
17
14
16
18
14
10
21
24
13
10
11
11
11
11
Given Cost Matrix is :
11      13      17      14
16      18      14      10
21      24      13      10
11      11      11      11
Allocated Cost Matrix is
2200    650     0       0
0       3150    700     0
0       0       2925    500
0       0       0       2200
The Transportation cost:12325
*/

//end of program

To see such C code reviews refer blog archieve at right side or click CCode lable for till posted posts list.

...till next post, bye-bye and take care.

No comments:

Post a Comment