Lab Manual: Programs using Array Class
in C#
Experiment 1: Sort an Array
Aim: To sort the elements of an array using [Link]() method.
Algorithm:
1. Declare and initialize an array.
2. Display the original array.
3. Use [Link]() to sort the array in ascending order.
4. Display the sorted array.
Program:
using System;
class Program
{
static void Main()
{
int[] arr = { 50, 10, 30, 20, 40 };
[Link]("Original Array:");
foreach (int num in arr)
[Link](num + " ");
[Link](arr);
[Link]("\nSorted Array:");
foreach (int num in arr)
[Link](num + " ");
}
}
Output:
Original Array:
50 10 30 20 40
Sorted Array:
10 20 30 40 50
Experiment 2: Reverse an Array
Aim: To reverse the elements of an array using [Link]() method.
Algorithm:
5. Declare and initialize an array.
6. Display the original array.
7. Use [Link]() to reverse the array.
8. Display the reversed array.
Program:
using System;
class Program
{
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
[Link]("Original Array:");
foreach (int num in arr)
[Link](num + " ");
[Link](arr);
[Link]("\nReversed Array:");
foreach (int num in arr)
[Link](num + " ");
}
}
Output:
Original Array:
12345
Reversed Array:
54321
Experiment 3: Copy Elements from One Array to Another
Aim: To copy elements from one array to another using [Link]().
Algorithm:
9. Declare and initialize a source array.
10. Create an empty destination array of the same size.
11. Use [Link]() to copy the elements.
12. Display the destination array.
Program:
using System;
class Program
{
static void Main()
{
int[] source = { 10, 20, 30, 40, 50 };
int[] destination = new int[[Link]];
[Link](source, destination, [Link]);
[Link]("Copied Array:");
foreach (int num in destination)
[Link](num + " ");
}
}
Output:
Copied Array:
10 20 30 40 50
Experiment 4: Find Index of an Element
Aim: To find the index of an element in an array using [Link]().
Algorithm:
13. Declare and initialize an array.
14. Use [Link]() to search for an element.
15. Display the index.
Program:
using System;
class Program
{
static void Main()
{
string[] fruits = { "Apple", "Banana", "Mango", "Orange" };
int index = [Link](fruits, "Mango");
[Link]("Index of Mango: " + index);
}
}
Output:
Index of Mango: 2
Experiment 5: Clear Elements from Array
Aim: To clear specific elements of an array using [Link]().
Algorithm:
16. Declare and initialize an array.
17. Use [Link]() to set specific elements to zero (default value).
18. Display the modified array.
Program:
using System;
class Program
{
static void Main()
{
int[] arr = { 1, 2, 3, 4, 5 };
[Link]("Before Clear:");
foreach (int num in arr)
[Link](num + " ");
[Link](arr, 1, 3); // Clear elements from index 1 to 3
[Link]("\nAfter Clear:");
foreach (int num in arr)
[Link](num + " ");
}
}
Output:
Before Clear:
12345
After Clear:
10005
Experiment 6: Resize an Array
Aim: To resize an array using [Link]().
Algorithm:
19. Declare and initialize an array.
20. Display the original array.
21. Use [Link]() to change the size of the array.
22. Assign new values and display the updated array.
Program:
using System;
class Program
{
static void Main()
{
int[] arr = { 10, 20, 30 };
[Link]("Original Array:");
foreach (int num in arr)
[Link](num + " ");
[Link](ref arr, 5);
arr[3] = 40;
arr[4] = 50;
[Link]("\nResized Array:");
foreach (int num in arr)
[Link](num + " ");
}
}
Output:
Original Array:
10 20 30
Resized Array:
10 20 30 40 50