reversing elements in array

This is a discussion on reversing elements in array within the C++ Programming forums, part of the General Programming Boards category; I've been trying to figure out how to reverse the elements in my array. But im drawing a blank. Any ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    61

    reversing elements in array

    I've been trying to figure out how to reverse the elements in my array. But im drawing a blank. Any help would be greatly appreciated.


    Code:
       #include <iostream>
    using namespace std;
    void reverse(int a[],int size );//prototype
    int main()
    {
    
     
     const int size=10;
    int a[size]={1,2,-3,-4,-5,-6,7,8,-9,10};
    
    
     reverse(a,size);//call
    
      return 0;
    
    }
    
    
     void reverse(int a[], int size) //reverse order of elements in array
     {
        
    	 for(int i=0;size<i;i++){
          
    	  cout<<a[i];
    	 }
    	 
    	  
    
    
    	 
    		   
    		
     }

  2. #2
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Well, I'll give you a hint:

    You still have the same number of elements right?
    You might want to look at using the '--' (decrement) operator and start your counter not at zero but a different value.

    Hope that helps!

    Nick.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  3. #3
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    Ok, what you said made a lot of sense and I altered the code but now I get this inifinite loop!

    Code:
         void reverse(int a[], int size) //reverse order of elements in array
     {
        
    	 for(int i=size;size>0;--i){
          
    	  cout<<a[i];
    	 }
    A little hint on why this may be occuring would be appreciated. thanks.

  4. #4
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    a little hint: if you have size elements in your array, they are indexed from 0 to size-1

  5. #5
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I'm thinking something like this:
    Code:
    void Reverse(int *Array,int Size)
    {
    	int LastIndex=size-1;
    	int TempInt=0;
    
    	if (!Array)
    		return;
    
    	for (int Loop=0;Loop<=(LastIndex/2);Loop++)
    	{
    		TempInt=Array[Loop];
    		Array[Loop]=Array[LastIndex-Loop];
    		Array[LastIndex-Loop]=TempInt;
    	}
    }
    I think it will work. To me it seems like the most logical way, but it's probably not the most efficient way.

    Basically it works by looping through the first half of the array and swapping each element with its far counterpart.
    benforbes@optusnet.com.au
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  6. #6
    Registered User
    Join Date
    Feb 2003
    Posts
    60
    Quote Originally Posted by dantestwin
    Ok, what you said made a lot of sense and I altered the code but now I get this inifinite loop!

    Code:
         void reverse(int a[], int size) //reverse order of elements in array
     {
        
    	 for(int i=size;size>0;--i){
          
    	  cout<<a[i];
    	 }
    A little hint on why this may be occuring would be appreciated. thanks.
    Your infinite loop is because of the test condition in the for loop. Replace "size > 0" with "i > 0" and it will work. Oh yeah, in the body of the loop, replace "a[i]" with "a[i-1]" to access the correct element. You've got everything right.
    "It compiled, let's ship it!"

    Guitar Australia
    my site for some easy tutorials.

  7. #7
    Registered User
    Join Date
    Jul 2004
    Posts
    61
    I got it to work. Thanks Nick.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count elements in an array
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-25-2007, 08:05 AM
  2. Setting all elements of an array to zero
    By kolistivra in forum C Programming
    Replies: 9
    Last Post: 08-29-2006, 03:42 PM
  3. funtion dealing with positive array elements
    By dantestwin in forum C++ Programming
    Replies: 5
    Last Post: 07-08-2004, 06:20 PM
  4. Type and nontype parameters w/overloading
    By Mr_LJ in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2004, 01:01 AM
  5. Reversing a Multi-D Array
    By Bradley33 in forum C++ Programming
    Replies: 1
    Last Post: 11-19-2001, 11:37 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21