RE: Army Composition
- Posted by Matthew Lewis <matthewwalkerlewis at YAHOO.COM> Oct 12, 2001
- 428 views
-----Original Message----- From: C. K. Lester [mailto:cklester at yahoo.com] >I had to modify your program for a few typos... and I made a few other >aesthetic changes. HOWEVER, it doesn't work properly... Check this out: That's what I get for coding in an email. I made some changes. Here's what I came up with: include get.e sequence required, amount, actual, ranks atom pop, startpop object junk pop = rand(1000000) pop = 20000 -- for testing purposes startpop = pop required = {1,20,10,10,5} ranks = { "Soldier", "Squad Leader","Lieutenant","Captain","General" } amount = repeat(1,length(required)) actual = repeat(0,length(required)) function product( sequence mult ) atom p p = 1 for i = 1 to length( mult ) do p *= mult[i] end for return p end function -- first calculate required number for each rank atom sum --for i = length(required) to 1 by -1 do for i = length(required) to 1 by -1 do for j = 2 to i do sum = product( required[j..i] ) amount[i] += sum end for end for atom quo integer ix ix = length( actual ) -- next calculate amount of each for a population while ix and pop do if ix < length( actual ) then actual[ix+1] += 1 pop -= 1 -- account for overflow end if quo = floor( pop / amount[ix] ) sum = quo actual[ix] += sum pop -= sum for i = ix-1 to 1 by -1 do sum *= required[i+1] pop -= sum actual[i] += sum end for --pop = remainder( pop , amount[ix] ) ix -= 1 end while printf(1,"\nFor a population of %d\n\n",startpop) for t=1 to length(actual) do printf(1,"%15s\t%5d\n",{ranks[t],actual[t]}) end for pop = 0 for i = 1 to length(actual) do pop += actual[i] end for printf(1,"%21d\n",pop) junk = wait_key()