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

The process of repeatedly running a set of computer instructions until some condition is specifed a) condition b) sequential condition c) global d) iteration

724


what is different between auto and local static? why should we use local static?

741


What is the process of writing the null pointer?

696


int i=3; this declaration tells the C compiler to a) reserve space in memory to hold the integer value b) associate the name i with this memory location c) store the value 3 at this location d) all the above

848


Which header file should you include if you are to develop a function which can accept variable number of arguments?

916






Where static variables are stored in c?

671


What is the difference between formatted&unformatted i/o functions?

713


Are there constructors in c?

696


How do I round numbers?

689


What are the __date__ and __time__ preprocessor commands?

674


What is difference between static and global variable in c?

623


What do you understand by friend-functions? How are they used?

757


Do variables need to be initialized?

706


Explain what is a 'locale'?

694


How can you avoid including a header more than once?

637