View Code of Problem 49

#include <stdio.h>
#include <string.h>

int main()
{
    int n;
    scanf("%d",&n);
    int num[n];
    int temp[n];
    int i,j;
    for(i=0;i<n;i++)
    {
        scanf("%d",&num[i]);
        temp[i]=num[i];
    }
      for(i=0;i<n;i++)
    {
        temp[i]=num[i];
       //printf("%d ",temp[i] );
    }
    int tmp=0;
 
    for(i=0;i<n-1;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(temp[j]>temp[j+1])
            {
                tmp=temp[j];
                temp[j]=temp[j+1];
                temp[j+1]=tmp;
            }
        } 
    }

    
    int a,b;
    for (i = 0; i < n; i++)
    {
       if(temp[0]==num[i])
        a=i;
    }

    for (i = 0; i < n; i++)
    {
       if(temp[n-1]==num[i])
        b=i;
    }

    //printf("%d %d\n",a,b );
    num[a]=num[0];
    num[b]=num[n-1];
    num[0]=temp[0];
    num[n-1]=temp[n-1];

    for (i = 0; i < n; i++)
    {
        printf("%d",num[i] );
        if(i!=n-1)
            printf(" ");
    }
    
    return 0;
  }

Double click to view unformatted code.


Back to problem 49