Calculating the Mode

This is a discussion on Calculating the Mode within the C Programming forums, part of the General Programming Boards category; Hi im trying to calculate the mode of an array, this is what i have so far, at this point ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    12

    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;
    }

  2. #2
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    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.

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, 03:06 AM
  2. Replies: 1
    Last Post: 04-24-2010, 01:35 PM
  3. Replies: 4
    Last Post: 09-16-2006, 07:11 PM
  4. calculating the mode
    By bigggame in forum C Programming
    Replies: 10
    Last Post: 06-13-2006, 03:04 AM
  5. Calculating mode
    By Lord CyKill in forum C Programming
    Replies: 1
    Last Post: 09-25-2003, 02:41 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21