CSE1701
Second Smallest Number (Id-2342)
Given a
set of elements, write an algorithm and the subsequent ‘C’ program to determine
the second smallest element in it.
Input
Format
Number
of elements in 'n'
Element-1
Element-2
...
Element-n
Output
Format
Second
smallest element in the set
Input for
the problem
Get the n elements
Processing
involved
#include
<stdio.h>
int
main()
{
int arr[20];
int n;
scanf("%d",&n);
for(int i = 0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int i, first=32766, second=32766;
for (i = 0; i < n ; i ++)
{
if (arr[i] < first)
{
second = first;
first = arr[i];
}
else if (arr[i] < second &&
arr[i] != first)
second = arr[i];
}
printf("%d",second);
return 0;
}
Output
for the problem
Second
smallest element in the set
Pseudocode
Step1.Get the number of elements n and then get
the elements
Step2. Find the smallest element
Step3. Find the second smallest element by finding
the smallest number greater than the smallest number
Step4.Display the second smallest element
Step5.End
Program:
#include
<stdio.h>
int
main()
{
int arr[20];
int n;
scanf("%d",&n);
for(int i = 0;i<n;i++)
{
scanf("%d",&arr[i]);
}
int i, first=32766, second=32766;
for (i = 0; i < n ; i ++)
{
if (arr[i] < first)
{
second = first;
first = arr[i];
}
else if (arr[i] < second &&
arr[i] != first)
second = arr[i];
}
printf("%d",second);
return 0;
}
Given a set of elements, write an algorithm and the subsequent ‘C’ program to perform cyclic right shift of the array by 'm' places. For example, if the elements are 12, 13, 16, 7, 10 and m =2 then the resultant set will be 7, 10, 12, 13, 16.
ReplyDeleteInput Format
Number of elements in 'n'
element1
element2
...
elementn
value of 'm'
Output Format
Elements in the set after right shift by 'm' places