- Course
- Functions C
- Find the minimum and maximum in an array
Find the minimum and maximum in an array
Last updated:
8/23/2020
⁃
Difficulty:
Intermediate
Create a C# program that implements a function called FindMinMax that receives three parameters, an array of real numbers and two real numbers.
You must request 5 real numbers from the user to create the data array.
The function will calculate the minimum and maximum values ??of the array of real numbers. Send the parameters by reference.
Input
5
8
9
4
3
Output
3
9
Solution
using System;
public class FindMinMaxArray
{
public static void Main(string[] args)
{
int total = 5;
float[] data = new float[total];
for (int i = 0; i < total; i++)
{
data[i] = Convert.ToSingle(Console.ReadLine());
}
float min = 0.0f;
float max = 0.0f;
FindMinMax(ref data, ref min, ref max);
Console.WriteLine(min);
Console.WriteLine(max);
}
public static void FindMinMax(ref float[] data, ref float min, ref float max)
{
max = data[0];
min = data[0];
for (int i = 1; i < data.Length; i++)
{
if (data[i] > max)
{
max = data[i];
}
if (data[i] < min)
{
min = data[i];
}
}
}
}