Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   reverse the contents of an array (http://www.go4expert.com/forums/reverse-contents-array-t607/)

wldcb 27Feb2006 22:15

reverse the contents of an array
 
Need code to reverse the contents of an array. NOT sort in ascending/descending order, but put last array entry to first, etc. EG.. if array consists of {2,3,4,7,12,98},, need to output {98, 12,7,4,3,2}

wldcb 28Feb2006 07:35

Re: reverse the contents of an array
 
I am still waiting for the reply.

coderzone 28Feb2006 07:36

Re: reverse the contents of an array
 
Quote:

Originally Posted by wldcb
Need code to reverse the contents of an array. NOT sort in ascending/descending order, but put last array entry to first, etc. EG.. if array consists of {2,3,4,7,12,98},, need to output {98, 12,7,4,3,2}

Code: CPP

#define ARR_SIZE 11

int main()
{
    int arr[ARR_SIZE] = {1,2,3,4,5,6,7,8,9,0,11};
    int i = 0;

    cout<<"Array content as input"<<endl;
    for(i=0;i<ARR_SIZE;i++)
        cout<<arr[i]<<"\t";
    cout<<endl;

    for(i=0;i<ARR_SIZE/2;i++)
    {
        int temp = arr[i];
        arr[i] = arr[ARR_SIZE - i-1];
        arr[ARR_SIZE - i-1] = temp;
    }

    cout<<"Array content as output"<<endl;
    for(i=0;i<ARR_SIZE;i++)
        cout<<arr[i]<<"\t";
    cout<<endl;

    return 0;
}


shabbir 28Feb2006 07:41

Re: reverse the contents of an array
 
Welcome to the forum and I hope you have your answer.

coderzone 28Feb2006 07:41

Re: reverse the contents of an array
 
probably yes.

wldcb 28Feb2006 07:50

Re: reverse the contents of an array
 
Thanks and yes I have the solution ready now.


All times are GMT +5.5. The time now is 09:17.