# Thread: need help with Java coding for North West Corner algorithm

1. Member
Join Date
Mar 2010
Posts
1
Rep Power
0

## 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. Senior Member
Join Date
Mar 2010
Posts
952
Rep Power
11
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-

#### Posting Permissions

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