Blogger Widgets

Total Page visits

Wednesday, April 24, 2013

Program in C# to find the second largest element in a single dimensional array



using System;
using System.Collections.Generic;
using System.Text;
namespace secondlargest
{
class Program
{
static void Main(string[] args)
{
int[] arr ={ 0, 5, 7, -2, 9, 12, 4, 8 };
int high, sechigh,temp;
high = sechigh = arr[0];
Console.WriteLine(“Array elements are”);
for (int i = 0; i < arr.Length; i++)
Console.Write(arr[i]+”  “);
Console.WriteLine();
for (int i = 0; i < arr.Length; i++)
{
temp = arr[i];
if (temp > high)
{
sechigh = high;
high = temp;
}
else if (temp > sechigh && temp != high)
sechigh = temp;
}
Console.WriteLine(“The second largest number in the array is  ” + sechigh);
Console.ReadLine();
}
}
}

No comments: