Wednesday, June 27, 2012
C# 13 - Find the smallest value in an array
1. Create a new console project.
2. Edit the Program.cs inside static void Main(string[] args) file as shown below.
static void Main(string[] args)
{
int[] array = { 3, 2, 1, 4, 8 };
PrintArray(array);
Console.WriteLine("Smallest Value: " + GetSmallestValue(array));
Console.ReadLine();
}
private static int GetSmallestValue(int[] array)
{
int smallest = array[0];
for (int i = 0; i < array.Length; i++)
{
if (array[i] < smallest)
smallest = array[i];
}
return smallest;
}
private static void PrintArray(int[] array)
{
for (int i = 0; i < array.Length; i++)
{
Console.Write(array[i] + " ");
}
Console.WriteLine();
}
3. Press F5 to run and see the results.
4. Thank you.
Labels:
.Net,
Arrays,
C#,
C# statements,
C# Tutorial,
Console,
Free Tutorials,
Tutorial
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment