p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   C++ Programming (http://p2p.wrox.com/forumdisplay.php?f=89)
-   -   How to reverse an array here? (http://p2p.wrox.com/showthread.php?t=72517)

code_lover February 2nd, 2009 05:08 AM

How to reverse an array here?
 
#include <iostream.h>
const int MAX = 5;
class array
{
private:
int arr[MAX];
public:
void populate();
void reverse();
void display();
};

void array::populate()
{
int n;
for (short i=0;i<=MAX;i++)
{
cout<<"\nEnter number:";
cin>>n;
arr[i]=n;
}
}

void array::reverse()
{
for (short i = 0;i<=MAX;i++)
{
arr[i] = arr[(MAX + 1)- i];

}

//arr[0]=arr[6];
//arr[1]=arr[5];
//arr[2]=arr[4];
//arr[3]=arr[3];
//arr[4]=arr[2];
//arr[5]=arr[1];
//arr[6]=arr[0];
}

void array::display()
{
for (short i = 0;i<=MAX;i++)
cout<<arr[i]<<endl;
}

main()
{
array a;

a.populate();
a.display();

a.reverse();
a.display();

system("pause");
}

Old Pedant February 3rd, 2009 02:22 AM

Think about what you have:

Code:

const int MAX = 5;
...
int arr[MAX];

So how many ELEMENTS will be IN that array???

Yes, 5.

But HOW are they NUMBERED???

Answer:
Code:

arr[0]
arr[1]
arr[2]
arr[3]
arr[4]

Yes, zero through 4. Only.

So even your display( ) function is wrong:
Code:

void array::display()
{
    for (short i = 0; i <= MAX; i++)
        cout<<arr[i]<<endl;
}

You should be using < MAX, *NOT* <= MAX.

SO now look at your code to reverse the elements, *EVEN AFTER* we change the <= to just < :
Code:

void array::reverse()
{
      for (short i = 0; i < MAX; i++)
    {
        arr[i] = arr[(MAX  + 1)- i];
                }
}

Let's "unroll" the loop and see what you are *ACTUALLY* doing:
Code:

arr[0] = arr[(5+1)-0] ==>> arr[0] = arr[6]
arr[1] = arr[(5+1)-1] ==>> arr[1] = arr[5]
arr[2] = arr[(5+1)-2] ==>> arr[2] = arr[4]
arr[3] = arr[(5+1)-3] ==>> arr[3] = arr[3]
arr[4] = arr[(5+1)-4] ==>> arr[4] = arr[2]

OOPS!!!

You *SHOULD* have been using
Code:

        arr[i] = arr[(MAX  - 1)- i]; // -1, *NOT* +1
But that's *STILL* not enough!

Let's make an example array:
Code:

arr[0] = 777
arr[1] = 222
arr[2] = 444
arr[3] = 888
arr[4] = 333

With YOUR code, even after my -1 adjustment, you would be doing:
Code:

arr[0] = arr[(5-1)-0] ==>> arr[0] = arr[4], so arr[0] is now 333
arr[1] = arr[(5-1)-1] ==>> arr[1] = arr[3], so arr[1] is now 888
arr[2] = arr[(5-1)-2] ==>> arr[2] = arr[2], so arr[2] is still 444
 arr[3] = arr[(5-1)-3] ==>> arr[3] = arr[1], so arr[3] is now 888!  got the NEW value of arr[1]!
 arr[4] = arr[(5-1)-4] ==>> arr[4] = arr[0], so arr[4] is now 333!  got the NEW value of arr[0]!

So you can see that this is complete mistake! You can *NOT* swap an array like that, *AT ALL*.

I'm sure this is homework for some class, and we don't do homework. (Or at least I won't.) We WILL help you when you run into trouble, as I have just done.

But now it is time for you to go put on your thinking cap and figure out the *RIGHT* way to swap an array.

*************

p.s.: Weren't you suspicious when you couldn't even get your populate( ) function to work??? Didn't it give you an error when you tried to put the 6th value into your 5 element array? Or are you possibly not running this with a debugger and you just got an undiagnosed crash?

code_lover February 25th, 2009 08:05 AM

Reversing an array: which method is efficient
 
ok i have figured out where i was wrong and have come up with this code here:

for (int i=0;i<=MAX/2;i++)
{
int temp;
temp = arr[i];
arr[i] = arr[(MAX-1)-i];
arr[(MAX-1)-i] = temp;
}

where MAX is the total number of elements an array has.

Another piece of code that i have found was this one, i like you to comment whether this one is efficient or the one above.

void Reverse(int *first,int *last)

{
int dum;
for(int x=0;((x>=0) && (x<=((last-first)/2)));x++)

{
dum=*(first+x);//store value of location first+x in dummy var
*(first+x)=*(last-(1+x)); // set loc first+x to whats in last-x
*(last-(1+x))=dum; // assign mem loc last-x dum
}

}

u6022 February 25th, 2009 09:10 AM

Can last < first?
 
Why the
Code:

(x>=0) &&
in

Code:

void Reverse(int *first,int *last)
{
  int dum;
  for(int x=0;((x>=0) && (x<=((last-first)/2)));x++)
  {
    dum=*(first+x);//store value of location first+x in dummy var
    *(first+x)=*(last-(1+x)); // set loc first+x to whats in last-x
    *(last-(1+x))=dum; // assign mem loc last-x dum
  }

}



All times are GMT -4. The time now is 01:24 PM.

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.