C# Passing array to function

We can pass array to function by using its name only. The formal parameter in function declaration must be of array type. Function accepts arrays to implement any logic on them which can be reused. Let’s see an example of passing array to the function.

using System;  
public class ArrayExample  
{  
static void printArray(int[] arr)  
{  
Console.WriteLine("Printing array elements:");  
for (int i = 0; i < arr.Length; i++)  
{  
Console.WriteLine(arr[i]);  
}  
}  
public static void Main(string[] args)  
{  
int[] arr1 = { 25, 10, 20, 15, 40, 50 };  
int[] arr2 = { 12, 23, 44, 11, 54 };  
printArray(arr1);
            printArray(arr2);  
}  
}
Output
Printing array elements:
25
10
20
15
40
50
Printing array elements:
12
23
44
11
54
Passing Array to function: finding second largest element of array
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[10];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        int m=secondLargest(arr);
                        Console.WriteLine("Second largest element in the array is "+m);
            }
            static int secondLargest(int[] arr)
            {
                        int largest=arr[0];
                        int second_largest=arr[0];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    if(arr[i]>largest)
                                    {
                                                largest=arr[i];
                                    }
                        }
                        for(int i=1;i<arr.Length;i++)
                        {
                                    if(arr[i]>second_largest && arr[i]<largest)
                                                second_largest=arr[i];
                        }                                          
                        return second_largest;
            }
}
Output
10
20
30
40
50
60
70
80
80
90
Second largest element in the array is 80  
Passing Array to function Example: sorting the array using bubble sort
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[10];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        sort(arr);
            }
            static void sort(int[] arr)
            {
                        int temp;
                        Console.WriteLine("The Sorted array is... ");
                        for(int i=0;i<arr.Length;i++)
                        {
                                    for(int j=i+1;j<arr.Length;j++)
                                    {
                                                if(arr[j]<arr[i])
                                                {
                                                            temp=arr[i];
                                                            arr[i]=arr[j];
                                                            arr[j]=temp;
                                                }
                                    }
                        }
                        for(int i=0;i<arr.Length;i++)
                        {
                                    Console.WriteLine(arr[i]);
                        }
            }
}
Output
10
12
3
2
6
1
7
3
2
1
The Sorted array is... 
1
1
2
2
3
3
6
7
10
12
Passing Array to function: printing sum and average of all elements
using System;  
public class ArrayExample  
{  
public static void Main(string[] args)
            {
                        int[] arr=new int[5];
                        for(int i=0;i<arr.Length;i++)
                        {
                                    arr[i]=Convert.ToInt32(Console.ReadLine());
                        }
                        sum(arr);
            }
            static void sum(int[] arr)
            {
                        int sum=0;
                        float avg=0;
                        for(int i=0;i<arr.Length;i++)
                        {
                                    sum=sum+arr[i];
                        }
                        avg=sum/5;
                        Console.WriteLine("sum of the array is "+sum+" Average of the array is "+avg);
            }
}
Output
1
2
3
4
5
sum of the array is 15 Average of the array is 3



ADVERTISEMENT
ADVERTISEMENT