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

Pages: 1-

actionscript algorithm

Name: Anonymous 2012-01-25 15:41

im trying to find the most occurring element in an array, but im trying to be clever about it instead of doing the long as sub-array method my class is doing currently

i cant tell why this isnt working

http://pastebin.com/u9Vad3Jf

any help would be appreciated

Name: Anonymous 2012-01-25 15:56

OMG THIS IS SO UGLY o_o

Name: Anonymous 2012-01-25 16:10

In APL:
(↑⍒+/A∘.=A)⌷A←1 2 2 3 3 4 4 4

Name: Anonymous 2012-01-25 16:22

damn AS programmers are stupid. probably because they are descendent from java programmers, only much younger. here, convert it to your stupid language.

#include<stdio.h>

int cmp(const void * a, const void * b){
  return *(int*)a - *(int*)b;
}

int main(){
  int i, current, max, maxi;
  int arr[] = { 2, 3, 1, 3, 2, 1, 3};
  qsort (arr, 7, sizeof(int), cmp);
  current=max=maxi=0;
  for(i=1; i<7; i++){
    if(arr[i]==arr[i-1]) current++;
    else current=0;
    if(current>max){
      max=current;
      maxi=i;
    }
  }
  printf("%d", arr[maxi]);
  return 0;
}


something like a bucket sort would be a lot better if values are small enough

Name: Anonymous 2012-01-25 17:18

>>4

int cmp(const void * a, const void * b){
  return *(int*)a - *(int*)b;
}


why?

Name: Anonymous 2012-01-25 17:28

>>4
i will try and decipher this, thank you

Name: Anonymous 2012-01-25 17:36

heres the actionscript equivalent to >>4

i think it looks much nicer

public fuinction countTheNumbers(nums:Array)
{
    var i:int;
    var current:int=0;
    var max:int=0;
    var maxim:int=0;

    nums.sort();


    for(var i:int=1; i<nums.length; i++)
    {
        if(arr[i]==arr[i-1])
            current++;
        else
            current=0;

        if(current>max)
        {
            max=current;
            maxim=i;
        }
    }

    trace(nums[maxim]);
}

Name: Anonymous 2012-01-25 18:12

>>5

void qsort ( void * base,
             size_t num,
             size_t size,
            int ( * comparator ) ( const void *, const void * ));

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