CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2009
    Posts
    3

    Unhappy What is wrong with my sorting function?

    What is wrong with my sorting function? I am trying to put numbers in ascending numbers.

    Code:
    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;
    }
    I am confused now for I believe I have tried everything.

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What is wrong with my sorting function?

    Looks right to me. What's wrong with it?

  3. #3
    Join Date
    Oct 2008
    Posts
    29

    Re: What is wrong with my sorting function?

    It works fine:
    Code:
    #include <iostream>
    #include <windows.h>
    
    using std::cout;
    
    
    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;
    }
    
    int main()
    {
    	int arr[] = {1, 0, 2, 5, 4, 3, 7, 6, 2};
    
    	sort(arr, 9);
    
    	for(int i=0;i<sizeof(arr)/sizeof(int);i++) cout << arr[i] << "\n";
    
    	while(true) Sleep(1000);
    
    	return 0;
    }

  4. #4
    Join Date
    May 2009
    Posts
    3

    Re: What is wrong with my sorting function?

    Thanks for the responses...it wasn't being displayed, so I copied the showarray function and it showed...I am trying to add pointers to it though but all hell breaks lose then 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;
    }

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: What is wrong with my sorting function?

    Quote Originally Posted by musique View Post
    Thanks for the responses...it wasn't being displayed, so I copied the showarray function and it showed...I am trying to add pointers to it though but all hell breaks lose then and I get this error (error C2664: 'showArray' : cannot convert parameter 1 from 'int *' to 'int *[]')....example...
    It would help if you dropped the "[]" syntax for your array parameters. This should make it much more clear as to what you're passing to these functions. There are still several things inconsistent in your program.

    When you pass an array, you are passing a pointer to the first element in the array. Therefore this definition:
    Code:
    void showArray(int array[], int numts);
    Is no different than this one:
    Code:
    void showArray(int* array, int numts)
    So you are never "passing arrays", you are always passing a pointer to the first element. Using "int *", should have been the argument type to all of your functions when dealing with arrays. For example:
    Code:
    void sort(int *array[], int numts)
    This should be:
    Code:
    void sort(int *array, int numts)
    You also should do more thorough checking in your function prototypes and your function definitions match.
    Code:
    void showArray(int *[], int);
    //...
    void showArray(int [], int)  // ?????
    {
    //...
    }
    Regards,

    Paul McKenzie

  6. #6
    Join Date
    May 2009
    Posts
    3

    Re: What is wrong with my sorting function?

    Thanks, will change it. Pointers are so confusing though...still trying to work on it.

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: What is wrong with my sorting function?

    Pointers are only as confusing as you let them be. In this context, think of a pointer simply as an array of unknown size (that's why you pass the size parameter too).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured