Reverse the content of the array

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

  1. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    38
    Trophy Points:
    28
    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}

    Code:
    #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;
    	for(i=0;i<ARR_SIZE;i++)
    		cout<<arr[i]<<"\t";
    	cout<<endl;
    
    	// Swap the array elements each from the first and the last.
    	// It handles automatically the odd and the even no of elements
    	for(i=0;i<ARR_SIZE/2;i++)
    	{
    		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;
    	for(i=0;i<ARR_SIZE;i++)
    		cout<<arr[i]<<"\t";
    	cout<<endl;
    
    	return 0;
    }
    
    Enjoy
     
  2. asadullah.ansari

    asadullah.ansari TechCake

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    Your code is very complex. Just update it. by this code.
    Code:
    #include<stdio.h>
    int main()
    {
     int a[10]={1,2,3,4,5,6,7,8,9,10},temp;
     int *ptr1=&a[0];
     int *ptr2=&a[9];
     while(ptr1<ptr2)
     {
       temp=*ptr1;
       *ptr1=*ptr2;
       *ptr2=temp;
       ++ptr1;
       --ptr2;
     }
     for(int i=0;i<10;++i)
       printf("%d\n",a[i]);
     return 0;
    }
    
     
  3. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,376
    Likes Received:
    388
    Trophy Points:
    83
    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

    Joined:
    Jan 9, 2008
    Messages:
    356
    Likes Received:
    14
    Trophy Points:
    0
    Occupation:
    Developer
    Location:
    NOIDA
    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

    Joined:
    Feb 5, 2008
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    0
    Yes!!! You are right.
     
  6. lead.smart34

    lead.smart34 New Member

    Joined:
    Feb 14, 2008
    Messages:
    77
    Likes Received:
    0
    Trophy Points:
    0
    obviously better
     
  7. crazytolearn57

    crazytolearn57 New Member

    Joined:
    Feb 14, 2008
    Messages:
    48
    Likes Received:
    0
    Trophy Points:
    0
    best
     
  8. aisha.ansari84

    aisha.ansari84 New Member

    Joined:
    Feb 13, 2008
    Messages:
    82
    Likes Received:
    1
    Trophy Points:
    0
  9. blessiepasag

    blessiepasag New Member

    Joined:
    Sep 17, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi!

    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

    Joined:
    Sep 17, 2010
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hello!

    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

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice