Chat LIVE With Programming Experts! There Are 23 Online Right Now...

 

Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,278 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,170 people online right now. Registration is fast and FREE... Join Now!





Function to reverse an integer array

Reverses and array of integer.

Submitted By: cipherence
Actions:
Rating:
Views: 28,631

Language: C++

Last Modified: August 28, 2006
Instructions: This function(ReverseArray is used to reverse an integer array. For example, if the
array is {1,2,3,4,5}, these functions will return an array like {5,4,3,2,1}.
The user needs to pass in a pointer to the array and the size of the array,
and the function will return a pointer to an array in the reverse order.

Snippet


  1. // this code was copied from cprogramming.com
  2. // my respects for posting this code with
  3. // no official whereabouts as to where it was from
  4.  
  5.  
  6. int*ReverseArray(int*orig,unsigned short int b)
  7. {
  8.     unsigned short int a=0;
  9.     int swap;
  10.    
  11.     for(a;a<--b;a++) //increment a and decrement b until they meet eachother
  12.     {
  13.         swap=orig[a];       //put what's in a into swap space
  14.         orig[a]=orig[b];    //put what's in b into a
  15.         orig[b]=swap;       //put what's in the swap (a) into b
  16.     }
  17.    
  18.     return orig;    //return the new (reversed) string (a pointer to it)
  19. }
  20. // the following is a test program
  21. #include<iostream>
  22.  
  23. int main()
  24. {
  25.     const unsigned short int SIZE=10;
  26.     int ARRAY[SIZE]={1,2,3,4,5,6,7,8,9,10};
  27.     int*arr=ARRAY;
  28.    
  29.     for(int i=0;i<SIZE;i++)
  30.     {
  31.         std::cout<<arr[i]<<' ';
  32.     }
  33.     std::cout<<std::endl;
  34.     arr=ReverseArray(arr,SIZE);
  35.     for(int i=0;i<SIZE;i++)
  36.     {
  37.         std::cout<<arr[i]<<' ';
  38.     }
  39.     std::cout<<std::endl;
  40.     std::cin.get();
  41.     return 0;
  42. }
  43.  
  44.  

Copy & Paste


Comments


David W 2008-10-07 01:48:54

/* A simplified demo of reversing an array of integers */ #include /* Recall that in C (or C++) arrays are always passed by ref */ myReverseArray( int ary[], int b ) { int a; for( a=0; a < --b; a++ ) /* increment a and decrement b until they meet */ { int temp = ary[a]; /* get temp copy of ary[a] */ ary[a] = ary; /* ary

David W 2008-10-07 01:54:12

CODE
/* Recall that in C (or C++) arrays are always passed by ref */ myReverseArray( int ary[], int b ) { int a; for( a=0; a < --b; a++ ) /* increment a and decrement b until they meet */ { int temp = ary[a]; /* get temp copy of ary[a] */ ary[a] = ary<b>; /* ary<b> <-- ary[a] */ ary<b> = temp; /* ary<b> <-- temp */ } }

sithujabi 2008-12-02 08:03:51

hey tanks alot........it was very much helpful for me.........tank u once again....


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.





Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month