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-Jul-2004, 07:09
ametzger620 ametzger620 is offline
New Member
 
Join Date: Jul 2004
Location: Raleigh, NC
Posts: 1
ametzger620 is on a distinguished road

Mode of an array


Can anyone help me with the C++ code for finding the mathematical mode of an array?
  #2  
Old 16-Jul-2004, 07:22
dsmith's Avatar
dsmith dsmith is offline
Senior Member
 
Join Date: Jan 2004
Location: Utah, USA
Posts: 1,351
dsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of lightdsmith is a glorious beacon of light
Here is a function that I have recently written to calculate the mode. You will have to adapt it to fit your needs. This uses a custom list function that I wrote. You can replace it with an array or a vector or whater you would like.

CPP / C++ / C Code:
void calc_mode(){
	int*	count = 0;
	int		size;
	int		index;
	list*	modes = new list;   //custom list 
	int*	position;
    int		min;
	int		max_count;


		free(count);
		min = find_min();
		size = (find_max() - min)+1;
		count = (int*) malloc(size * sizeof(int) );
		for(index=0;index<size;index++)
			*(count+index) = 0;

		if(data->home()){
			do{
            	*(count + *( (int*) data->get_data() ) - min) += 1;
			}while(data->next());
		}
		delete modes;
		modes = new list;
		max_count = 0;
		for(index=0;index<size;index++){
			if ( *(count+index) > max_count){
				delete modes;
				modes = new list;
				position = (int*) malloc(sizeof(int));
				*position = index+min;
				modes->add_data(position);
				max_count = *(count+index);
			}
			else if ( *(count+index) == max_count){
				position = (int*) malloc(sizeof(int));
				*position = index+min;
				modes->add_data(position);
			}
		}


	printf("The most reocurring numbers are: \n");
	if(modes->home()){
		do{
			printf("\t%d\n",*( (int*) modes->get_data() ) );
		}while(modes->next());
	}
	printf("Each of these numbers occur %d times\n",max_count);

	//BEGIN TEMPORARY OUTPUT TO TEST FUNCTIONALITY
	FILE*	temp;
	temp = fopen("count.txt","w");
	for(index=0;index<size;index++)
		fprintf(temp,"[%d]: %d\n",index+min,*(count+index));
	fclose(temp);
	//END TEMPORARY OUTPUT

	delete modes;
	free(count);
}

It is uncommented and needs some helper data structures, but hopefully, it can be of some help for you to implement a mode routine to your program.
 
 

Recent GIDBlogMeeting the populace 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
Speed up C++ code about 3d array! Truong Son C++ Forum 0 16-Mar-2004 21:52
[Tutorial] Standard I/O aaroncohn C Programming Language 20 27-Feb-2004 21:07
c: array comparison jack C Programming Language 7 26-Jan-2004 11:21
Extra null element in an array samtediou MySQL / PHP Forum 2 11-Dec-2003 11:52

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 16:54.


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