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.
Structure:
public void YourFunctionName(int[] arrayName)
{
// Your function code here
}
Scenario 1: Write a C# program, create a method named DisplayCarParts that takes an array of strings as a parameter and prints each element of the array in the console.
The Main method should create a string array as named carparts and call the DisplayCarParts method with this array.
Print a Simple Array using Array to function
using System;
class Program
{
static void DisplayParts(string[] listofParts)
{
Console.WriteLine(“Car parts:”);
foreach (string part in listofParts)
{
Console.WriteLine(part);
}
}
static void Main()
{
string[] carpartlist = { “Engine” ,”Sensor” ,”starter” };
DisplayParts(carpartlist);
}
}
Output:
Car parts:
Engine
Sensor
Starter
Explanation:
using System;
class Program
{
static void DisplayParts(string[] listofParts)
Defines a static method that takes a string array as a parameter
Console.WriteLine(“Car parts:”);
Prints “Car parts:” to the console
foreach (string part in listofParts)
Iterates through each element in the carparts array. For more details please refer C# For Loop section in this Url : https://iqratechnology.com/academy/c-sharp-training/c-for-loop/
Console.WriteLine(part);
Prints each car part to the console
static void Main()
Defines the Main method, the entry point of the application
string[] carpartlist = { “Engine”, “Sensor”, “starter” };
Create and initialize an array of string with varibale carpartlist
DisplayParts(carpartlist);
Calls the DisplayParts method and passes the carpartlist array as an argument to it
Output:
Car parts:
Engine
Sensor
Starter
Scenario 2: Write a C# program that calculates and displays the addition of any three numbers using a method named Sum.
The Sum method should take any 3 numbers and return their sum.
using System;
class Program
{
static int Sum( int[] Value)
{
int sum = 0;
foreach (int number in Value)
{
sum += number;
}
return sum;
}
static void Main()
{
int[] Value = {2,4,8 };
int ValueofSum = Sum(Value);
Console.WriteLine(ValueofSum);
}
}
Output: 14
Explanation:
using System;
Includes the System namespace, which contains fundamental classes like Console
class Program
Defines a class named Program
static int Sum(int[] Value)
Method that calculates the sum of elements in an integer array
int sum = 0;
Define a varibale named sum and set value 0 to it.
foreach (int number in Value)
Iterates through each element in the Value array. For more details please refer C# For Loop section in this Url : https://iqratechnology.com/academy/c-sharp-training/c-for-loop/
sum += number;
Adds each element number to sum
return sum;
Returns the calculated sum
static void Main()
The entry point of the program
int[] Value = { 2,4,8};
Define an integer array as named Value with elements 2, 4, and 8
int ValueofSum = Sum(Value);
Calls the Sum method with Value as the argument and stores the result in ValueofSum
Console.WriteLine(ValueofSum);
Prints the result (sum of array elements) to the console
Course Video
Task:
1. Sum of Elements:
Create a function SumArray that takes an array of integers and returns the sum of its elements.
Test the function by passing an array of integers and printing the result.
2. Average of Elements:
Create a function AverageArray that takes an array of doubles and returns the average of its elements.
Test the function by passing an array of doubles and printing the result.
3.Find Maximum:
Create a function FindMax that takes an array of integers and returns the maximum value.
Test the function by passing an array of integers and printing the result.
4. Count Occurrences:
Create a function CountOccurrences that takes an array of integers and a target integer, and returns the number of times the target integer occurs in the array.
Test the function by passing an array of integers and a target integer, and printing the result.
5. Filter Even Numbers:
Create a function FilterEvenNumbers that takes an array of integers and returns a new array containing only the even numbers from the original array.
Test the function by passing an array of integers and printing the result.
6. Calculate Median:
Create a function CalculateMedian that takes an array of integers and returns the median value.
Test the function by passing an array of integers and printing the result.