Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/articles/cpp-tutorials/)
-   -   Reverse the content of the array (http://www.go4expert.com/articles/reverse-content-array-t1273/)

coderzone 27Aug2006 05:16

Reverse the content of the array
 
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

asadullah.ansari 24Jan2008 18:13

Re: Reverse the content of the array
 
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;
}


shabbir 24Jan2008 21:17

Re: Reverse the content of the array
 
I see your one more complex but yes more efficient for ADT's as you are not moving the data but just the pointers.

asadullah.ansari 25Jan2008 11:03

Re: Reverse the content of the array
 
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.

debleena_doll2002 20Feb2008 11:25

Re: Reverse the content of the array
 
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.

lead.smart34 26Feb2008 18:21

Re: Reverse the content of the array
 
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

crazytolearn57 26Feb2008 18:36

Re: Reverse the content of the array
 
Quote:

Originally Posted by lead.smart34
obviously better

best

aisha.ansari84 5Mar2008 18:41

Re: Reverse the content of the array
 
nice

blessiepasag 17Sep2010 16:57

Re: Reverse the content of the array
 
Hi!

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

Thanks a lot..

blessiepasag 17Sep2010 17:02

Re: Reverse the content of the array
 
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!


All times are GMT +5.5. The time now is 16:22.