![]() |
|
#1
|
|||
|
|||
Finding mode in array of numbersHello,
I can't seem to figure out how to get the mode from an array. I'm doing this program in pointers, and am confused because I'm fairly new to pointers. Could anyone help me out? The problem is in the findMode function. CPP / C++ / C Code:
|
|
#2
|
||||
|
||||
Re: Finding mode in array of numbersHi SnowCat,
Welcome to the GIDForums. Please explain more about what is the mode. You must return a value at the end of the function. So, Instead of only using "return" you should return like this: return mode; And why do you define the function return type as float? Have it as int. Regards, Paramesh. __________________
Don't walk in front of me, I may not follow. Don't walk behind me, I may not lead. Just walk beside me and be my friend. |
|
#3
|
|||
|
|||
Re: Finding mode in array of numbersthe mode is the number which occurs most frequently in the set. your first problem is in
CPP / C++ / C Code:
anyways, about the function itself. it seems to me that it should be declared as returning float, but then you shoudl return a float. but then the local variables lastnum and mode need to be float. hint: you could make this program a lot simpler by using all int values instead of floats, which bring in lots of subtle issues such as accuracy etc. it seems to me that your findMode() function should be returning the variable mode. but what's this about testing for -1 return value from findMode()? you never do that in the function itself. it seems to me that at the end you should see if the "mode" is still the first element, and if the curlen is still 1. in that case, there is no mode and return -1. btw, you have a memory leak. whenever you allocate something with new, you must release it back to the OS with delete. and when it's an array with new[], like in your program, don't forget to do CPP / C++ / C Code:
one more thing -- once you get this program working, you should add some error checking, especially for NULL pointers. hth and good luck, ubergeek |
|
#4
|
|||
|
|||
Re: Finding mode in array of numbersQuote:
I see a fundamental problem with program design (or maybe it is terminology). What do you mean by "No mode"? The usual mathematical definition of "mode" is something like: The mode of a sequence of numbers is the most common (frequent) value. In general, a list can have more than one mode. Consider: 1, 1, 3, 3, 4, 5, 6, 7 There are two modes: 1 and 3 (both have a frequency of two, and no member of the list has a frequency greater than two). Now consider the list: 1, 2, 3, 4, 5, 6, 7 How many modes are there? (Left as an exercise for the student: Is there room for interpretation here?) Now, for your function. How would it return a value indicating something about the nature of the mode, and also return the value(s) of the mode(s)? What is the mode of this sequence? -1, -1, -1, 2, 3, 4 It's -1, right? Then how the heck can your can the calling function distinguish between a list whose mode is -1 and a list that has whatever properties that you will call "No mode.", since you use -1 as an indicator of that condition? Or are you dealing with lists for which -1 is not allowed as a member? Or what? My recommendation is 1. Write a Program Specification that tells exactly what the function will do (what about single mode lists, multi-mode lists, no-mode lists --- what?) How is the function going to tell the calling program two things? (A condition that you call "No mode" and the value of the mode, if it exists according to your specification.) 2. Make up a few lists that you can use to test the program. 3. Implement the function. Regards, Dave |
Recent GIDBlog
Flickr uploads of IA pictures by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| sorting numbers through array | zidanefreak01 | C++ Forum | 3 | 26-Jun-2005 02:41 |
| Help with an array using pointers | glulu76 | C++ Forum | 1 | 01-May-2005 11:21 |
| duplicate numbers in array? | njypa | C++ Forum | 1 | 27-Feb-2005 16:08 |
| template comiling problems - need expert debugger! | crq | C++ Forum | 1 | 01-Feb-2005 21:26 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The