Reverse the content of the array

coderzone's Avatar author of Reverse the content of the array
This is an article on Reverse the content of the array in C++.
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: CPP
#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
TechCake
24Jan2008,18:13   #2
asadullah.ansari's Avatar
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;
}
Go4Expert Founder
24Jan2008,21:17   #3
shabbir's Avatar
I see your one more complex but yes more efficient for ADT's as you are not moving the data but just the pointers.
TechCake
25Jan2008,11:03   #4
asadullah.ansari's Avatar
Quote:
Originally Posted by shabbir
I see your one more complex but yes more efficient for ADT's as you are not moving the data but just the pointers.
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 by asadullah.ansari; 25Jan2008 at 11:08..
Ambitious contributor
20Feb2008,11:25   #5
debleena_doll2002's Avatar
Quote:
Originally Posted by shabbir
I see your one more complex but yes more efficient for ADT's as you are not moving the data but just the pointers.
Yes!!! You are right.
Contributor
26Feb2008,18:21   #6
lead.smart34's Avatar
Quote:
Originally Posted by asadullah.ansari
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;
}
obviously better
Go4Expert Member
26Feb2008,18:36   #7
crazytolearn57's Avatar
Quote:
Originally Posted by lead.smart34
obviously better
best
Contributor
5Mar2008,18:41   #8
aisha.ansari84's Avatar
nice
Newbie Member
17Sep2010,16:57   #9
blessiepasag's Avatar
Hi!

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

Thanks a lot..
Newbie Member
17Sep2010,17:02   #10
blessiepasag's Avatar
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!