Thread: Help swapping 2 arrays

  1. #1
    Registered User
    Join Date
    Feb 2005
    Posts
    4

    Help swapping 2 arrays

    This whole pointer thing is really baffling me =(

    I have to create a function that swaps 2 arrays. I know how to create the function, I know how to swap the arrays inside the function, but I can't figure out how to get the main() to recognize it =(


    Please help, I have to turn this assignment in really soon.

  2. #2
    Registered User
    Join Date
    Mar 2004
    Posts
    494
    Post your code so people can help you out, the members here are programmers not clairvoyants , so by posting your code they will point your to the right direction.
    When no one helps you out. Call google();

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    184
    what the main() has to recoganise. the sentence is incomplete =( ??

    post the you code or tell us whats the actual problem with the code

    s.s.harish

  4. #4
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    This code is really long, so I really can't post all of it

    Code:
    int print_Array(int x[]);
    
    int exchange(int x[], int y[]);
    
    int sort_Array(int x[], int flag);
    
    int counter (int x[], int flag);
    
    int get_menu_choice (void);
    
    
    main()
    {
    	
       int array_A[100], array_B[100];
       
       srand(997);
     
    Large amounts of code here allowing for manipulation of the 2 
    arrays.  Lots of misc stuff like counting the number of even/odd 
    values, sorting the arrays, ect. 
    
       exchange(array_A[] , array_B[])'
    	  
       return (0);   /* indicate program ended successfully */
    }
    
    Lots of functions here that perform the actions mentioned aboe
    
    int exchange(int num1[], int num2[])
    {
     
    int forflag = 0;
    int temp[100];
    
    for( forflag = 0, forflag <100, forflag++)
    {
    temp[forflag] = num1[forflag];
    num1[forflag] = num2[forflag];
    num2[forflag] = temp[forflag];
    }
      
      return(0);
      }

    Ehh, I really hope this is readable. Posting the whole code would have been too much so i cut a lot of it out. Hope i didn't cut too much.

  5. #5
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
    int exchange(int num1[], int num2[])
    {
     
    int forflag = 0;
    int temp[100];
    
    for( forflag = 0, forflag <100, forflag++)
    {
    temp[forflag] = num1[forflag];
    num1[forflag] = num2[forflag];
    num2[forflag] = temp[forflag];
    }
      
      return(0);
      }
    That should work, but you're making temp unncessarily large. You're only ever swapping 1 variable at a time so you only need 1 temp variable, not an array. Something like:
    Code:
    int exchange(int num1[], int num2[])
    {
     
    int forflag = 0;
    int temp;
    
    for( forflag = 0, forflag <100, forflag++)
    {
    temp = num1[forflag];
    num1[forflag] = num2[forflag];
    num2[forflag] = temp;
    }
      
      return(0);
      }
    Also, you only ever return 0 from that function which makes a return value pretty useless. I'd get rid of the return statement and define exchange() with a void return value instead of int.

    As far as making main() recognize the swapped arrays, it looks like it should work to me. What's your code for printing the arrays for verification look like?
    If you understand what you're doing, you're not learning anything.

  6. #6
    Registered User
    Join Date
    Feb 2005
    Posts
    4
    *sigh* I'm a freaken idiot.

    Thank you for all your help so far


    I kept trying to do stuff like array_A[] = num1[] and just messing around with * and & makes.


    So umm, why don't I have to have array_A[] = num1[] inside the function?


    Sorry for such n3wb question, this is our professors first time with an intro course, and he doesn't follow the book at all. Makes things really hard.

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can't copy an array by doing something like array_A[] = num1[] in C. If you want to copy all the contents of an array in one statement you can use the memcpy() function, but I think your current exchange() function is doing a fine job.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Code:
    void SwapArrays(int *Array1, int *Array2,int Size)
    {
      int *temp;
      
      temp=malloc(Size*sizeof(int));
     
      if (temp)
      {
         memcpy((int *)temp,(int *)Array1,size*sizeof(int));
         memcpy((int *)Array1,(int *)Array2,size*sizeof(int));
         memcpy((int *)Array2,(int *)temp,size*sizeof(int));
         free(temp);
      }
    }
    I think that should swap correctly using memcpy. I'm a C++ fanatic so using just pure C was extremely difficult.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    We don't have to typecast malloc in C.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-04-2008, 08:27 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  4. Swapping Pointers & Arrays
    By bartybasher in forum C++ Programming
    Replies: 6
    Last Post: 10-25-2003, 02:17 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM