write a program to fined second smallest and largest element
in a given series of elements (without sorting)

Answer Posted / siddharth chauhan

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication13
{
class Program
{
static void Main(string[] args)
{

Console.WriteLine("Enter Array Length.");
int ArrayLength =
Convert.ToInt32(Console.ReadLine());
int[] arr = new int[ArrayLength];

Console.WriteLine("\nEnter Array Elements");
for (int i = 0; i < arr.Length; i++)
{
arr[i] = Convert.ToInt32(Console.ReadLine());
}

Console.WriteLine("\nElements are :- ");
for (int i = 0; i < arr.Length; i++)
{
Console.WriteLine(arr[i]);
}

int Largest = arr[0], SecondLargest = arr[0],
Smallest = arr[0], SecondSmallest = arr[0];
for (int j = 1; j < arr.Length; j++)
{
if (Smallest > arr[j])
{
Smallest = arr[j];
}

if (Largest < arr[j])
{
Largest = arr[j];
}
}

for (int j = 1; j < arr.Length; j++)
{
int value = arr[j];

if (arr[j] < SecondSmallest && arr[j] !=
Smallest)
{
SecondSmallest = arr[j];
}

if (SecondLargest < arr[j] && arr[j] < Largest)
{
SecondLargest = arr[j];
}
}

Console.WriteLine("Largest : " + Largest);
Console.WriteLine("Second Largest : " +
SecondLargest);
Console.WriteLine("Smallest : " + Smallest);
Console.WriteLine("Second Smallest : " +
SecondSmallest);
Console.ReadLine();

}

Is This Answer Correct ?    3 Yes 4 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Do you know the difference between malloc() and calloc() function?

817


What is extern storage class in c?

715


What is dynamic variable in c?

784


What is the use of gets and puts?

773


What is bubble sort in c?

824


How do I send escape sequences to control a terminal or other device?

837


What is malloc and calloc?

800


What is an expression?

841


Tell me when is a void pointer used?

862


Why main is used in c?

822


What is return type in c?

882


Explain enumerated types.

798


What is c variable?

797


What is extern keyword in c?

869


Can two or more operators such as and be combined in a single line of program code?

1111