View Code of Problem 49

#include<stdio.h>
int main(){
	int i,n,a[100],min,max,temp;
	scanf("%d",&n);
	for(i=0;i<n;i++){
		scanf("%d",&a[i]);
	}
	min=max=0;
	for(i=0;i<n;i++){
		if(a[i]>a[max]){
			max=i;
		}
		if(a[i]<a[min]){
			min=i;
		}
	}

	if(min==n-1&&max==0){
		temp = a[min];
		a[min] = a[max];
		a[max]=temp;
	}else{
		temp = a[min];
		a[min] = a[0];
		a[0]=temp;
		temp = a[max];
		a[max] = a[n-1];
		a[n-1]=temp;
	
	
	}
	for(i=0;i<n;i++){
		if(i==n-1){
			printf("%d",a[i]);
		}else{
			printf("%d ",a[i]);
		}
	}

	return 0;
}

Double click to view unformatted code.


Back to problem 49