1. Sorting numbers
- Posted by msuclay at hotmail.com Mar 21, 2001
- 454 views
- Last edited Mar 22, 2001
Hello all, I would first like to say thanks for the great forum and such a fun programming language. Again, I am a beginner and would like to get some help. I am sure that this is very simple for most of you guys, but I am stuck. What I am trying to do is input 24 numbers and sum them together. Once I have the total, I need to figure out the largest number and subtract it from the total. I figured the best way to do it is to sort the numbers from least to greatest and grab the last number, however this is my problem. Here is the code that I was working on before I got stuck. global procedure onClick_sparebutton() atom AB1, AB2, AB3, AB4, AB5, AB6, AB7, AB8 atom BC1, BC2, BC3, BC4, BC5, BC6, BC7, BC8 atom CA1, CA2, CA3, CA4, CA5, CA6, CA7, CA8 atom sumAB, sumBC, sumCA, sumtotal atom sparenumber object y AB1 = getNumber( ab1 ) AB2 = getNumber( ab2 ) AB3 = getNumber( ab3 ) AB4 = getNumber( ab4 ) AB5 = getNumber( ab5 ) AB6 = getNumber( ab6 ) AB7 = getNumber( ab7 ) AB8 = getNumber( ab8 ) BC1 = getNumber( bc1 ) BC2 = getNumber( bc2 ) BC3 = getNumber( bc3 ) BC4 = getNumber( bc4 ) BC5 = getNumber( bc5 ) BC6 = getNumber( bc6 ) BC7 = getNumber( bc7 ) BC8 = getNumber( bc8 ) CA1 = getNumber( ca1 ) CA2 = getNumber( ca2 ) CA3 = getNumber( ca3 ) CA4 = getNumber( ca4 ) CA5 = getNumber( ca5 ) CA6 = getNumber( ca6 ) CA7 = getNumber( ca7 ) CA8 = getNumber( ca8 ) sumAB = AB1 + AB2 + AB3 + AB4 + AB5 + AB6 + AB7 + AB8 sumBC = BC1 + BC2 + BC3 + BC4 + BC5 + BC6 + BC7 + BC8 sumCA = CA1 + CA2 + CA3 + CA4 + CA5 + CA6 + CA7 + CA8 sumtotal = sumAB + sumBC + sumCA I relize it is very imcomplete, but I just don't understand the sort function
2. Re: Sorting numbers
- Posted by Jeffrey Fielding <JJProg at cyberbury.net> Mar 21, 2001
- 463 views
- Last edited Mar 22, 2001
If that's all you need to do, you don't even need to sort them. Here's how I would go about it: constant INPUTS = {ab1,ab2,ab3,ab4,ab5,ab6,ab7,ab8, bc1,bc2,bc3,bc4,bc5,bc6,bc7,bc8, ca1,ca2,ca3,ca4,ca5,ca6,ca7,ca8} global procedure onClick_sparebutton() atom sumtotal, max, value sumtotal = 0 max = 0 for i = 1 to 24 do value = getNumber(INPUTS[i]) sumtotal += value if value > max then max = value end if end for sumtotal -= max -- Subtract the largest number end procedure Jeff Fielding On Wednesday 21 March 2001 22:10, you wrote: > > > Hello all, I would first like to say thanks for the great forum and such a > fun programming language. Again, I am a beginner and would like to get > some help. I am sure that this is very simple for most of you guys, but I > am stuck. What I am trying to do is input 24 numbers and sum them > together. Once I have the total, I need to figure out the largest number > and subtract it from the total. I figured the best way to do it is to sort > the numbers from least to greatest and grab the last number, however this > is my problem. Here is the code that I was working on before I got stuck. > > > > global procedure onClick_sparebutton() > atom AB1, AB2, AB3, AB4, AB5, AB6, AB7, AB8 > atom BC1, BC2, BC3, BC4, BC5, BC6, BC7, BC8 > atom CA1, CA2, CA3, CA4, CA5, CA6, CA7, CA8 > atom sumAB, sumBC, sumCA, sumtotal > atom sparenumber > object y > > AB1 = getNumber( ab1 ) > AB2 = getNumber( ab2 ) > AB3 = getNumber( ab3 ) > AB4 = getNumber( ab4 ) > AB5 = getNumber( ab5 ) > AB6 = getNumber( ab6 ) > AB7 = getNumber( ab7 ) > AB8 = getNumber( ab8 ) > BC1 = getNumber( bc1 ) > BC2 = getNumber( bc2 ) > BC3 = getNumber( bc3 ) > BC4 = getNumber( bc4 ) > BC5 = getNumber( bc5 ) > BC6 = getNumber( bc6 ) > BC7 = getNumber( bc7 ) > BC8 = getNumber( bc8 ) > CA1 = getNumber( ca1 ) > CA2 = getNumber( ca2 ) > CA3 = getNumber( ca3 ) > CA4 = getNumber( ca4 ) > CA5 = getNumber( ca5 ) > CA6 = getNumber( ca6 ) > CA7 = getNumber( ca7 ) > CA8 = getNumber( ca8 ) > > sumAB = AB1 + AB2 + AB3 + AB4 + AB5 + AB6 + AB7 + AB8 > sumBC = BC1 + BC2 + BC3 + BC4 + BC5 + BC6 + BC7 + BC8 > sumCA = CA1 + CA2 + CA3 + CA4 + CA5 + CA6 + CA7 + CA8 > sumtotal = sumAB + sumBC + sumCA > > > y=sort({AB1,AB2,AB3,AB4,AB5,AB6,AB7,AB8,BC1,BC2,BC3,BC4,BC5,BC6,BC7,BC8,CA1 >,CA2,CA3,CA4,CA5,CA6,CA7,CA8}) > > > > I relize it is very imcomplete, but I just don't understand the sort > function > > >
3. Re: Sorting numbers
- Posted by Irv Mullins <irvm at ellijay.com> Mar 21, 2001
- 482 views
- Last edited Mar 22, 2001
On Wed, 21 Mar 2001, msuclay at hotmail.com wrote: > Hello all, I would first like to say thanks for the great forum and such a > fun programming language. Again, I am a beginner and would like to get some > help. I am sure that this is very simple for most of you guys, but I am > stuck. What I am trying to do is input 24 numbers and sum them together. > Once I have the total, I need to figure out the largest number and subtract > it from the total. I figured the best way to do it is to sort the numbers > from least to greatest and grab the last number, however this is my problem. > Here is the code that I was working on before I got stuck. <snip> First of all, it would simplify things if you used an array: Writing long lists of variables is messy and error prone, and can cause naming collisions when programs get large. sequence nums nums = repeat( 0, 24 ) -- create and initialize to zero atom AB,BC,CA, total, max -- storage for category totals AB = 0 BC = 0 CA = 0 Get the numbers, nums[1] = getNumber(ab1) nums[2] = getNumbers(ab2)....and so on Then to get the category totals, use a loop: for i = 1 to 8 do AB += nums[i] BC += nums[i + 8] CA += nums[i + 16] end for Now, get the highest number in the list, using a sort: nums = sort(nums) max = nums [14] total = AB + BC + CA - max -- Regards, Irv