Answer to Question #249127 in C++ for Talha

Question #249127
Write a C++ program to rearrange a given sorted array of positive integers . Note: In final array, first element should be maximum value, second minimum value, third second maximum value , fourth second minimum value, fifth third maximum and so o n
1
Expert's answer
2021-10-11T00:05:49-0400
using namespace std;


/*
	Write a C++ program to rearrange a given sorted array of positive integers . 
	Note: In final array, first element should be maximum value, second minimum value, 
	third second maximum value , fourth second minimum value, fifth third maximum and so o n
*/


main()
{
	int x[] = {8, 7, 6, 5, 4, 3, 2, 1};
	int l,u,v,n=0;
	
	l = sizeof(x)/sizeof(x[0]);
	cout<<"\n\tOriginal Array : ";
	for(n=0;n<l;n++) cout<<x[n]<<", ";
	
	u=0;
	v=l-1;
	cout<<"\n\n \tRearraged Array: ";
	for(n=0;n<(l/2);n++)
	{
		cout<<x[u]<<", ";
		cout<<x[v]<<", ";
		u++;
		v--;
	}
	if(l%2==1) cout<<x[l/2];
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS