-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy path_04_ArrayQuestions04.cs
73 lines (63 loc) · 1.72 KB
/
_04_ArrayQuestions04.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
using System;
using System.Net;
using OurCompany;
/*
* 04. Implement a routine to receive an integer array and return
* the min and max values as output parameters: FindMinAndMax(array, out min, out max)
*/
public class ArrayQuestion04
{
public static void Main(string[] args)
{
int[] array = { 7, 1, 9, 0, 35, 40, 8, 17, 40, 0, 8, 3, 7, 15 };
int minimum;
int maximum;
// Case 1
FindMinAndMax(array, out minimum, out maximum);
Console.WriteLine($"Min: {minimum}; Max: {maximum}");
Console.WriteLine($"Test result: {minimum == 0 && maximum == 40}");
// Case 2
FindMinAndMax2(array, out minimum, out maximum);
Console.WriteLine($"Min: {minimum}; Max: {maximum}");
Console.WriteLine($"Test result: {minimum == 0 && maximum == 40}");
// Case 3: Test FindMin
int min = FindMin(array);
Console.WriteLine($"Test result: {min == 0}");
// Case 4: Test FindMax
int max = FindMax(array);
Console.WriteLine($"Test result: {max == 40}");
}
public static void FindMinAndMax(int[] array, out int min, out int max)
{
min = array[0];
max = array[0];
for (int i = 1; i < array.Length; i++)
{
min = Math.Min(min, array[i]);
max = Math.Max(max, array[i]);
}
}
public static void FindMinAndMax2(int[] array, out int min, out int max)
{
min = FindMin(array);
max = FindMax(array);
}
public static int FindMin(int[] array)
{
int min = array[0];
for (int i = 1; i < array.Length; i++)
{
min = Math.Min(min, array[i]);
}
return min;
}
public static int FindMax(int[] array)
{
int max = array[0];
for (int i = 1; i < array.Length; i++)
{
max = Math.Max(max, array[i]);
}
return max;
}
}