P r o g r a m m i n g - a - M o d e - F i n d e r -a guide to programming a mode finder- By: Brian Gordon ____________ /Introduction\_________________________________ | | |This guide will outline the technique I found | |is the best way to determine a mode from a | |set of data. It includes most of the source | |code from the original program, then explains | |it in almost-understandable English. | |I use -> for STO and & for the Theta letter. | \______________________________________________| First thing in the program is initialization: ClrHome DelVar L1 DelVar L3 DelVar L6 These first 4 lines clear a few lists (the input, a middleman list, and the results). Input L1 dim(L1)->A A->dim(L2) For(X,1,A,1) For(Y,1,A,1) If (L1(X)=L1(Y) 1+L2(X)->L2(X) End:End Takes input to L1, the dimensision of which is then stored to A. A then becomes the length of L2, the "high-score" list. AASTAT grades which number is used the most (ie the mode) by scoring it against how often other numbers are used. Here, a double loop is made to step through every element in the list and check it against every other element in the list. This returns a lot of double and triple matches, but at least it gets them all. If the element matches another, L2 is accessed. Remember, this is the scoreboard list. Say the user entered {1,2,7,8,3,4,0,8} so that L1(4) matches with L1(7). In that case, L2(4) (the scorekeeper element for 8 is increased. It is now a simple idea to see which element in L2 has the highest score and follow that element number over to L1, and there's your winner. Problem is repeat modes. There will be at least 3 or 4 repeats of the same mode right now. If you were to disp L2 right now, you would get: {1 1 1 2 1 1 1 2} So, tracing that back to L1, it looks like 1,2,7,3,4,0 all have 1 point and 8 has 2. Now we need to make a for loop that finds the highest-scoring element. Unfortunately, since there are two 8s, both with a score of 2, we then need to make some For loops that take into account the number of 8s and match it with the amount of highest-scoring elements. L2(1)->& For(Z,1,A,1 If (L2(Z)>&) L2(Z)->& End Ooh, scary. First off, & is initialized to be the first element in L2. This For loop is going to find the highest element in L2, so & is used to keep track of the highest score found so far (not the -number- of the highest-scoring element). Therefore, the first element is used as the initial highest score. The For loop steps through L2, Z the number of each element being checked during that particular pass. It checks whether the current score being checked is greater than the highest score found so far (stored in &). If it is, then it becomes the highest score by being stored to &. This loop repeats until every element in L2 has been checked against the high score. After the loop is finished, & contains the highest score found. However, the element numbers (yes that's a plural; remember that multiple elements can share that same top score... multiple modes) themselves ARE NOT STORED. It is utterly unfeasable to combine this step and the next step: finding the winning element numbers and storing them in a compact list. 1->O For(P,1,dim(L2),1) If (L2(P)=&):Then L1(P)->L3(O) O+1->O End End SortA(L3) The For loop steps through L2 again, using P now instead of Z. Each time through it checks whether the current element, L2(P), matches the top score. If it does, then we know that L1(P) is a winner by association. In other words, if L2(P) is the top score, then it's corresponding L1 element, L1(P), is the actual number that is one of the modes. L1(P) is then stored to L3(O), the next empty element in L3, for later use. O is incremented for the next pass and the loop starts over. At the end of this loop, you have a new list, L3. Sort it ascending in anticipation for later in the program (trust me here). Stop there and Disp L3. You see that L3 is {8 8}. Of course there's only one mode here, but as mentioned before, it's repeated because the user entered -two- 8s so they both scored 2. 0->M For(Q,1,dim(L3),& M+1->M M->dim(L6 L3(Q)->L6(M) End The reasoning within this For loop lies within a brainload of conceptual math done by me. The lowdown: if the top score (&) is 2, there are 2 repeats for -each correct mode- in L3... that's a huge Duh. Think the Handshakes problem. If & is 8, it means that there are 8 repeats for -each correct mode- in L3. So the theory is that we can somehow splice the correct ones out. We do that by only looking at every &th element in L3. This serves to operate on only the correct elements (eliminating repeats) and at the same time takes care of the problem of multiple modes, since at this point the other modes obviously must have the same score: &. Using a similar costruct as the last For loop, the loop runs through L3 at increments of &, each time storing the current L3 element to the next free element of L6. Add this For loop to your code and run it. Then Disp L6. { 8 } is the output. Congratulations, you just computed a mode. One final consideration: there are 2 types of NO MODE when computing with this technique: 1) dim(L1)=dim(L6) - this is the "standard" NO MODE where all the numbers are different. 2) dim(L1)=dim(L3) - this is a more complex NO MODE when L2 is solidly one score. An example would be if the user entered: {7,7,7,8,8,8,5,5,5}. There is no value with a lower score than the highest score, therefore technically this is a NO MODE, however the program will display {5,7,8} as the mode. Please add these two conditions to your program. For example, instead of writing Disp L6, write: If (dim(L1)!=dim(L6) and dim(L1)!=dim(L3)) Disp L6 != is the not-equal-to-sign found on the Test menu. I hope you learned a ton in this tutorial and I urge you to learn to do this on your own, without a tutorial. Copyright 2004 by Brian Gordon