Help Finding the mode, calling fuction

This is a discussion on Help Finding the mode, calling fuction within the C++ Programming forums, part of the General Programming Boards category; Hi, i have this much done and i believe i have the right function down to find the mean of ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    16

    Help Finding the mode, calling fuction

    Hi, i have this much done and i believe i have the right function down to find the mean of my randomly sorted array. However, i am having a problem figureing out where to call the MODE fuction and how to call can. Can you help? Thanks

    and i realize i have a lot of other formating errors and such but i just want to make sure it works first. thanks

    Code:
    #include <cstdlib>
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    void bubbleSort();
    void selectionSort();
    void insertionSort();
    
    int main(int argc, char *argv[])
    
    {
        int sort;
        system("color e0");
        do
        {
            cout << "Select a sort to run: \n";
            cout << "\t1: Bubble sort\n";
            cout << "\t2: Selection sort\n";
            cout << "\t3: Insertion sort\n";
            cout << "\t0: Exit\n";
            cin >> sort;
            system("cls");
            
            if (sort==1)          bubbleSort();
            else if (sort==2)     selectionSort();
            else if (sort==3)     insertionSort();
            
        } while (sort != 0);
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    
    void bubbleSort()
    
    {   srand ((unsigned) time(NULL));
        int size;
        int main();
        cout << "Array Size: "<<endl;
        cin >> size;
        system("cls");
        cout << "Array Size: " << size <<endl<<endl;
        int tosort[size];
        for (int i=0;i<size;i++)
            {
            int x = (rand() %100);
            tosort [i] = x;
            cout << x << " " ;
            }
            cout<<endl;
            
        int tmp;
        
        for (int y=0; y < size; y++)
        {
            for (int x=0; x < size; x++)
            {
                if(tosort[x] > tosort[x+1])
                {
                             tmp = tosort[x];
                           tosort[x] = tosort[x+1];
                            tosort[x+1] = tmp;
                }
                cout << tosort[x] << " ";
            }
         
            cout << endl;
        }
        
    double mean = 0, median = 0, mode= 0,;
    float dev=0;
    double sum, sum2;
    for (int meancount=0; meancount<size; meancount++)
    {
        sum=sum + tosort[meancount];
        sum2=sum2 + tosort[meancount]*tosort[meancount];
    }
    double tot;
    int med=size/2;
    mean = sum/size;
    dev = (sum2 - size*mean*mean);
    tot= sqrt( dev /size);
    cout <<endl<<endl<< "Mean: " << mean <<endl; 
    cout <<"Range: " << abs( tosort[0] - tosort[size-1]) <<endl;
    cout <<"Sum: " << sum << endl;
    
    if (size % 2 ==0)
    {
             double ave=0;
             ave= tosort[med-1] + tosort[med];
             double final=0;
             final = ave/2;
             cout << "Median: " << final << endl;
             }
             else
             { cout << "Median: " << tosort[ med ] << endl; }
    
    
    cout << "Standard Deviation: " <<tot << endl;
    
    system("pause");
    system("cls");
    main();
    }
    void selectionSort() 
    
    {
        srand ((unsigned) time(NULL));
        int size;
        cout << "Array Size: ";
        cin >> size;
        cout << endl;
        int tosort[size];
        for (int i=0;i<size;i++)
            {
            int x = (rand() %100);
            tosort [i] = x;
            cout << x << " " ;
            }
            cout<<endl;
    
    	int minimum, tmp;
    	
    
    	for (int r = 0; r < size; r++)
    	{
    		minimum = r;
    
    		for (int q = r; q < size; q++)
    		{
    			if (tosort[q] < tosort[minimum])
    			{
    				minimum = q;
    			}
    		}
    
    		tmp = tosort[minimum];
            tosort[minimum] = tosort[r];
            tosort[r] = tmp;
    
    		for (int x=0; x<size; x++)
    			cout << tosort[x] << " ";
    		
    		cout << endl;
    	}
    double mean = 0, median = 0, mode= 0;
    float dev=0;
    double sum, sum2;
    for (int meancount=0; meancount<size; meancount++)
    {
       sum=sum + tosort[meancount];
        sum2=sum2 + tosort[meancount]*tosort[meancount];
    }
    double tot;
    int med=size/2;
    mean = sum/size;
    dev = (sum2 - size*mean*mean);
    tot= sqrt( dev /size);
    
    cout <<endl <<endl<< "Mean:" << mean<<endl; 
    cout << "Range: " << abs( tosort[0] - tosort[size-1])<<endl;
    cout <<"Sum: " << sum << endl;
    if (size % 2 ==0)
    {
             double ave=0;
             ave= tosort[med-1] + tosort[med];
             double final=0;
             final = ave/2;
             cout << "Median: " << final << endl;
             }
             else
             { cout << "Median: " << tosort[ med ] << endl; }
             
             cout << "Standard Deviation: " <<tot << endl;
    system("pause");
    system("cls");
    }
    
    void insertionSort()
    
    {
    	int tmp, a;
    	srand ((unsigned) time(NULL));
        int size;
        cout << "Array Size: "<<endl;
        cin >> size;
        cout << endl;
        int tosort[size];
        for (int i=0;i<size;i++)
            {
            int x = (rand() %100);
            tosort [i] = x;
            cout << x << " "  ;
          
            }
    	  cout << endl;
    
    	for (int t=1; t<size; t++)
    	{
    		tmp = tosort[t];
    
    		for (a = t-1; (a >= 0 && tmp < tosort[a]); a--)
    		{
    			tosort[a+1] = tosort[a];
    		}
    		tosort[a+1] = tmp;
    
    
    		for (int x=0; x<size; x++)
    			cout << tosort[x] << " ";
    		
    		cout << endl;
    	}
    double mean = 0, median = 0, mode= 0;
    float dev=0;
    double sum, sum2;
    for (int meancount=0; meancount<size; meancount++)
    {
         sum=sum + tosort[meancount];
        sum2=sum2 + tosort[meancount]*tosort[meancount];
    }
    double tot;
    int med=size/2;
    mean = sum/size;
    dev = (sum2 - size*mean*mean);
    tot= sqrt( dev /size);
    cout <<endl<<endl<< "Mean: " << mean<<endl; 
    cout << "Range: " << abs(tosort[0] - tosort[size-1])<<endl;
    cout <<"Sum: " << sum << endl;
    
    if (size % 2 ==0)
    {
             double ave=0;
             ave= tosort[med-1] + tosort[med];
             double final=0;
             final = ave/2;
             cout << "Median: " << final << endl;
             }
             else
             { cout << "Median: " << tosort[ med ] << endl; }
             cout << "Standard Deviation: " <<tot << endl;
             
    system("pause");
    system("cls");
    }
    
    
    void mode (int tosort[], int arraysize)
    {
    int mode[101]={};
    int x=0;
    int y=0;
    int max=0;
    
    for (x=0; x<101; x++)
    {
    mode[x]=0;
    }
    
    for (x=0; x<101; x++)
    {
    for(y=0; y<arraysize; y++)
    {
    if (tosort[y]==x)
    {
    mode[x]=mode[x]+1;
    }
    }
    }
    
    for (x=0; x<101; x++)
    {
    if (max<mode[x])
    {
    max=mode[x];
    }
    }
    
    for (x=0; x<101; x++)
    {
    if (max==mode[x])
    {
    cout << "Mode: " << x;
    }
    }
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    and i realize i have a lot of other formating errors and such but i just want to make sure it works first
    Those formatting errors make your code virtually unreadable. How can we help with code that is painful to read? If you spend an extra 30 seconds formatting your code before posting it here, more people would be inclined to help you.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    16
    is there anything that you can press that will auto-format it for you like in true basic or is it a thing you have to do yourself? thanks

  4. #4
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    Double posting.

    Sorting Array Help

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    When you paste code from your IDE to here, the formatting will stay preserved. If your code looks the same in your IDE as it does posted above, then that is your first problem. You should keep your code formatted as you write it. It will make it much easier for you to spot bugs that are due to mismatched braces, missing parenthesis, bad scoping, etc.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    6,194
    Basically you've thrown some meaningless code into each of the three xxxSort() functions related to computing the median. You need to remove that, and (I guess) place some logic in main() to compute the median and print it (or whatever).

    Your bubbleSort() function is also calling main() which is not allowed in C++.

    Without knowing what you're trying to achieve (looking at random code which doesn't do what you want, does not explain much) it's hard to be more specifc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Finding mode!
    By afiq8289 in forum C Programming
    Replies: 1
    Last Post: 11-27-2008, 09:51 AM
  2. Finding the Mode
    By Lucid15 in forum C Programming
    Replies: 34
    Last Post: 10-07-2008, 06:04 PM
  3. Finding mode in array of numbers
    By Snowcat in forum C++ Programming
    Replies: 1
    Last Post: 01-16-2006, 08:58 PM
  4. finding mode of an array
    By n00by in forum C Programming
    Replies: 1
    Last Post: 05-18-2004, 07:40 PM
  5. Finding the mode of an array
    By Shnakepup in forum C++ Programming
    Replies: 16
    Last Post: 05-16-2003, 10:01 AM

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