Thread: Calculating the Mode

  1. #1
    Registered User
    Join Date
    Nov 2011

    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

    #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]);
    	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;

  2. #2
    Join Date
    Dec 2007
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Calculating Mode
    By Amberlampz in forum C Programming
    Replies: 12
    Last Post: 08-05-2011, 04:06 AM
  2. Replies: 1
    Last Post: 04-24-2010, 02:35 PM
  3. Replies: 4
    Last Post: 09-16-2006, 08:11 PM
  4. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 04:04 AM
  5. Calculating mode
    By Lord CyKill in forum C Programming
    Replies: 1
    Last Post: 09-25-2003, 03:41 AM
Website Security Test