View Code of Problem 49

#include<bits/stdc++.h>
using namespace std;
int main(){
	int a,b[100],min,max,posx = 0,posm = 0;
	cin>>a;
	for(int i = 0;i<a;i++)cin>>b[i];
	max = min = b[0];
	for(int i = 1;i<a;i++){
		if(max < b[i]){
			max = b[i];	
			posx = i;
		}
	}
	
	for(int i = 1;i<a;i++){
		if(min > b[i]){
			min = b[i];	
			posm = i;
		}
	}
	int temp;
	temp = b[0];
	b[0] = b[posm];
	b[posm] = temp;
	
	temp = b[a-1];
	b[a-1] = b[posx];
	b[posx] = temp;
	
	
	
	for(int i = 0;i < a;i++){
		cout<<b[i];
		if(i != a-1)cout<<" ";	
	}
	return 0;
} 

Double click to view unformatted code.


Back to problem 49