Find Second Largest Element of an Array in Windows Store App

In this article I explain how to find the second largest element of an array in a Windows Store App.

Introduction

In my previous article I described how to find the largest element of an array and in this article I will describe how to find the second highest element of an array. So here I find the second highest number.

Use the following procedure to create it.

Step 1

First of all you must create a new Windows Store Application.

Open Visual Studio 2012
"File" -> "New" -> "Project..."
Choose "Template" -> "Visual C#" -> "Window Store app"
Choose "Blank App (XAML)" then rename the application

new-windows-store-app.jpg

Step 2

Write the following XAML code in "Mainpage.Xaml" (that is available in Solution Explorer):

<Page

    x:Class="find_largest_no_in_array.MainPage"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:local="using:find_largest_no_in_array"

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

    mc:Ignorable="d">

 

    <Grid Background="Red">

        <Grid.RowDefinitions>

            <RowDefinition Height="110*"/>

            <RowDefinition Height="59*"/>

            <RowDefinition Height="40*"/>

            <RowDefinition Height="559*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="167*"/>

            <ColumnDefinition Width="156*"/>

            <ColumnDefinition Width="360*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Array content is :" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Foreground="White" Grid.Column="1"  Grid.Row="1" TextAlignment="Left" Margin="0,0,375,12" Grid.ColumnSpan="2"/>

        <TextBlock x:Name="text1" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Foreground="White" Grid.Row="1" Margin="10,10,0,0" Grid.Column="2" Width="401" VerticalAlignment="Top" HorizontalAlignment="Left" Height="23" />

        <TextBlock x:Name="text2" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Left" VerticalAlignment="Top" Height="23" Width="53"></TextBlock>

        <TextBlock Text=" Second Largest Number in array is :" FontSize="20" FontFamily="Arial" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2" Foreground="White" HorizontalAlignment="Left" Grid.RowSpan="2" Width="306"  />

    </Grid>

</Page>

 

Step 3

Now write the following C# code for the button within "Mainpage.Xaml.cs".

using System;

using System.Collections.Generic;

using System.IO;

using System.Linq;

using Windows.Foundation;

using Windows.Foundation.Collections;

using Windows.UI.Xaml;

using Windows.UI.Xaml.Controls;

using Windows.UI.Xaml.Controls.Primitives;

using Windows.UI.Xaml.Data;

using Windows.UI.Xaml.Input;

using Windows.UI.Xaml.Media;

using Windows.UI.Xaml.Navigation;

 

namespace find_largest_no_in_array

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }       

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

            int j, k=0, secondbig;

            int[] arr = { 59, 12, 88, 2, 9, 58, 4, 20, 6 };

            for (int i = 0; i < arr.Length; i++)

            {

                text1.Text += arr[i] + " ";

            }

            int big = arr[0];

            for ( j = 1; j < arr.Length; j++)

            {

                if (big < arr[j])

                {

                    big = arr[j];

                    k = j;

                }

            }

            secondbig = arr[arr.Length - k - 1];

            for (int m = 0; m < arr.Length; m++)

            {

                if (secondbig < arr[m] && k != m)

                {

                    secondbig = arr[m];

                }

            }

 

            text2.Text = secondbig.ToString();

        }

    }

}

 

Step 4

Now "Run" your app.

second-higest-element.jpg