C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-12-2006, 12:18 AM   #1
Registered User
 
Join Date: Jun 2006
Posts: 4
calculating the mode

Hi there,

So I have a 10 element array of int's and I need to calculate the mode.

I haven't written any code (for the calculation part) because I have no idea where to start. I was just hoping someone who has done this before or thinks they have a good idea could point me in the right direction, I would appreciate it.

Is there a function to do this for me in math.h?
bigggame is offline   Reply With Quote
Old 06-12-2006, 12:29 AM   #2
Awesomefaceradcore
 
bivhitscar's Avatar
 
Join Date: Apr 2006
Location: Melbourne, Australia
Posts: 186
The mode is just the average, should be pretty easy. I'd suggest trying it out first and then come here with a specific problem if you get stuck.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me...
bivhitscar is offline   Reply With Quote
Old 06-12-2006, 12:36 AM   #3
Registered User
 
Join Date: Jun 2006
Posts: 4
Hey, thanks i appreciate any help i can get..

I'm looking for:

"the value that appears most frequently in the values input by the user."

Is there a sorting function that checks for repeat input?
bigggame is offline   Reply With Quote
Old 06-12-2006, 12:42 AM   #4
Awesomefaceradcore
 
bivhitscar's Avatar
 
Join Date: Apr 2006
Location: Melbourne, Australia
Posts: 186
Oh crap, it is to, sorry about that. Still, you could at least write a loop or something, just to show us that you're trying. We like to help, but not without some effort from you first.
__________________
it's ironic considerate rarity patron of love higher knowledge engulfs me...
bivhitscar is offline   Reply With Quote
Old 06-12-2006, 02:27 AM   #5
Registered User
 
Join Date: Mar 2006
Posts: 726
You could sort the data, then loop through it looking for the longest string of repeat values.
__________________
Code:
#include <stdio.h>

void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
/3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
jafet is offline   Reply With Quote
Old 06-12-2006, 04:58 AM   #6
Super Moderator
 
Bubba's Avatar
 
Join Date: Aug 2001
Posts: 7,819
Code:
for (int i=0;i<10;i++)
{
  //Do some stuff here
}
Bubba is offline   Reply With Quote
Old 06-12-2006, 08:38 AM   #7
Registered User
 
Join Date: Jun 2006
Posts: 4
ok, ok sorry guys don't hate me i was thinking of something like this (keep in mind I already ordered the entries from smallest to largest):

Code:
void compute_mode(char test_scores[],int *n_scores)
{
int i, j,k;
char mode[*n_scores];
int n_modes = 1;

for(i=o;i<*n_scores;i++)
{
  for(j=i+1;j<*n_scores;j++) 
  {
    if( test_scores[i] < test_scores[j] )
      continue;
    else 
      {
       mode[n_modes] = test_scores[i];
       n_modes++;
       }
    }
}

for(k = 0; k <n_modes; k++)
   {
   printf("Mode %s", mode);
   }
}

Last edited by bigggame; 06-13-2006 at 12:28 AM.
bigggame is offline   Reply With Quote
Old 06-12-2006, 04:13 PM   #8
Frequently Quite Prolix
 
dwks's Avatar
 
Join Date: Apr 2005
Location: Canada
Posts: 7,698
Code:
printf("Mode %s", mode[]);
That won't work.

test_score isn't the same variable as test_scores.
__________________
dwk

Seek and ye shall find. quaere et invenies.

"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell


Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net

My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
dwks is offline   Reply With Quote
Old 06-13-2006, 01:18 AM   #9
and the hat of Jobseeking
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,710
> i was thinking of something like this
Close enough - now write something which actually compiles.

> char mode[*n_scores];
C doesn't (yet) support variable length arrays.

> for(i=o;i<*n_scores;i++)
I guess that o was meant to be 0 (zero)
__________________
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.

Salem is offline   Reply With Quote
Old 06-13-2006, 01:44 AM   #10
+++ OK NO CARRIER
 
quzah's Avatar
 
Join Date: Oct 2001
Posts: 10,643
Quote:
Originally Posted by Salem
> char mode[*n_scores];
C doesn't (yet) support variable length arrays.
C99 does. Your compiler on the other hand...


Quzah.
__________________
Hundreds of thousands of dipshits can't be wrong.


Are you up for the suck?
quzah is offline   Reply With Quote
Old 06-13-2006, 03:04 AM   #11
Registered User
 
Join Date: Mar 2006
Posts: 726
You can make it simpler. A whole lot simpler, in fact:

Let's say you want to find the mode of 0,0,1,1,1,2,4,5,5,5,5,6,7,7,8,10. Let's call a row of consecutive numbers (like 3,3,3) a "run". You can proceed thus:

We start with 0, because it is the first element. We find that the run of 0s is of length 2. This is the longest run so far.

We proceed to the next element, 1, which has a run of three elements. This is longer than the zero run, so we store this as the longest run instead. The length of the 0s run is forgotten, as it is not needed.

... ...

In the end, 5 is found to be the mode, as it has a run of 5. The trick is that finding the mode only requires that you keep track of the longest run SO FAR, while ignoring those runs shorter than the longest run found so far. After you've checked all the runs, the longest run so far is guaranteed to be your mode, because you've found the longest run in the whole array.

Tip: you will only need a loop counter, two variables to keep track of the longest run found, and two more to keep track of the current run. You will only need to run through the whole array once.
__________________
Code:
#include <stdio.h>

void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
/3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
jafet is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
console mode and service mode George2 C# Programming 0 06-01-2008 01:42 AM
6 measly errors beene Game Programming 11 11-14-2006 11:06 AM
Calculating mode Lord CyKill C Programming 1 09-25-2003 02:41 AM
Showing the directory sturcture Unregistered C Programming 1 03-26-2002 04:46 PM
Implementing "ls -al" pdstatha Linux Programming 11 03-20-2002 04:39 AM


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


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22