Exercise
Function Minmaxarray
Objective
Write a C# function named MinMaxArray, to return the minimum and maximum values stored in an array, using reference parameters:
float[] data={1.5f, 0.7f, 8.0f}
MinMaxArray(data, ref minimum, ref maximum);
(after that call, minimum would contain 0.7, and maximum would contain 8.0)
Write Your C# Exercise
C# Exercise Example
// Import the System namespace to use basic classes like Console
using System;
class Program
{
// Function to find the minimum and maximum values in an array
public static void MinMaxArray(float[] data, ref float minimum, ref float maximum)
{
// Initialize minimum and maximum to the first element in the array
minimum = data[0];
maximum = data[0];
// Loop through the array to find the min and max values
foreach (float value in data)
{
// Check if the current value is less than the current minimum
if (value < minimum)
{
minimum = value; // Update the minimum value
}
// Check if the current value is greater than the current maximum
if (value > maximum)
{
maximum = value; // Update the maximum value
}
}
}
// Main method to test the MinMaxArray function
public static void Main()
{
// Declare and initialize an array of float values
float[] data = { 1.5f, 0.7f, 8.0f };
// Declare variables to store the minimum and maximum values
float minimum, maximum;
// Call the MinMaxArray function to find the minimum and maximum values
MinMaxArray(data, ref minimum, ref maximum);
// Print the minimum and maximum values to the console
Console.WriteLine($"Minimum: {minimum}"); // Expected: 0.7
Console.WriteLine($"Maximum: {maximum}"); // Expected: 8.0
}
}