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

What are the types of type qualifiers in c?

852


which is conditional construct a) if statement b) switch statement c) while/for d) goto

1006


What is a class c rental property?

868


How is pointer initialized in c?

803


What does printf does?

996


How can I write a function that takes a format string and a variable number of arguments?

844


What is a program flowchart?

833


Where does the name "C" come from, anyway?

886


given post order,in order construct the corresponding binary tree

2546


What is meant by gets in c?

820


How do you convert a decimal number to its hexa-decimal equivalent.Give a C code to do the same

886


Where we use clrscr in c?

915


What do you know about the use of bit field?

825


Define and explain about ! Operator?

798


Can we assign string to char pointer?

824