Need help on flip array algorithm

This is a discussion on Need help on flip array algorithm within the C Programming forums, part of the General Programming Boards category; Dear Sir or Madam: I have been struggling in finding a way to flip an array. I want to flip ...

  1. #1
    DiepVien_007
    Guest

    Need help on flip array algorithm

    Dear Sir or Madam:
    I have been struggling in finding a way to flip an array.
    I want to flip array from begin index to end index.
    Code:
      float  Array[5] = {3.0,  5.7,  6.0,  8.9,  9.4};
    
      this means:  Array[0] = 3.0
                          Array[1]  = 5.7
                          Array[2]  = 6.0
                          Array[3]  = 8.9
                          Array[4]  = 9.4
    
    How can I flip this array so that it become:
    
                         Array[4] = 3.0
                         Array[3] = 5.7
                         Array[2] = 6.0
                         Array[1] = 8.9
                         Array[0] = 9.4
    If an array is larger such as 1000 elements , how can i flip it by a
    special algorithm?
    Your help would deeply appreciated.

    DiepVien_007

  2. #2
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    You could do swapping by introducting a temporary variable.

    temp = array [i];
    array [i] = array [j];
    array [j] = temp;

    To swap the complete array you would need to let i run from 0 until half of the array. The variable j runs from the last element index until half of the array. Or, you could calculate j by realising that the j should decrease at same speed as i increases.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Hello, well, I don't know if this is a good way to do this, but:
    Code:
    #include <stdio.h>
    
    #define SIZE 5
    
    int main(void) {
    	int i;
    	float arr[SIZE] = {3.0, 5.7, 6.0, 8.9, 9.4};
    	float tmp;
    	
    	for (i=0; i<SIZE; ++i)
    		printf("arr[%d] = %.1f\n",i,arr[i]);
    
    	//rotate the array, loop until the middle (I think)
    	for (i=0; i<(SIZE/2)+1; i++) 
    	{
    		tmp = arr[i];
    		arr[i] = arr[SIZE-i-1];
    		arr[SIZE-i-1] = tmp;
    	}
    
    	printf("\n\n");
    	for (i=0; i<SIZE; ++i)
    		printf("arr[%d] = %.1f\n",i,arr[i]);
    	return 0;
    }

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Why do you need to flip it? Why not just process it backwards, like going from index N-1 to 0, instead of 0 to N-1.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Simply reversing the array?

    Code:
    void Reverse( float array[], int size )
    {
      int i;
      int m = size >> 1;
      int j = size - 1;
      float temp;
    
      for( i = 0; i < m; ++i, --j )
      {
        temp = array[i];
        array[i] = array[j];
        array[j] = temp;
      }
    }
    Last edited by MrWizard; 03-17-2003 at 05:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about multidimensional arrays
    By richdb in forum C Programming
    Replies: 22
    Last Post: 02-26-2006, 09:51 AM
  2. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  3. code doesnt flip array
    By noob2c in forum C Programming
    Replies: 3
    Last Post: 03-29-2003, 09:54 AM
  4. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 09:31 AM

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