1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Reverse the content of the array

Discussion in 'C++' started by coderzone, Aug 27, 2006.

  1. coderzone

    coderzone Super Moderator

    I thought of adding this small code snippets to thelibrary which just reverses the content of the array. Not sort in ascending/descending order, but put last array entry to first, etc. EG.. if array consists of {2,3,4,7,12,98},, need to output {98, 12,7,4,3,2}

    #include <iostream.h>
    #define ARR_SIZE 11
    int main()
    	int arr[ARR_SIZE] = {1,2,3,4,5,6,7,8,9,0,11};
    	int i = 0;
    	// Dispay the content of the array initially
    	cout<<"Array content as input"<<endl;
    	// Swap the array elements each from the first and the last.
    	// It handles automatically the odd and the even no of elements
    		int temp = arr[i];
    		arr[i] = arr[ARR_SIZE - i-1];
    		arr[ARR_SIZE - i-1] = temp;
    	// Dispay the content of the array after the swap.
    	cout<<"Array content as output"<<endl;
    	return 0;
  2. asadullah.ansari

    asadullah.ansari TechCake

    Your code is very complex. Just update it. by this code.
    int main()
     int a[10]={1,2,3,4,5,6,7,8,9,10},temp;
     int *ptr1=&a[0];
     int *ptr2=&a[9];
     for(int i=0;i<10;++i)
     return 0;
  3. shabbir

    shabbir Administrator Staff Member

    I see your one more complex but yes more efficient for ADT's as you are not moving the data but just the pointers.
  4. asadullah.ansari

    asadullah.ansari TechCake

    What's the target of program. Just Reverse the content of Array. That code is doing that.
    In my program , a[9] & a[0] , a[8] & a[1], a[7] & a[2] , a[6] & a[3], a[5] & a[4] are swapping by pointer. Not too much efficient but this algorithm can be made generic for string as well as any data type may be user data or may enbuilt.
    Last edited: Jan 25, 2008
  5. debleena_doll2002

    debleena_doll2002 New Member

    Yes!!! You are right.
  6. lead.smart34

    lead.smart34 New Member

    obviously better
  7. crazytolearn57

    crazytolearn57 New Member

  8. aisha.ansari84

    aisha.ansari84 New Member

  9. blessiepasag

    blessiepasag New Member


    Anyone who can help me how to generate SEO properly??
    Also, I need some suggestion for free website hosting..

    Thanks a lot..
  10. blessiepasag

    blessiepasag New Member


    Need some help in working out with this problem using PHP..
    Is there anyone who has code for this one????Urgent haha
    thanks thanks

    Euclid’s Algorithm: Create a program that will implement Euclid’s GCD (Greatest Common Divisor) algorithm:
    Assume you wish to find the GCD of 2047 and 391.

    1. Divide the larger by the smaller and note the remainder: 2047/391 = (391 X 5) + 92

    2. Divide the remainder (92) into the previous divisor (391): 391/92 = (92 X 4) + 23

    3. Repeat steps 1 and 2 until the remainder is 1 or zero.

    3a Divide the remainder (23) into the previous divisor (92): 92/23 = (23 X 4) + 0

    4. When the remainder is zero the last divisor is the GCD! 23 X 89 =2047 and 23 X 17 = 391. Therefore 89/17 = 2047/391

    5. When the remainder is 1 the two numbers have NO common divisor and are relatively prime. Example: Assume you wish to find the GCD of 8191 and 1023.

    8191/1023 = (1023 X 8) + 7

    1023/7 = (7 X 146) + 1

    The remainder is 1 therefore these two numbers have NO common divisor!

Share This Page