Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C++ and Visual C++ > C++ Programming
| Search | Today's Posts | Mark Forums Read
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 2nd, 2009, 05:08 AM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default 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");
}
__________________
MAXOOD!

Life is an endless journey towards perfection
Reply With Quote
  #2 (permalink)  
Old February 3rd, 2009, 02:22 AM
Friend of Wrox
 
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

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?
Reply With Quote
The Following User Says Thank You to Old Pedant For This Useful Post:
code_lover (February 25th, 2009)
  #3 (permalink)  
Old February 25th, 2009, 08:05 AM
Authorized User
 
Join Date: Jan 2007
Location: , , .
Posts: 46
Thanks: 2
Thanked 1 Time in 1 Post
Default 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
}

}
__________________
MAXOOD!

Life is an endless journey towards perfection
Reply With Quote
  #4 (permalink)  
Old February 25th, 2009, 09:10 AM
Registered User
 
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default 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
  }

}
Reply With Quote


Similar Threads
Thread Thread Starter Forum Replies Last Post
Reverse Feed senthilworld VB How-To 1 October 25th, 2007 03:07 PM
scalar and reverse crmpicco Perl 2 February 16th, 2007 08:18 AM
Reverse Engineering abdul_owiusa General .NET 0 July 18th, 2006 10:22 AM
reverse problem yui0329 C# 5 April 25th, 2005 08:12 AM
copying array to table (reverse of getrows?) rachelow VB Databases Basics 0 July 19th, 2003 03:10 AM





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