Thread: Switching arrays

  1. #1
    Unregistered
    Guest

    Switching arrays

    I have been trying this for a week and i still dont understand how to do it. I have to reverse the elements of an a array. I turned the problem into the teacher already and she said all i did was print them backwards and not reversed the elements. I know how to exchange values but i just cant figure this problem out.

    this is my array.....can u help me reverse the elements..??

    ary[MAX]={89,92,56,34,28};
    for (i=0;i<MAX;i++)

    thanx.

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Option 1:

    Create a second array of the same size. Then perform the following algorithm:

    array_new [0] = array_old [N]
    array_new [1] = array_old [N-1]
    ...
    array_new [N] = array_old [0]

    Then you could copy array_new to array_old so array_old is reversed. Or you could change pointer of something different.

    Option 2:

    Use a temporary variable to keep old value and use this for switching. Algorithm:

    temp_value = array [0]
    array [0] = array [N]
    array [N] = temp_value

    ...etc. Take care if the numer of elements is odd.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers & arrays and realloc!
    By zesty in forum C Programming
    Replies: 14
    Last Post: 01-19-2008, 04:24 PM
  2. Replies: 16
    Last Post: 01-01-2008, 04:07 PM
  3. problems switching arrays
    By stillwell in forum C++ Programming
    Replies: 3
    Last Post: 02-27-2006, 11:46 AM
  4. Need Help With 3 Parallel Arrays Selction Sort
    By slickwilly440 in forum C++ Programming
    Replies: 4
    Last Post: 11-19-2005, 10:47 PM
  5. Crazy memory problem with arrays
    By fusikon in forum C++ Programming
    Replies: 9
    Last Post: 01-15-2003, 09:24 PM