Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Finding the mode in an array...

Name: Anonymous 2008-05-18 15:57

This is supposed to find the mode of a partially filled array. it does not work yet is tis code my CS teacher gave me. what the fuck is wrong with it?

int findMode(int array[], int size)
{
int occ = 0, result, index = 0, mode = 0;

for( ; index < size; index += result)
{
result = linSearch(array, size, array[index]);
if(result < occ)
{
mode = array[index];
occ = result;
}
}
return mode;
}
//////////
int linSearch(int array[], int size, int target)
{
int i;
for(i = 0; i < size; i++)
if(array[i] == target)
return i;

return -1;
}

Name: Anonymous 2008-05-18 15:59

It's not indented properly

Name: Anonymous 2008-05-18 16:31

USE CODE TAGS FOR FUCK'S SAKE. USE FUCKING CODE TAGS.

Name: Anonymous 2008-05-18 16:44

lin search needs to return the number of times it found a value, not just whether it found it.  Then you need to do this for every value in the array and save the candidates for mode.
All that looks like O(n^2).  Can get O(n + n log n) by sorting the array then iterating through it once.  If you know the range of possible values, and the max is small enough just use a modified counting sort.

Now go read your SICP.

Name: Anonymous 2008-05-18 18:06

I know how to do it in expert O(log n) time. Study more, suckers.

Name: Anonymous 2008-05-19 11:16

>>3
this?

int example() {
    tab
 space
  double-space
}

Name: Anonymous 2009-03-06 7:16

U must be talking   about ASP classic   ASP classic was   not very well   I had no   test in it   to you Failing   to grant beet.

Name: Anonymous 2010-12-25 1:47

Name: Anonymous 2011-02-03 8:37

Name: Anonymous 2011-02-04 12:16

Name: Anonymous 2011-02-04 15:13

Name: Sgt.KabukimanꚘ켦 2012-05-23 5:55

All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy
 All work and no play makes Jack a dull boy

Don't change these.
Name: Email:
Entire Thread Thread List