Tortorella's Rant Posted September 23, 2012 Share Posted September 23, 2012 Maybe it's asking a lot, but.. I'm brand new to this with no prior experience or understanding so I really have no idea what the hell I'm doing. This isn't homework, just practice. I've figured out a couple very simple algorithms on my own. But there are a couple questions that have no examples or explanations in the book, such as: Mode: Design an algorithm that finds the mode of a list of numbers. Ensure your algorithm can handle multi-modal cases. 1. Present your answer in pseudo-code I kinda get how pattern matching and largest value works. But when they ask for reverse list order and mode, I dunno how to make an algorithm that would work. Maybe someone could provide an example? Link to comment Share on other sites More sharing options...
Drive-By Body Pierce Posted September 23, 2012 Share Posted September 23, 2012 Sounds like it might be for your homework, but I don't mind helping anyway. There are many ways you could implement this, I will try to explain a method that I hope is easy to understand. The list of numbers should be stored in an array. This inherently assigns an index or position to each number. Potentially the array could contain all unique numbers, making each number appearing in the array the mode. Consequently, the resulting mode should also be an array which is the same length as the list of numbers you originally started with. When cycling through the original list, store each unique value in this resulting array. To keep track of the number of times each occurred, you could make a third array. Ensure that the values stored in this array for number of occurrences have the same index as the corresponding unique number in the resulting mode array. Link to comment Share on other sites More sharing options...
Drive-By Body Pierce Posted September 23, 2012 Share Posted September 23, 2012 I'm gonna assume you know some basics of mathematics and programming...what for loops, if statements, variable types are and that arrays start at 0 for most languages Say this is your list: [9 7 3 7 4 5 7 4 3 8] The following could be pseudo code to find the mode. list[10] = [9 7 3 7 4 5 7 4 3 8]; //create the array for the original list mode[length(list)]; //create an empty array to store the resulting unique numbers, that is the length of the original list mode_count[length(list)]; //create an empty array to store the number of occurrences of the resulting unique numbers, same length for i = 0 to length(list)-1 mode_count[i] = 0; //fill the empty count array with 0's end for i bool exists = false; //boolean to keep track of if the number already exists for i = 0 to length(list)-1 exists = false; for j = 0 to length(list)-1 if (list[i] == mode[j]) mode_count[j] = mode_count[j] + 1; //found the number already, increment the count exists = true; end if end for j if (exists == false); mode[i] = list[i]; mode_count[i] = 1; //didn't find the number already, add it to the result list end if end for i int max_loc = 0; //variable to store the location of the maximum occurrence for i = 0 to length(list)-1 if (mode_count[i] != null && mode_count[i] > mode_count[max_loc]) max_loc = i; //search and store the location of the max occurrence end if end for i for i = 0 to length(list)-1 if (mode_count[i] == mode_count[max_loc]) print (mode[i]) //prints all numbers with the max occurrence end if end for i The arrays should then look like this (assuming i didn't make any errors above): mode = [9 7 3 null 4 5 null null null 8] mode_count = [1 3 2 0 2 1 0 0 0 1] max_loc = 1 This is certainly not the most efficient way of going about this, but I think its a good way to illustrate the required concepts. Let me know if you have any questions, I'll be around of a little while. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.