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 is the best way of making my program efficient?

572


What is the difference between exit() and _exit() function?

607


Compare array data type to pointer data type

600


write a program using linked list in which each node consists of following information. Name[30] Branch Rollno Telephone no i) Write the program to add information of students in linked list

2239


What extern c means?

546






Given two strings S1 and S2. Delete from S2 all those characters which occur in S1 also and finally create a clean S2 with the relevant characters deleted.

837


What are structure types in C?

671


What is extern storage class in c?

514


What is meant by operator precedence?

678


What is indirection in c?

629


program for reversing a selected line word by word when multiple lines are given without using strrev

1948


What is call by value in c?

559


Can a file other than a .h file be included with #include?

686


an expression contains relational operators, assignment operators, and arithmatic operstors. In the absence of parentheses, they will be evaluated in which of the following order a) assignment, relational, arithematic b) arithematic, relational, assignment c) relational, arithematic, assignment d) assignment, arithematic, relational

812


string reverse using recursion

1812