reverse the contents of an array

Discussion in 'C++' started by wldcb, Feb 27, 2006.

  1. wldcb

    wldcb New Member

    Joined:
    Feb 27, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    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}
     
  2. wldcb

    wldcb New Member

    Joined:
    Feb 27, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    I am still waiting for the reply.
     
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    Code:
    #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;
    }
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Welcome to the forum and I hope you have your answer.
     
  5. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    736
    Likes Received:
    39
    Trophy Points:
    28
    probably yes.
     
  6. wldcb

    wldcb New Member

    Joined:
    Feb 27, 2006
    Messages:
    3
    Likes Received:
    0
    Trophy Points:
    0
    Thanks and yes I have the solution ready now.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice