Sorting Array Help

This is a discussion on Sorting Array Help within the C++ Programming forums, part of the General Programming Boards category; Hi, i have this much of my program done and it works and everything but i still missing a few ...

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

    Sorting Array Help

    Hi, i have this much of my program done and it works and everything but i still missing a few things. I was wondering how do i get the mode of my array? And i need to write this program using a different kind or sorting technique different from the three i have used already. Can you help me please? 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;
        
        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");
    }

  2. #2
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,167
    First step is to fix your indentation.

    And you should NEVER call main() yourself.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    Here begins the list of suggestions for that code, in the order they appear in your program:
    • <math.h> is deprecated, use <cmath> instead
    • The "if (sort==1) ... else if (sort==2) .. else if (sort==3)" code should be replaced with a switch statement
    • Try and use a consistent brace-placement style
    • Don't duplicate large amounts of code. Put common code in one function.
    • Do not call srand more than once in your program, unless you specifically want to repeat a previous sequence of random numbers
    • Don't prototype main, there is NEVER a need for that
    • You cannot declare an array using a variable as the size in C++. Either use a vector, or use new[], or use a fixed-size array
    • Your BubbleSort is broken - it has a buffer overrun
    • Fix your indentation
    • Try and be consistent with your use of whitespace
    • mode= 0,; why the extra comma?
    • You don't need to mix doubles and floats, just pick one and stick with it
    • [med-1] produces a buffer underrun when the size entered is zero, as does [size-1]
    • NEVER call main!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-31-2009, 12:34 PM
  2. two dimensional array sorting in C/C++
    By George2 in forum C Programming
    Replies: 16
    Last Post: 11-19-2006, 02:17 AM
  3. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 12:01 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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