|
C# Example: The Minimum and Maximum Values in an Array |
|
|
This series of sample programs shows how to find the
highest number in a series. In these examples, we will deal with arrays.
We will use single and multi-dimensional.
|
In the first example, we create a simple array of
double-precision numbers (the numbers were selected randomly). To look for
the lowest or the highest number in the range, we use a for loop:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise
{
public class Program
{
static int Main(string[] args)
{
var Numbers = new double[10];
var Minimum = 0D;
var Maximum = 0D;
Numbers[0] = 4478.72;
Numbers[1] = 67.97;
Numbers[2] = 2.32;
Numbers[3] = 13004.05;
Numbers[4] = 0.44;
Numbers[5] = 873846.08;
Numbers[6] = 120.12;
Numbers[7] = 479.23;
Numbers[8] = 12.42;
Numbers[9] = 873846.04;
Minimum = Numbers[0];
for (var i = 0; i < Numbers.Length; i++)
{
Console.WriteLine("Number {0,2}:{1, 10}", i + 1, Numbers[i]);
}
for (var i = 0; i < Numbers.Length; i++)
{
if (Minimum > Numbers[i])
Minimum = Numbers[i];
}
Console.WriteLine();
Console.WriteLine("Minimum = {0}", Minimum);
for (int i = 0; i < Numbers.Length; i++)
{
if (Maximum < Numbers[i])
Maximum = Numbers[i];
}
Console.WriteLine("Maximum = {0}\n", Maximum);
return 0;
}
}
}
Here is an example that uses methods:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace MinMax
{
public class Exercise
{
private int Min(int x, int y)
{
if (x < y)
return x;
return y;
}
private int Max(int x, int y)
{
if (x > y)
return x;
return y;
}
private int Min(int[] numbers)
{
int m = numbers[0];
for (int i = 0; i < numbers.Length; i++)
if (m > numbers[i])
m = numbers[i];
return m;
}
private int Max(int[] numbers)
{
int m = numbers[0];
for (int i = 0; i < numbers.Length; i++)
if (m > numbers[i])
m = numbers[i];
return m;
}
public static int Main()
{
int a = 24, b = 26;
int[] nbrs = { 27, 2, 93, 7957, 4, 795, 14 };
Exercise exo = new Exercise();
Console.WriteLine("The minimum of 14 and 26 is {0}", exo.Min(a, b));
Console.WriteLine("The maximum of 14 and 26 is {0}", exo.Max(a, b));
Console.WriteLine("The minimum of the array is {0}", exo.Min(nbrs));
Console.WriteLine("The maximum of the array is {0}", exo.Max(nbrs));
return 0;
}
}
}
This would produce:
The minimum of 14 and 26 is 24
The maximum of 14 and 26 is 26
The minimum of the array is 2
The maximum of the array is 7957
Press any key to continue . . .
This example uses a two-dimensional array to find the
largest and the lowest values in the series:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Exercise
{
public class Program
{
static int Main(string[] args)
{
var Numbers = new double[2, 5];
var Minimum = 0D;
var Maximum = 0D;
Numbers[0, 0] = 67.97;
Numbers[0, 1] = 873846.04;
Numbers[0, 2] = 2.32;
Numbers[0, 3] = 4478.72;
Numbers[0, 4] = 13004.05;
Numbers[1, 0] = 479.23;
Numbers[1, 1] = 873846.08;
Numbers[1, 2] = 0.44;
Numbers[1, 3] = 12.42;
Numbers[1, 4] = 120.12;
Minimum = Numbers[0, 0];
int counter = 1;
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
Console.WriteLine("Number {0}:\t{1}", counter, Numbers[i, j]);
counter++;
}
}
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
if (Minimum > Numbers[i, j])
Minimum = Numbers[i, j];
}
}
Console.WriteLine();
Console.WriteLine("Minimum = {0}", Minimum);
for (int i = 0; i < 2; i++)
{
for (int j = 0; j < 5; j++)
{
if (Maximum < Numbers[i, j])
Maximum = Numbers[i, j];
}
}
Console.WriteLine("Maximum = {0}", Maximum);
return 0;
}
}
}
|
|