0% found this document useful (0 votes)
4 views3 pages

C# Array Operations Guide

The document provides practical examples of declaring, assigning, and accessing elements in arrays using C#. It covers topics such as initializing arrays, finding their length, sorting arrays in ascending and descending order, copying arrays, and working with two-dimensional arrays. Additionally, it demonstrates how to sort each row of a two-dimensional array and retrieve its dimensions.

Uploaded by

Usman Khalid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

C# Array Operations Guide

The document provides practical examples of declaring, assigning, and accessing elements in arrays using C#. It covers topics such as initializing arrays, finding their length, sorting arrays in ascending and descending order, copying arrays, and working with two-dimensional arrays. Additionally, it demonstrates how to sort each row of a two-dimensional array and retrieve its dimensions.

Uploaded by

Usman Khalid
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Array Practical’s

Demonstrates how to declare, assign, and access array elements in Output:


C#.
Element[0] = 100
int[] n = new int[10]; // n is an array of 10 integers
// Initializing elements of array n Element[1] = 101
for (int i = 0; i < 10; i++) { Element[2] = 102
n[i] = i + 100; Element[3] = 103
} // Accessing and displaying array elements
Element[4] = 104
for (int j = 0; j < 10; j++) {
[Link]("Element[{0}] = {1}", j, n[j]); Element[5] = 105
} Element[6] = 106
Iterate through an array using the foreach loop. Element[7] = 107
int[] n = new int[10]; // n is an array of 10 integers
Element[8] = 108
// Initializing elements of array n
for (int i = 0; i < 10; i++) { Element[9] = 109
n[i] = i + 100;
} // Using foreach loop to access array elements Output:
int index = 0; Element[0] = 100
foreach (int value in n) {
Element[1] = 101
[Link]("Element[{0}] = {1}", index, value);
index++; Element[2] = 102
How to get the length of an array? Element[3] = 103
Output:
int[] numbers = { 10, 20, 30, 40, 50 }; Element[4] = 104
Array Length: 5
int length = [Link];
Element[5] = 105
[Link]("Array Length: " + length);
Demonstrates how to declare an array and find its length in C#. Element[6] = 106

int[] numbers = { 10, 20, 30, 40, 50 }; Element[7] = 107


// Finding length of the array
[Link]("The total number of Output:
elements in the array: " + [Link]); The total number of elements in the array: 5
Demonstrates how to sort an array in C#.
int[] numbers = { 50, 20, 40, 10, 30 };
// Sorting the array in ascending order
[Link](numbers); Output:
[Link]("Sorted Array:"); Sorted Array:
foreach (int num in numbers) 10 20 30 40 50
{
[Link](num + " ");
}
Sorting array in Descending Order.
Output:
int[] numbers = { 50, 20, 40, 10, 30 };
// Sorting the array in descending order Sorted Array in Descending Order:
[Link](numbers); 50 40 30 20 10
[Link](numbers);
[Link]("Sorted Array in Descending Order:");
foreach (int num in numbers) {
[Link](num + " ");
}
Demonstrates how to copy an array in C#. Output:
int[] sourceArray = { 1, 2, 3, 4, 5 }; Copied Array:
int[] destinationArray = new int[[Link]];
1 2 3 4 5
// Copying array using [Link]()
[Link](sourceArray, destinationArray, [Link]);
[Link]("Copied Array:");
foreach (int num in destinationArray) {
[Link](num + " ");
}
Clone() method creates a shallow copy of the array.
int[] sourceArray = { 10, 20, 30, 40, 50 }; Output:
// Cloning the array Cloned Array:
int[] clonedArray = (int[])[Link](); 10 20 30 40 50
[Link]("Cloned Array:");
foreach (int num in clonedArray) { Output:
[Link](num + " ");
}
a[0,0]: 0
Demonstrate the uses of the two-dimensional array. a[0,1]: 0
/* an array with 5 rows and 2 columns*/ a[1,0]: 1
int[,] a = new int[5, 2] {{0,0}, {1,2}, {2,4}, {3,6}, {4,8} }; a[1,1]: 2
int i, j;
/* output each array element's value */
a[2,0]: 2
for (i = 0; i < 5; i++) { a[2,1]: 4
for (j = 0; j < 2; j++) { a[3,0]: 3
[Link]("a[{0},{1}] = {2}", i, j, a[i,j]); a[3,1]: 6
}
a[4,0]: 4
}
Demonstrates how to find the total elements in a two- a[4,1]: 8
dimensional array in C#.
int[,] matrix = new int[3, 4]; Output:
[Link]("Total elements: " + [Link]); Total elements: 12
Demonstrates how to get the dimensions of a
multidimensional array.
// Declare and initialize a 3x4 matrix
Output:
int[,] matrix = {
{1, 2, 3, 4}, Number of Rows: 3
{5, 6, 7, 8}, Number of Columns: 4
{9, 10, 11, 12}
};
// Get dimensions
int rows = [Link](0);
int cols = [Link](1);
// Display the dimensions
[Link]("Number of Rows: " + rows);
[Link]("Number of Columns: " + cols);
Demonstrates how to sort each row of a two-dimensional array.
int[,] array = {
{3, 1, 4}, Output:
{9, 2, 8}, 1 3 4
{5, 7, 6} 2 8 9
};
5 6 7
// Sorting each row
for (int i = 0; i < [Link](0); i++) {
int[] row = new int[[Link](1)];
// Copy row to 1D array
for (int j = 0; j < [Link](1); j++) {
row[j] = array[i, j];
}
// Sort the row
[Link](row);
// Copy back sorted row to 2D array
for (int j = 0; j < [Link](1); j++) {
array[i, j] = row[j];
}
}
// Display sorted array
for (int i = 0; i < [Link](0); i++) {
for (int j = 0; j < [Link](1); j++) {
[Link](array[i, j] + " ");
}
[Link]();
}

You might also like