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