Thread: using pointers insde functions

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    84

    using pointers insde functions

    in the code below i am trying to make a function that will sort an array into numerical order but the syntax i am using below is
    wrong. Can anybody tell me what I am doing wrong here .
    Thanks!

    Code:
    #include<iostream>
    using namespace std;
    
    void SortArray(int* array);
    
    int main(void)
    {
    	int ages[2]={6,5};
    	cout << ages[0] << " " << ages[1] << endl;
    
    	SortArray(ages);
    
    	cout << ages[0] << " " << ages[1] << endl;
    
    
    	return(0);
    }
    
    void SortArray(int* array)
    {
    	int temp=0;
    	int* pnum1=array[0];
    	int* pnum2=array[1];
    
    	if(*pnum1>*pnum2)
    	{
    		temp=*pnum1;
    		*pnum1=*pnum2;
    		*pnum2=temp;
    }

  2. #2
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    I assume the missing } is a typo

    Try this:
    Code:
    void SortArray(int* array)
    {
    	int temp=0;
    	int* pnum1=&array[0];
    	int* pnum2=&array[1];
    
    	if(*pnum1>*pnum2)
    	{
    		temp=*pnum1;
    		*pnum1=*pnum2;
    		*pnum2=temp;
    	}
    }

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Forget people . This dozy sod(thats me) forgot to use the address of operator!

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Endo you just beat me . Congrats on your 100 posts

  5. #5
    Seeking motivation... endo's Avatar
    Join Date
    May 2002
    Posts
    537
    Woohoo, I didn't even notice.

    Y'know what you are doing is the beginnings of a bubblesort, that would be much better to use if you're able.

  6. #6
    Registered User
    Join Date
    Jun 2002
    Posts
    267
    hey, writing a bubble sort method is a great way to familiarize yourself with arrays, and is a good "gateway algorithm" into the more advanced types of sort

  7. #7
    Registered User
    Join Date
    Aug 2001
    Posts
    84
    Hey Endo youve grown from a boy to a man now

    Bubble sort ? Is that when you have an array of numbers and you loop while sorting until you get the array in numerical order? If not please give a breif description.

  8. #8
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    Code:
    void BubbleSort(int * List, int ListLength)
    {
        int SwapNumber, NumberCounter, PassCounter = 0;
        bool SwapMade;
        
        do
        {
            SwapMade = false;
            PassCounter++;
            
            for (NumberCounter = 0; NumberCounter < (ListLength - PassCounter); NumberCounter++)
            {
                if (List[NumberCounter] > List[NumberCounter + 1])
                {
                    SwapNumber = List[NumberCounter];
                    List[NumberCounter] = List[NumberCounter + 1];
                    List[NumberCounter + 1] = SwapNumber;
                    SwapMade = true;
                }
            }
        } while (SwapMade);
    }
    Bubble sort moves through the list comparing number [n] to number [n+1]. If [n] is greater than [n+1], the two numbers are swapped. The result is that following each pass through the list, the largest number will 'rise' to the 'top' of the list (Cheesy analogy: like a bubble will rise to the top in a glass of water). Above, the bubble sort moves through the List comparing List[NumberCounter] to List[NumberCounter + 1] and swapping as required. When a swap occurs, the boolean SwapMade is set to true. When no swaps are required (SwapMade == false), the list is sorted and the function exits.

    If you're going to write your own sort, you should try a selection sort (just as easy, twice as fast) or, even better, use qsort.

    fletch
    "Logic is the art of going wrong with confidence."
    Morris Kline

  9. #9
    Registered User fletch's Avatar
    Join Date
    Jul 2002
    Posts
    176
    "Logic is the art of going wrong with confidence."
    Morris Kline

  10. #10
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Going back years in my memory:
    Code:
    int i, ii;
    int iArray[5] = { 2, 4, 5, 1, 3};
    
    for ( i = 0; i < 5; i++)
    {
      for (ii = i + 1; ii < 5; ii ++)
      {
         if ( iArray[i] < iArray[ii])
         {
            int swap = iArray[i];
            iArray[i] = iArray[ii];
            iArray[ii] = swap;
          }
        }
    }
    Untested, but if memory serves than it is correct.

    However if you are using C++ than do something like this:
    Code:
    #include <algorithm>
    
    vector<int> iVec;
    
    int x;
    while (cin, x)
        iVec.push_back(x);
    
    sort ( iVec.begin(), iVec.end() );
    Something to this effect works well in C++, however MSVC++ 6 does not support the C++ STL and therefore you can't use this on that particular compiler.
    Last edited by Troll_King; 07-23-2002 at 09:21 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP WITH FUNCTIONS and POINTERS!!!
    By cjohnson412 in forum C++ Programming
    Replies: 4
    Last Post: 08-11-2008, 10:48 PM
  2. Passing Structure Pointers to Functions
    By samus250 in forum C Programming
    Replies: 15
    Last Post: 03-20-2008, 03:13 PM
  3. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  4. pointers, functions, parameters
    By sballew in forum C Programming
    Replies: 3
    Last Post: 11-11-2001, 10:33 PM