Function Minmaxarray - C# Programming Exercise

In this exercise of C#, you will create a function named MinMaxArray that receives an array of numbers and returns the minimum and maximum values using reference parameters. The function should take a float array and return the minimum and maximum values using variables passed by reference. For example, if you have the array float[] data = {1.5f, 0.7f, 8.0f}, the function MinMaxArray should return 0.7 as the minimum value and 8.0 as the maximum value. This exercise is useful for understanding how to manipulate arrays and use reference parameters in C#. Additionally, it allows you to practice handling data in collections and manipulating values in the context of functions. It’s a great opportunity to enhance your skills with functions and reference parameters in C#.

This exercise will help you understand how to optimize the use of arrays and values returned through reference parameters, improving the efficiency and flexibility of your C# programs.

 Category

Functions

 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

// 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
    }
}

 Share this C# exercise

 More C# Programming Exercises of Functions

Explore our set of C# programming exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C#. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C#.

  •  Function Reverse, recursive

    In this exercise of C#, you need to create a program that uses recursion to reverse a string of characters. The program should receive a string as input...

  •  Function WriteRectangle

    In this exercise of C#, you need to create two functions: one called WriteRectangle to display a filled rectangle on the screen using asterisks, and another ca...

  •  Function Palindrome, iterative

    In this exercise in C#, you need to create an iterative function that checks if a string is symmetric (a palindrome). A palindrome is a word, number, phrase, or other...

  •  Function Palindrome, recursive

    In this exercise in C#, you will need to create a recursive function to check if a string is symmetric (a palindrome). A palindrome is a word, number, phrase, ...

  •  Function GetMinMax

    In this exercise in C#, you will need to write a function named "GetMinMax", which will ask the user to enter a minimum value (a number) and a maximum value (a...

  •  Function Multiply & MultiplyR

    In this exercise in C#, you will need to write two functions called "Multiply" and "MultiplyR" to calculate the product of two numbers using sums. The first ve...