Thread: I'd like some help with making a swap function.

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    I'd like some help with making a swap function.

    Hello,

    I am a student learning to program in C and currently learning about pointers.

    I am having trouble completing a program that swaps and prints two arrays.

    Basically the output should read:

    10, 9, 8, 7, 6, 5, 4, 3, 2, 1
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10


    I have setup a function called 'swap' where the swapping of the data gets done, and printing of the swap function should be in the main function.

    Please find the additional details of the restriction in the code below.

    Code:
    // Cannot use [] in the swap function.
    // need to reverse the output of the ar[10] and ar2[10]
    
    #include <stdio.h>
    
    void swap(int *ptr, int *ptr2);
    
    int main(void)
    {
    	int ar[10]={1, 2, 3, 4, 5, 6, 7, 8, 9, 10};		// Declaration of the arrays.
    	int ar2[10]={10, 9, 8, 7, 6, 5, 4, 3, 2, 1};	
    	int i;
    	
    	swap(ar, ar2);									// Call on the swap function.
    	
    	/*
    	printf("Original ar[10]: ");
    	for(i=0; i<10; i++)								// Printing of the original statement.
    	{
    		printf("%d", ar[i]);
    	}
    		printf("\n\n");
    	
    	printf("Original ar2[10]: ");
    	for(i=0; i<10; i++)								// Printing of the original statement.
    	{
    		printf("%d", ar2[i]);
    	}
    		printf("\n\n");
    	*/
    	
    
    	
    	return 0;
    }
    
    void swap(int *ptr, int *ptr2)
    {
    	int temp, i;
    	
    	temp=*ptr;
    	*ptr=*ptr2;
    	*ptr2=temp;
    	
    	
    	return 0;
    }
    Thanks for all the help in advance.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Your swap function swaps a single pair of numbers. You could perhaps use a loop to run each index 0 through 9 into your swap function.

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Temp in your swap function should be a pointer to an int... int *temp ... not an int.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by CommonTater View Post
    Temp in your swap function should be a pointer to an int... int *temp ... not an int.
    Why would you want it to be a pointer? You are swapping ints.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    You will also need to put your swap function into your loops... so it runs across the entire array not just the first item.

  6. #6
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by CommonTater View Post
    Temp in your swap function should be a pointer to an int... int *temp ... not an int.
    No, it should be an int. If you store the address of the variable you're swapping, then the original value is lost before you can retrieve it to complete the swap.
    If you understand what you're doing, you're not learning anything.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by itsme86 View Post
    No, it should be an int. If you store the address of the variable you're swapping, then the original value is lost before you can retrieve it to complete the swap.
    True... I thought from a quick glance he was swapping pointers, not values... my mistake.

  8. #8
    Registered User
    Join Date
    Feb 2011
    Posts
    2
    Thanks guys for helping me out here.

    When I posted the question, I should have probably said that I was aware of putting the loop somewhere in the swap function.

    I have tried to re-build the code once more but weirdly I am still very much confused about this problem.

    I think I need to go back and study arrays and look at pointers once more.

    In the meantime, I would love to get my head around the problem above, can someone help me out to finish the code? (maybe less vague details on completing the code?)

    Thanks in advance and I appreciate all the help.

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by sw881118 View Post
    Thanks guys for helping me out here.

    When I posted the question, I should have probably said that I was aware of putting the loop somewhere in the swap function.

    I have tried to re-build the code once more but weirdly I am still very much confused about this problem.

    I think I need to go back and study arrays and look at pointers once more.

    In the meantime, I would love to get my head around the problem above, can someone help me out to finish the code? (maybe less vague details on completing the code?)

    Thanks in advance and I appreciate all the help.
    I don't really know how much more less vague we can possibly be other than "put your swap statement in a loop". The only other issue here is the fact that you have to pass pointers/addresses to your swap function; so when you want to swap ar[5] with ar2[5], you need to pass ar+5 and ar2+5 to your function (because those are the addresses of ar[5] and ar2[5] respectively).

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by sw881118 View Post
    Thanks guys for helping me out here.

    When I posted the question, I should have probably said that I was aware of putting the loop somewhere in the swap function.

    I have tried to re-build the code once more but weirdly I am still very much confused about this problem.

    I think I need to go back and study arrays and look at pointers once more.

    In the meantime, I would love to get my head around the problem above, can someone help me out to finish the code? (maybe less vague details on completing the code?)

    Thanks in advance and I appreciate all the help.
    Well, if the goal is to swap the contents of array #1 with those of array #2, it's really a pretty simple loop... use your current swap function, pass in pointers to the array elements swap(&array1[i], &array2[i]) and let it work it's magic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sudoku Brute Force Algorithm Help (recursion?)
    By ridilla in forum C Programming
    Replies: 22
    Last Post: 05-07-2010, 03:31 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. help on making int to string from function
    By peter_hii in forum C++ Programming
    Replies: 5
    Last Post: 04-12-2006, 02:58 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM