Thread: Help! With Sorting

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    6

    Unhappy Help! With Sorting

    Ok, So I got this assignment from my professor, and I know the first sections of code are correct, he pretty much gave them to us in class. But in the last section, section 5, I need to sory by decreasing order. He gave us a "hint" and said to sort by the number of astisks that are outputted, he also said to use the two sorting methos in the book selectionSort or Bubble sort. I think selection sort will work, but have no clue how to get it to work.

    Code:
    #include<iostream>
    #include<iomanip>
    using namespace std;
    
    const int size = 11;
    int arx[size];
    
    // if you want void printHist(); // Print histogram function (Sec. 2, 4, 6)
    // void sortHist(); // Sort histogram function (Sec. 5)
    
    void main()
     
    // section 1-3
    {
    	int i, j, temp;
    	srand(9876);
    
    	for (i=0; i<size; i++)
    		arx[i]=0;
    
    	for (i=0; i<2000; i++)
    		arx[rand()%11]++;
    
    	for (i=0; i<size; i++)
    	{
    		cout << arx[i] << ":" ;
    		for (j=0; j<arx[i]; j+=10)
    			cout<< "*" ;
    		cout << endl;
    	}
    
    // Section 4
    	cout<<endl<<endl;
    	for (i=0; i<size; i++)
    		arx[i]=0;
    
    	for (i=0; i<2000; i++)
    	{
    		temp=((rand()%11) + (rand()%11))/2;
    			arx[temp]++;
    	}
    
    	for (i=0; i<size; i++)
    	{
    		cout << arx[i] << ":" ;
    		for (j=0; j<arx[i]; j+=10)
    			cout<< "*" ;
    		cout << endl;
    	}
    		
    //Section 5 Sort from greatest to least number of ****
    cout<<endl<<endl;
    
    	for (i=0; i<size; i++)
    		arx[i]=0;
    
    	for (i=0; i<2000; i++)
    	{
    		temp=((rand()%11) + (rand()%11))/2;
    			arx[temp]++;
    	}
    
    	for (i=0; i<size; i++)
    	{
    		cout << arx[i] << ":" ;
    		for (j=0; j<arx[i]; j+=10)
    			cout<< "*" ;
    		cout << endl;
    	}
    } // main
    Please help!

    Thanks in advance!
    Jessica

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    So once you've picked your sorting technique, implement it. You've got your data in arx[], and apparently you want it from high to low (so be sure as you follow along to switch less-than and greater-than, since most things sort from low to high). Hopefully you can figure out how to swap two numbers.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Bubble Sort for an array is pretty simple. For reference, here's the explanation including Pseudocode implementation code from Wikipedia. And, from a quick google, here's someone else's reasonable C++ implementation of it: simplified bubble sort c++ - C++ (Lines 25 to 43)

    Happy Birthday for last week! I wonder if you like doing the same thing during thunderstorms that I do.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    6
    Thanks!

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Couple nits:

    #1
    Code:
    const int size = 11;
    
    ...
    
    for (i=0; i<2000; i++)
        arx[rand()%11]++;
    
    
    ...
    
    for (i=0; i<2000; i++)
    {
        temp=((rand()%11) + (rand()%11))/2;
        arx[temp]++;
    }
    
    ...
    
    for (i=0; i<2000; i++)
    {
        temp=((rand()%11) + (rand()%11))/2;
        arx[temp]++;
    }
    You should be able to safely replace those "magic" numbers with the variable size. If that variable ever changes then you'd need to find and modify the code in several extra places. If you use the variable size instead and at some point in the future change its value, then there is nothing else that needs to be done with the code other than change size from 11 to something else.


    #2
    Code:
    void main()
    main should return int not void.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting algorithms, worst-case input
    By Leftos in forum C++ Programming
    Replies: 17
    Last Post: 06-15-2009, 01:33 PM
  2. Need help with linked list sorting function
    By Jaggid1x in forum C Programming
    Replies: 6
    Last Post: 06-02-2009, 02:14 AM
  3. Sorting Algorithm Help
    By cjwenigma in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2007, 02:04 PM
  4. sorting structure members using pointers
    By robstr12 in forum C Programming
    Replies: 5
    Last Post: 07-25-2005, 05:50 PM
  5. Still Needing Help : selection sorting
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 10-14-2001, 08:41 PM