Thread: Sorting function not working

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    22
    Yup, I just tried moving the show array function to see if it just was not displaying and it was just that, it is working just was not being displayed, thanks. I just need to add pointers to it now, but when I do that all hell breaks lose and I get this error (error C2664: 'showArray' : cannot convert parameter 1 from 'int *' to 'int *[]')...example...

    Code:
    /***This program dynamically allocates an array large enough to
    hold a user-defined number of test scores***/
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    //Function prototypes
    int getnumtestscores();
    void gettestscores(int[], int);
    void showArray(int *[], int);
    void sort(int [], int);
    double average(int[], int);
    
    
    int main()
    {
    	int numts = getnumtestscores();
    
    	int *testscores = new int[numts];		//To dynamically allocate an array
    
    	gettestscores(testscores, numts);
    
    	cout << "The testscores are: \n"; showArray (testscores, numts);
    
    	cout << "The testscores sorted are: \n"; sort (testscores, numts);
    
    	showArray (testscores, numts);
    
        cout << "The average of all " << numts << " test scores is: " 
             << fixed << showpoint << setprecision(2)
             << average(testscores, numts);
    	cout << endl;
    
        // Free dynamically allocated array in memory
        delete [] testscores;
        // Make scores point to null
        testscores = 0;
    
    	return 0;
    }
    
    int getnumtestscores()
    {
        int num;
    
        cout << "\n How many test scores do you want to enter : ";
        for(;;) // loop forever  ... until return
        { 
            cin >> num;
         
            return num;
        }  
    }
    
    void gettestscores(int testscores[], int numts)
    {
    	cout << "What are the testscores for the students?\n";
    
    	for (int count = 0; count < numts; count++)
    	{
    		cout << "Testscore " << setw(2) << (count+1) << ":";
    
    		if (testscores < 0)		//Input validation
    		{
    			cout << "An invalid score was entered, please enter a score more than 0\n";
    		}
    
    		cin >> testscores[count];
    	}
    }
    
    void sort(int *array[], int numts)
    {
    	int lowIndex, *lowest;
    
    	for (int count = 0; count < (numts-1); count++)
    	{
    		lowIndex = count;
    		lowest = array[count];
    		
    		for (int index = count + 1; index < numts; index++)
    		{
    			if (*(array[index]) < *lowest)
    			{
    				lowest = array[index];
    				lowIndex = index;
    			}
    		}
    		array[lowIndex] = array[count];
    		array[count] = lowest;
    
    
    	}
    
    	return;
    }
    
    void showArray(int array[], int numts)
    {
    	for (int count = 0; count < numts; count++)
    		cout << array[count] <<" \n";
    	cout << endl;
    }
    
    double average(int array[], int numts)
    {
    	int total = 0;
    
    	for (int count = 0; count < numts; count++)
    	
    		total +=array[count];
    
    	  return total/numts;
    }

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Are you wanting to sort an array of pointers to things, or are you wanting to use a pointer to an array? (The later being totally unnecessary since the array is a pointer when passed to sort).
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM