Find Smallest Element of an Array in Windows Store App

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

Introduction

I have already explained that an array is a collection of similar data types that contain several elements of the same data type within a single array. So I want to search for the smallest element of an array.

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_out_smallest_element_of_an_array.MainPage"

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

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

    xmlns:local="using:find_out_smallest_element_of_an_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="75*"/>

            <RowDefinition Height="53*"/>

            <RowDefinition Height="54*"/>

            <RowDefinition Height="586*"/>

        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>

            <ColumnDefinition Width="303*"/>

            <ColumnDefinition Width="275*"/>

            <ColumnDefinition Width="788*"/>

        </Grid.ColumnDefinitions>

        <TextBlock Text="Array content is :" FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="1" ></TextBlock>

        <TextBlock Text="Smalllest element is :" FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="1" Grid.Row="2" ></TextBlock>

        <TextBlock x:Name="text1" FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="2" Grid.Row="1" HorizontalAlignment="Left"></TextBlock>

        <TextBlock x:Name="text2" FontFamily="Arial" FontSize="20" FontWeight="ExtraBold" Grid.Column="2" Grid.Row="2" HorizontalAlignment="Left"></TextBlock>

    </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_out_smallest_element_of_an_array

{

    public sealed partial class MainPage : Page

    {

        public MainPage()

        {

            this.InitializeComponent();

        }

        protected override void OnNavigatedTo(NavigationEventArgs e)

        {

            int[] arr = { 14, 85, 57, 25, 36, 2, 5, 9, 5, 95 };

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

            {

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

            }

            int smallest = arr[0];

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

            {

                if (smallest > arr[j])

                {

                    smallest = arr[j];

                }

            }

            text2.Text += smallest.ToString();

        }

    }

}

 

Step 4


Now "Run" your app.


smallest-element-of-array.jpg