Ad

Thursday, July 18, 2013

Find Maximum value of an Array without Using Predefined Method in C#.Net?

Overview:
In this article i would like to present Find Maximum value of an Array without Using Predefined Method.In Previous Article i would like to publish How To Find the Largest Number using Arrays in c#.Net?



Description:
                Array can be defined as a collection of similar dataitems.In C#.Net array act as a ReferenceType.We can access the elements of an array using index values.

Syntax:

                    DataType[] NameOf The Array=new DataType[size];

E.g:
                   int[] a=new int[5];
                                (or)
                   int[] a=new int[]{12,10,1,3,5};
Note:
  1. If we Declare the size of the array it can take exactly that size number of values.
  2. If we are not declare the size of the array then we can accommodate n number of values.
Program:


using System;

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

namespace arr
{
    class Class1
    {
        static void Main(string[] args)
        {
            int size,max,loc=1;
            Console.WriteLine("Enter the Number of Elements In an Array");
            size = Convert.ToInt32(Console.ReadLine());
            int[] a=new int[size];          
            Console.WriteLine("Enter the Elements Of The Array");
            for (int i = 0; i < a.Length; i++)
            {
                a[i] = Convert.ToInt32(Console.ReadLine());
            }
            max = a[0];
            for (int j = 0; j < a.Length; j++)
            {
                if (a[j] > max)
                {
                    max = a[j];
                    loc = j + 1;
                }
            }
            Console.WriteLine("maximum Element Present at location {0} and its value is {1}",loc,max);
            Console.ReadLine();
        }
    }
}

  Output:

Maximum Element Of Array Without Using Predefined Functions in C#

No comments: