Q:

Find smallest element from integer array in C#

belongs to collection: C# Basic Programs | array programs

0

For example we have list of integers:

18, 13, 23, 12, 27

Initially large = 18; In first comparison small > 13; true , Now small becomes 13. In second comparison small > 23; false , Now small is 13. In third comparison small > 12; true , Now small becomes 12. In forth comparison small > 27; false , Now small is 12.

 

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Consider the example:

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

namespace ConsoleApplication1
{
	class Program
	{
		static void Main()
		{
			int i       = 0;
			int small   = 0;
			//integer array declaration
			int[] arr   = new int[5];

			Console.WriteLine("Enter array elements : ");
			//read array elements
			for (i = 0; i < arr.Length; i++)
			{
				Console.Write("Element[" + (i + 1) + "]: ");
				arr[i] = int.Parse(Console.ReadLine());
			}

			//assign fist element to the 'small' 
			//compare it with other array elements
			small = arr[0];

			for (i = 1; i < arr.Length; i++)
			{
				//compare if small is greater than of any element of the array
				//assign that element in it.
				if (small > arr[i])
					small = arr[i];
			}

			//finally print the smallest elemeent of the integer array
			Console.WriteLine("Smallest element in array is : " + small);
		}
	}
}

Output

Enter array elements :
Element[1]: 12
Element[2]: 13
Element[3]: 10
Element[4]: 25
Element[5]: 8
Smallest element in array is : 8

 

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

C# Basic Programs | array programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Find palindrome numbers from array using C# progra... >>
<< Find largest element from an integer array in C#...