-
Calculating the Mode
Hi im trying to calculate the mode of an array, this is what i have so far, at this point i am stuck as to what is wrong, the code prints off random values on the cmd.exe screen, does anybody have any ideas where i have gone wrong? im using visual studio C++ 2010. many thanks
Code:
#include <stdio.h>
#define Size 10
int main()
{
int median[Size]={10,13,16,16,16,18,21,21,24,36};
int i;
int j;
int modecount = 1;
int lastmodecount = 0;
int index = 0;
int stop;
for (i=0; i<Size; i++);{
for (j=i+1; j<Size; j++);{
if (median[i] == median [j]);
modecount++;}
if(modecount > lastmodecount);
{lastmodecount = modecount;
index = i;}
modecount = 0;}
printf("The mode of myArray is %d, and occurs %d times\n",median[index],lastmodecount);
scanf("%d", &stop);
return 0;
}
-
Code:
for (i=0; i<Size; i++);{
for (j=i+1; j<Size; j++);{
if (median[i] == median [j]);
You don't (generally) put semi-colons after *every* statement. Putting them after ifs and for just renders those statements useless in most cases.