21:57 -
Data Structures using C
Write a program in C using arrays to store a 10 integer data and find the second smallest and second largest elements in the array.
#include<stdio.h>
#include<conio.h>
/*function
declaration*/
largest_and_second_largest(int
list[],int &largest,int &slargest);
smallest_and_second_smallest(int
list[],int &smallest,int &ssmallest);
main()
{
int
a[10],i,j,largest,slargest,smallest,ssmallest;
clrscr();
for (i=0;i<10;i++)
{
printf("\n enter %d element
:", i+1);
scanf("%d",&a[i]);
}
largest_and_second_largest(a,largest,slargest);
printf("The
largest of the list is %d and the second largest is
%d\n",largest,slargest);
smallest_and_second_smallest(a,smallest,ssmallest);
printf("Smallest
is=%d and Second Smallest is=%d",smallest,ssmallest);
getch();
}
/*function
to find largest and second largest integer*/
largest_and_second_largest(int
list[],int &largest,int &slargest)
{
int n=10;
int largeindex=0,i;
largest=list[0];
for (i=1;i<n;i++) //find the largest loop
if (list[i]>largest)
{
largest=list[i];
largeindex=i; //to eliminate the largest later
}
//we have found the largest, stored in
largest and its
//index stored in largeindex. Now find the
second largest
//ignoring the largest.
if (largeindex==0)
slargest=list[1];
else
slargest=list[0];
for (i=0;i<n;i++)
if (list[i]>slargest &&
i!=largeindex)
slargest=list[i];
//we have found the second largest
}/*end
of largest_and_second_largest*/
/*function
to find smallest and second smallest integer*/
smallest_and_second_smallest(int
list[],int &smallest,int &ssmallest)
{
int n=10;
int smallindex=0,i;
smallest=list[0];
for (i=1;i<n;i++) //find the smallest loop
if (list[i]<smallest)
{
smallest=list[i];
smallindex=i; //to eliminate the smallestlater
}
//we have found the smallest, stored in
smallest and its
//index stored in smallindex. Now find the
second smallest
//ignoring the smallest.
if (smallindex==0)
ssmallest=list[1];
else
ssmallest=list[0];
for (i=0;i<n;i++)
if (list[i]<ssmallest &&
i!=smallindex)
ssmallest=list[i];
//we have found the second smallest
}/*end
of smallest_and_second_smallest*/