It’s a basics code, just sharing it since since will be useful for some body, who is in needy.

I am just refresing my OOPS and Basic knowledge, just experimenting. Ha ha ha.

Advice to guyz, don’t simply copy it, try to understand the logic and do it by yourself.

[code lang=”c-sharp”]

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

namespace ConsoleAppPro
{
/// <summary>
///
/// </summary>
public class NumberOps
{

/// <summary>
/// Runs the number ops.
/// </summary>
public void RunNumberOps()
{
Random rand = new Random();

int[] input = new int[20];

Console.WriteLine("Numbers to Process");
Console.WriteLine("—————————-");

for (int i = 0; i < 20; i++)
{
input[i] = rand.Next(100); //Generating numbers in random (below 100)

Console.Write(String.Format("{0},", input[i]));
}

Console.WriteLine("nnRESULTn—————————-");
FirstNSecondLargest(input);

Console.Read();
}

/// <summary>
/// Firsts the N second largest.
/// </summary>
/// <param name="inVals">The in vals.</param>
public void FirstNSecondLargest(int[] inVals)
{

int largest = 0;
int smallest = 0;
int secondLargest = 0;
int secondSmallest = 0;

largest = inVals[0];
secondLargest = 0;
secondSmallest = 0;
smallest = inVals[0];

for (int iIndex = 0; iIndex < inVals.Length; iIndex++)
{
if (inVals[iIndex] > largest) //Largest Value
{
secondLargest = largest;
largest = inVals[iIndex];
}
else if (inVals[iIndex] >= secondLargest && inVals[iIndex] != largest)
{
secondLargest = inVals[iIndex]; //second largest
}

if (smallest > inVals[iIndex]) //Smallest Value
{

secondSmallest = smallest;
smallest = inVals[iIndex];
}
else if (inVals[iIndex] < secondSmallest && inVals[iIndex] != smallest)
{
secondSmallest = inVals[iIndex]; //second smallest
}

}

Console.WriteLine(String.Format("Largest: {0}, Second Largest: {1}, Second Smallest: {3}, Smallest: {2}", largest, secondLargest, smallest, secondSmallest));

}
}
}

[/code]

Smallest picture

PS: I have tested this only few times. If you find any issues in the logic. please notify me, so that i can fix it up for later use.

This is just an information sharing, hope this helps some body. Tweak it, if could and Happy Coding!!!


Discover more from Cloud Distilled ~ Nithin Mohan

Subscribe to get the latest posts sent to your email.

By Nithin Mohan TK

Technology Enthusiast | .NET Specialist | Blogger | Gadget & Hardware Geek

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.