![]() |
| | #1 |
| Registered User Join Date: Jun 2006
Posts: 4
| calculating the mode 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 | |
| | #2 |
| Awesomefaceradcore 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 | |
| | #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 | |
| | #4 |
| Awesomefaceradcore 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 | |
| | #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 | |
| | #6 |
| Super Moderator Join Date: Aug 2001
Posts: 7,819
| Code: for (int i=0;i<10;i++)
{
//Do some stuff here
}
|
| Bubba is offline | |
| | #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 | |
| | #8 |
| Frequently Quite Prolix Join Date: Apr 2005 Location: Canada
Posts: 7,698
| Code: printf("Mode %s", mode[]);
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 | |
| | #9 |
| and the hat of Jobseeking 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) |
| Salem is offline | |
| | #10 | |
| +++ OK NO CARRIER Join Date: Oct 2001
Posts: 10,643
| Quote:
Quzah.
__________________ Hundreds of thousands of dipshits can't be wrong. Are you up for the suck? | |
| quzah is offline | |
| | #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 | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |