C# Array to Function

HTML
CSS
C#
SQL

Array to Function

In C#, “Array to function” refers to passing an array as an argument to a function. This allows the function to operate on the elements of the array. When you pass an array to a function, you are essentially giving that function access to the elements of the array, enabling it to perform operations like reading, modifying, or analysing those elements.

Print a Simple Array using Array to function

using System;

class Program

{

    static void Main()

    {

        // Define an array of integers

        int[] numbers = { 1, 2, 3, 4, 5 };

        // Call the function and pass the array as an argument

        PrintArray(numbers);

    }

    // Define a function that accepts an array of integers as a parameter

    static void PrintArray(int[] arr)

    {

        Console.WriteLine(“Array elements:”);

        foreach (int num in arr)

        {

            Console.Write(num);

        }

    }

}

Output: Array elements: 1 2 3 4 5

Find the Sum of the number in the Array using Array to function

using System;

class Program

{

    static void Main()

    {

        // Define an array of integers

        int[] numbers = { 1, 2, 3, 4, 5 };

        // Call the function to calculate the sum and store the result

        int sum = CalculateSum(numbers);

        // Print the sum

        Console.WriteLine($”The sum of the numbers in the array is: {sum}”);

    }

    // Define a function to calculate the sum of the numbers in the array

    static int CalculateSum(int[] arr)

    {

        int sum = 0;

        foreach (int num in arr)

        {

            sum += num;

        }

        return sum;

    }

}

Output: The sum of the numbers in the array is: 15

Find the Maximum number in the Array using Array to function

using System;

class Program

{

    static void Main()

    {

        // Define an array of integers

        int[] numbers = { 10, 5, 20, 15, 8 };

        // Call the function to find the maximum number and store the result

        int maxNumber = FindMax(numbers);

        // Print the maximum number

        Console.WriteLine($”The maximum number in the array is: {maxNumber}”);

    }

    // Define a function to find the maximum number in the array

    static int FindMax(int[] arr)

    {

        // Initialize a variable to store the maximum number

        int max = arr[0];

        // Iterate over each element of the array

        foreach (int num in arr)

        {

            // If the current element is greater than the current maximum, update the maximum

            if (num > max)

            {

                max = num;

            }

        }

        // Return the maximum number found

        return max;

    }

}

Output: The maximum number in the array is: 20

Practices Tasks

1.You have an array of temperatures recorded throughout the day and you have to find maximum temperature

We declare a Main method, which serves as the entry point of our program. We create an integer array temperature and initialize it with a set of temperature values: { 42, 45, 60, 80, 82 }.We create a separate function with name Findmax and use return in that to get a value in main method. We call the FindMax function and pass the temperatures array as an argument. The return value (maximum temperature) is stored in the max variable. We use Console.WriteLine to display the maximum temperature retrieved from the FindMax function, using string interpolation to format the output. We use Console.ReadLine() to pause the console and wait for user input before closing the program. Your output should like below.

Output 

2. You have to sort your family names using Array to function concept

We declare the Main method as the entry point of our program. We initialize a string array familyNames containing a list of family names “John”, “Alice”, “Emily”, “David”, “Sophia”.We create separate function name as SortNames call the SortNames function in main method and pass the familyNames array as an argument to sort the names. We use Console.WriteLine to display a message “Sorted Names:” to indicate that the names have been sorted. We iterate through the familyNames array using a foreach loop and display each name to the console using Console.WriteLine.We use Console.ReadLine() to pause the console and wait for user input before closing the program. Your output should like below.

Output 

3. We define a 2D integer array named studentGrades.This array has 3 rows (students) and 3 columns (subjects: Math, Science, English).Each row represents a student, and each column represents a subject with corresponding grades. We start by printing a header message using Console.WriteLine to indicate the list of subjects. Next, we use a nested for loop to iterate over the 2D array studentGrades.The outer loop (for (int student = 0; student < studentGrades.GetLength(0); student++)) iterates over each row (student) of the array. The inner loop (for (int subject = 0; subject < studentGrades.GetLength(1); subject++)) iterates over each column (subject) of the array for the current student. Inside the loops, we use Console.Write to display the grades (studentGrades[student, subject]) followed by a space. After displaying all grades for a student, we use Console.WriteLine() to move to the next line for the next student. By using array to function  your Output should like below.

Output 

4. Defined a 2D array of student grades, representing the grades of three students in three subjects. The rank method is called student grades as an argument to rank the students based on their grades. The rank method: Prints a header message indicating the list of subjects. Calculates the sum of marks for each subject and displays the ranking of students.

Output