Friday, July 31, 2009

C++ prog to reverse an array?

int*ReverseArray(int*orig,unsigned short int b)


{


unsigned short int a=0;


int swap;





for(a;a%26lt;--b;a++) //increment a and decrement b until they meet eachother


{


swap=orig[a]; //put what's in a into swap space


orig[a]=orig[b]; //put what's in b into a


orig[b]=swap; //put what's in the swap (a) into b


}





return orig; //return the new (reversed) string (a pointer to it)


}

C++ prog to reverse an array?
#include%26lt;iostream.h%26gt;


void main()


{


int arr[]={1,4,5,3,25,7,8,48,9};


int size=9,lo=0,high=size-1;


for(lo=0;lo%26lt;=size/2;lo++,high--)


{


int t=arr[lo];


arr[lo]=arr[high];


arr[high]=t;


}


cout%26lt;%26lt;"\nReverse Array ";


for(int i=0;i%26lt;size;i++)


{


cout%26lt;%26lt;arr[i]%26lt;%26lt;",";


}


}
Reply:#include%26lt;iostream.h%26gt;


#include%26lt;conio.h%26gt;


#define MAX 10


void main()


{


int arr[MAX],l,high=MAX-1,temp;


clrscr();


cout%26lt;%26lt;"\n Enter all elements of array: ";


for(i=0;i%26lt;MAX;i++)


cin%26gt;%26gt;arr[i];


for(i=0;i%26lt;=MAX/2;i++)


{


temp=arr[i];


arr[i]=arr[high];


arr[high]=temp;


high--;


}





cout%26lt;%26lt;"\nReverse Array ";


for(int i=0;i%26lt;MAX;i++)


{


cout%26lt;%26lt;arr[i]%26lt;%26lt;" ";


}


getch();


}


No comments:

Post a Comment