swap function

This is a discussion on swap function within the C Programming forums, part of the General Programming Boards category; I'm having trouble with my swap. I'm just trying to compare strings and swap them. Code: for(int n=0; n<= 13; ...

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    49

    swap function

    I'm having trouble with my swap. I'm just trying to compare strings and swap them.


    Code:
        for(int n=0; n<= 13; n++)
        for (int i=0; i<= 13; i++ )
            {
                int j=i+1;
                int result = strcmp( records[i].name,  records[j].name);
                if ( result > 0 )
                    swap(i,j);
    my problem is that no swap is taking place.

    Thanks
    Last edited by SpEkTrE; 02-18-2005 at 09:11 AM.
    Code this

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Well that depends on how you defined swap (which you didn't show us)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    Code:
    . o O ( void swap(int i, int j) { return; } )
    If you understand what you're doing, you're not learning anything.

  4. #4
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    the values are being swapped, however you are not seeing this beacuse the swap function is being sent copies of the variables.... not the actual variables.

    You need to use pointers in order to do this

  5. #5
    Registered User
    Join Date
    Sep 2003
    Posts
    49
    Code:
    void alpha(struct employee *records);
    ...
    main()....
    ....
    
    
    
    void alpha(struct employee *records)
    {
    	for(int n=1; n<= 13; n++)
    		for (int j=1; j<= 13; j++ ) 
    		{
    			int result = strcmp ( records[i].name,  records[i+1].name);
    			if ( result > 0 )
    				swap (i,j );
    			i++;
    		}
    }

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,417
    Yes, very good - now paste your swap function (not asking again)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User computerfreaks's Avatar
    Join Date
    Jan 2005
    Posts
    30
    Code:
    void swap(int *m, int *n);
    
    int main()
    {
        int x = 3;
        int y = 5;
    
        int *ptx;
        int *pty;
    
        // Assign addresses of x and y to pointers
        ptx = &x;
        pty = &y;
    
    
    
        swap(ptx,pty);
        // could also be called using swap(&x, &y);
    
    
        return 0;
    }
    
    void swap(int *x, int *y)
    {
        int temp;
    
        temp = *x;
        *x = *y;
        *y = temp;
    }

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Code:
    void swap(int &x, int &y)
    {
        int temp;
    
        temp = x;
        x = y;
        y = temp;
    }
    edit: You can ignore my reply, what i posted is a C++ way. Im new too and ive been reading a C++ and C book so i got confused.
    Last edited by InvariantLoop; 02-18-2005 at 12:21 PM.
    When no one helps you out. Call google();

  9. #9
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934

    Lightbulb

    >what i posted is a C++ way
    Don't worry, I did the same thing in the distant past. But Quzah set me straight real quick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 06:05 PM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 10:00 AM

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