View Code of Problem 49

#include<iostream> 
#include <stdio.h>
#include <math.h>
#include "string.h"
using namespace std;
int main(){
	int a[10];
	int n;
	cin>>n;
	for(int i=0;i<n;i++)
	cin>>a[i];
	int min=a[0];
	int mini=0;
	for(int i=0;i<n;i++){
		if(min>a[i]){
			min=a[i];
			mini=i;
		}
	}
	swap(a[0],a[mini]);
	int max=a[0];
	int maxi=0;
	for(int i=0;i<n;i++){
		if(max<a[i]){
			max=a[i];
			maxi=i;
		}
	}
	swap(a[n-1],a[maxi]);
	for(int i=0;i<n;i++){
		cout<<a[i];
		if(i!=n-1){
			cout<<" ";
		}
	}
	
	return 0;
}

Double click to view unformatted code.


Back to problem 49