Friday, March 30, 2018

Second Smallest Number (Id-1242)

Just for Practice 1 Additional [3-Jan-2018 to 10-Apr-2018]

CSE1002 Bonus Practice Sheet Winter 2018 [17-Mar-2018 to 7-Apr-2018]
Program ID- 1242
Given a set of elements, design an Algorithm and write the subsequent C program to determine the second smallest number in that set.
Input Format

Number of elements in ‘n’

element-1

element-2



element-n

Output Format

Second smallest element in the set
Code: C
The Below Codes are for reference only. It doesn't mean everyone to directly copy/paste those codes.

#include<stdio.h>
#include<string.h>
int main()
{
    int n;
    scanf("%d",&n);
    int arr[n],i,j,k;
    for(i=0;i<n;i++)
    {
        scanf("%d",&arr[i]);
    }
    for(i=0;i<n;i++)
    {
        for(j=i+1;j<n;j++)
        {
            if(arr[i]>arr[j])
            {
                k=arr[j];
                arr[j]=arr[i];
                arr[i]=k;
            }
        }
    }
    printf("%d",arr[1]);
}

No comments:

Post a Comment