Tuesday, July 14, 2009

C++, finding mode. It works - but is there an easier way?

void ord(double [], int);


int t, c = 0, count[50], p; double arr[50];


void main(){


cout %26lt;%26lt; "Postitive, 50 max, -1 to find mode)\n";


for (;;){


cout %26lt;%26lt; c + 1 %26lt;%26lt; ":%26gt;%26gt;";


cin %26gt;%26gt; t;


if (t == -1) break;


arr[c += 1] = t;}


ord(arr, c);


cout %26lt;%26lt; endl %26lt;%26lt; "Mode is: ";


int temp = count[1];


p = 1;


for (int i = 2; i %26lt;= c; i++){


if (count[i] %26gt; temp){


temp = count[i];


p = i;}}


cout %26lt;%26lt; arr[p] %26lt;%26lt; endl %26lt;%26lt; "Seen: " %26lt;%26lt; count[p] %26lt;%26lt; " times.\n";


system ("pause"); }





int f;


void ord(double arr[], int size){





for (int i = 1; i %26lt; size; i++){





for (int j = 1; j %26lt;= size; j++){


if ( arr[i] == arr[j] )


f++;}


count[i] = f;


f = 0;}}

C++, finding mode. It works - but is there an easier way?
Your code is super sloppy.





Get the array populated then do this:





int size;


int array[50];





// assume size is the array length and array[size-1] is the last element.








for (int i = 0; i %26lt; size; i++)


{





int count = 0;





for (int j = 0; j %26lt; size; j++)


{


count++;


}





cout %26lt;%26lt; "Element: " %26lt;%26lt; array[i] %26lt;%26lt; " count: "%26lt;%26lt; count %26lt;%26lt; endl;


}
Reply:????????????????????????????? dorks Report It



No comments:

Post a Comment