Thursday, June 28, 2012
C# 14 - Find the largest 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("Largest Value: " + GetLargestValue(array));
Console.ReadLine();
}
private static int GetLargestValue(int[] array)
{
int largest = array[0];
for (int i = 0; i < array.Length; i++)
{
if (array[i] > largest)
largest = array[i];
}
return largest;
}
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.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment