Results 1 to 2 of 2
  1. #1
    ytcheng is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default need help with Java coding for North West Corner algorithm

    :confused:Hi,
    I wrote a code to generate a table that reflects the supply of factory and demand of warehouse. here's the data

    Factory Supply warehouse demand
    Factory 1 100 Warehouse 1 150
    Factory 2 200 Warehouse 2 180
    Factory 3 300 Warehouse 3 270

    I am suppose to write a program that would generate a table which looks like this
    100 0 0
    50 150 0
    0 30 270

    where the rows represent the supply and the columns represent the demand. Sum of row 1 equals the supply of factory 1 and so on. The sum of column 1 equals the demand of warehouse 1 and so on. I have attempted to write the program but it is not generating the table that I want. Please point out to me where the problem is and how it can be fix.


    public class SD {
    public static void main(String []args){
    int []s={100,200,300};
    int[]d={150,180,270};
    System.out.println("s= "+s.length);
    System.out.println("d= "+d.length);

    int [][]calc=new int[s.length][d.length];
    int sums=0;
    int sumd=0;
    for(int i=0;i<=s.length-1;i++)
    { int totalsupply=s[i];

    for(int j=0;j<=d.length-1;j++)
    { int totaldemand=d[j];

    if(s[i]<=d[j])
    { calc[i][j]=s[i];
    calc[i+1][j]=d[j]-s[i];
    calc[0][1]=0;
    calc[0][2]=0;
    sums+=calc[i][j];
    }
    System.out.print(calc[i][j]+" ");
    }
    System.out.println("\n");
    }
    }
    //output I get is this
    //s= 3
    //d= 3
    //100 0 0

    //50 80 200

    //0 0 70

  2. #2
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    You have your array of arrays and nested loop set up properly. Now when you calculate your cells, you want to work on only the cell calc[i][j] during that iteration of the loop. in pseudocode:
    Java Code:
    for (each factory)
        for (each warehouse)
            if (remaining demand for this factory can be filled from this warehouse)
                quantity q = remaining demand from this factory
            else
                quantity q = remaining supply in this warehouse
            enter q in this cell
            subtract q from this factory's demand
            subtract q from this warehouse's supply
        next warehouse
    next factory
    Of course if remaining demand and/or remaining supply is zero, you still go through the steps. Hope that makes sense.

    -Gary-

Similar Threads

  1. help with java coding
    By helpisontheway in forum New To Java
    Replies: 4
    Last Post: 11-14-2009, 08:00 AM
  2. java coding
    By zawad in forum Suggestions & Feedback
    Replies: 1
    Last Post: 07-07-2009, 02:55 PM
  3. Developer III - JAVA; North Texas area
    By ScottSearch in forum Jobs Offered
    Replies: 0
    Last Post: 09-22-2008, 10:06 PM
  4. View Corner
    By BogdanNb in forum SWT / JFace
    Replies: 0
    Last Post: 07-25-2008, 03:44 PM
  5. Java/Application Developers Needed - North Carolina
    By brown2sl in forum Jobs Offered
    Replies: 0
    Last Post: 03-10-2008, 08:17 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •