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
The process of repeatedly running a set of computer instructions until some condition is specifed a) condition b) sequential condition c) global d) iteration
what is different between auto and local static? why should we use local static?
What is the process of writing the null pointer?
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
Which header file should you include if you are to develop a function which can accept variable number of arguments?
Where static variables are stored in c?
What is the difference between formatted&unformatted i/o functions?
Are there constructors in c?
How do I round numbers?
What are the __date__ and __time__ preprocessor commands?
What is difference between static and global variable in c?
What do you understand by friend-functions? How are they used?
Do variables need to be initialized?
Explain what is a 'locale'?
How can you avoid including a header more than once?