GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 16-Jan-2006, 17:43
Snowcat Snowcat is offline
New Member
 
Join Date: Jan 2006
Posts: 1
Snowcat is on a distinguished road

Finding mode in array of numbers


Hello,

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:

#include <iostream>
using namespace std;

void userArray();
void showArray(float [], int);
float findMode(float [], int);

int main()
{

	// Function for array of integers
	float *values;
	int size;

	cout << "Enter size of array: ";
	cin >> size;
	cout << endl;

	// dynamically generated
	values = new float[size];

	cout << "Enter " << size << " elements, one after the other." << endl;

	for (int count = 0; count < size; count++)
	{
		cout << "Element " << (count + 1) << ": ";
		cin >> values[count];
	}


	// Mode, show
	cout << endl;

	if (findMode(values, size) == -1)
	{
		cout << "No mode." << endl;
	}

	else
	{
		cout << findMode(values, size) << endl;
	}

	return 0;
}

float findMode(float *array, int elems)
{
	   int lastnum = array[0];
	   int curlen = 1;
	   int mode = array[0];
	   int modelen = 1;
	   for(int i = 1; i < elems; i++)
	   {
		   if (array[i] == lastnum)
			   curlen++;
		   else
		   {
			   if (curlen > modelen)
			   {
				   modelen = curlen;
				   mode = lastnum;
			   }
			   lastnum = array[i];
			   curlen = 1;
		   }
	   }

	return ;
}

  #2  
Old 16-Jan-2006, 20:51
Paramesh's Avatar
Paramesh Paramesh is offline
Regular Member
 
Join Date: Sep 2005
Location: The Milky Way
Posts: 927
Paramesh is a jewel in the roughParamesh is a jewel in the roughParamesh is a jewel in the rough

Re: Finding mode in array of numbers


Hi 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  
Old 17-Jan-2006, 20:53
ubergeek ubergeek is offline
Regular Member
 
Join Date: Jan 2005
Posts: 775
ubergeek is a jewel in the roughubergeek is a jewel in the roughubergeek is a jewel in the rough

Re: Finding mode in array of numbers


the mode is the number which occurs most frequently in the set. your first problem is in
CPP / C++ / C Code:
	if (findMode(values, size) == -1)
	{
		cout << "No mode." << endl;
	}

	else
	{
		cout << findMode(values, size) << endl;
	}
that is a logical error; you are calling findMode() twice. this wastes time. instead, call it once before the loop and save its return value. then test that explicitly.

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:
delete[] values;
when you're done with them (that is, just before the return 0).

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  
Old 18-Jan-2006, 07:57
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,693
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Finding mode in array of numbers


Quote:
Originally Posted by Snowcat
Hello,

I can't seem to figure out how to get the mode from an array.

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 GIDBlogFlickr uploads of IA pictures by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 01:08.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.