Code Snippets

  

C++ Source Code


Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 95,549 C++ Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 926 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert

Register to Make This Box Go Away!



Function to reverse an integer array

Reverses and array of integer.

Submitted By: cipherence
Actions:
Rating:
Views: 13,408

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


There are currently no comments for this snippet. Be the first to comment!

Add comment


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





Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->