Results 1 to 2 of 2
- 03-15-2010, 02:51 AM #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
- 03-15-2010, 10:30 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 953
- Rep Power
- 4
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:
Of course if remaining demand and/or remaining supply is zero, you still go through the steps. Hope that makes sense.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
-Gary-
Similar Threads
-
help with java coding
By helpisontheway in forum New To JavaReplies: 4Last Post: 11-14-2009, 07:00 AM -
java coding
By zawad in forum Suggestions & FeedbackReplies: 1Last Post: 07-07-2009, 01:55 PM -
Developer III - JAVA; North Texas area
By ScottSearch in forum Jobs OfferedReplies: 0Last Post: 09-22-2008, 09:06 PM -
View Corner
By BogdanNb in forum SWT / JFaceReplies: 0Last Post: 07-25-2008, 02:44 PM -
Java/Application Developers Needed - North Carolina
By brown2sl in forum Jobs OfferedReplies: 0Last Post: 03-10-2008, 07:17 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks