View Code of Problem 49

#include <stdio.h>
#include <math.h>

int main(int argc, const char * argv[]) {
    int n;
    int temp;
    int min, max;
    int l = 0, h = 0;
    int num[100];
    
    scanf("%d", &n);
    for(int i = 0; i < n; i++){
        scanf("%d", &num[i]);
    }
    
    min = max = num[0];
    for(int i = 0; i < n; i++) {
        if(min > num[i]) {
            min = num[i];
            l = i;
        }
        if(max < num[i]) {
            max = num[i];
            h = i;
        }
    }
    
    temp = num[l];
    num[l] = num[0];
    num[0] = temp;
    
    temp = num[h];
    num[h] = num[n - 1];
    num[n - 1] = temp;
   
    int i;
    for(i = 0; i < n - 1; i++){
        printf("%d ", num[i]);
    }
    printf("%d", num[i]);
    return 0;
}

Double click to view unformatted code.


Back to problem 49