View Code of Problem 49

#include <stdio.h>
#include <malloc.h>
main(){
	int n,*a,i,min,max,t1;
	scanf("%d",&n);
	a = (int *)malloc(n*sizeof(int));
	for(i = 0; i<n; i++)
		scanf("%d",&a[i]);
	max = a[0];t1 = 0;
	for(i = 1; i<n; i++){
		if(max < a[i]){
			max = a[i];
			t1 = i;
		}
	}
	a[t1] = a[n-1];a[n-1] = max;
	min = a[0];	t1 = 0;
	for(i = 1; i<n; i++){
		if(min > a[i]){
			min = a[i];
			t1 = i;
		}
	}	
	a[t1] = a[0];a[0] = min;
	for(i = 0;i<n-1;i++){
		printf("%d ",a[i]);
	}
	printf("%d",a[n-1]);
	free(a);
	return 0;
}

Double click to view unformatted code.


Back to problem 49