Function Greatest Value In A Array - C# Programming Exercise

In this exercise of C#, you will learn how to create a function that returns the greatest value stored in an array of real numbers, which is specified as a parameter. This type of exercise is perfect for practicing how to work with arrays and how to compare elements within them in C#. The function will receive an array of float numbers and iterate through the array to find the maximum value. For example, when running the program with the data array containing the values {1.5f, 0.7f, 8.0f}, the function will return 8.0f as the maximum value. This exercise is excellent for enhancing your C# skills when working with arrays, functions, and comparisons.

With this exercise, you will learn how to handle arrays and create functions in C# that operate on them to perform tasks such as finding the maximum value in a set of data.

 Category

Functions

 Exercise

Function Greatest Value In A Array

 Objective

Write a C# function which returns the greatest value stored in an array of real numbers which is specified as parameter:

float[] data={1.5f, 0.7f, 8.0f}
float max = Maximum(data);

 Write Your C# Exercise

// Importing necessary namespaces
using System;

class Program
{
    // Main method to drive the program
    public static void Main()
    {
        // Example array of floating-point numbers
        float[] data = { 1.5f, 0.7f, 8.0f };

        // Calling the Maximum function to find the greatest value in the array
        float max = Maximum(data);

        // Display the result
        Console.WriteLine("The greatest value in the array is: " + max);
    }

    // Function to return the greatest value in an array of real numbers
    public static float Maximum(float[] arr)
    {
        // Initialize max to the smallest possible value
        float maxValue = arr[0];

        // Loop through the array to find the maximum value
        for (int i = 1; i < arr.Length; i++)
        {
            // Update max if a larger value is found
            if (arr[i] > maxValue)
            {
                maxValue = arr[i];
            }
        }

        // Return the largest value found in the array
        return maxValue;
    }
}

 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 factorial (iterative)

    In this exercise of C#, you will learn how to create an iterative (non-recursive) function to calculate the factorial of the number specified as a parameter. T...

  •  Function WriteTitle

    In this exercise of C#, you will learn how to create a function named "WriteTitle" that writes a text centered on the screen, in uppercase, with extra spaces, ...

  •  Function return value for Main

    In this exercise of C#, you will learn how to create a program in which you can use the WriteTitle function to write a title that the user will specify ...

  •  Function CountDV

    In this exercise of C#, you will learn how to create a function that calculates the amount of numeric digits and vowels a text string contains. The function...

  •  Function IsAlphabetic

    In this exercise of C#, you will learn how to create a function that tells if a character is alphabetic (from A to Z) or not. This function should be us...

  •  Function IsNumber

    In this exercise of C#, you will learn how to create a function that tells if a text string represents an integer number. This function should be used l...