0% found this document useful (0 votes)
2 views4 pages

Arrays in C#

The document provides a C# program that demonstrates the use of single-dimensional and multidimensional arrays. It includes details about the program structure, variables, data types, and the flow of execution for both types of arrays. The output of the program displays the elements of the single-dimensional array and the elements of the multidimensional array in a formatted manner.

Uploaded by

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

Arrays in C#

The document provides a C# program that demonstrates the use of single-dimensional and multidimensional arrays. It includes details about the program structure, variables, data types, and the flow of execution for both types of arrays. The output of the program displays the elements of the single-dimensional array and the elements of the multidimensional array in a formatted manner.

Uploaded by

ramesh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Single dimension Array

using System;

class Program

static void Main(string[] args)

// Define an array of integers

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

// Display the elements of the array

Console.WriteLine("Elements of the array:");

for (int i = 0; i < numbers.Length; i++)

Console.WriteLine($"Element {i + 1}: {numbers[i]}");

# Program Structure

The program consists of a single class Program with a Main method, which is the entry point of the
program.

# Variables and Data Types

- numbers: an integer array (int[]) that stores the elements { 10, 20, 30, 40, 50 }.

- i: an integer variable (int) used as the loop counter.

- args: a string array (string[]) that represents the command-line arguments (not used in this program).
# Program Flow

1. Array Definition: The program defines an integer array numbers with five elements: { 10, 20, 30, 40,
50 }.

2. Display Array Elements: The program uses a for loop to iterate over the elements of the numbers
array.

3. Loop Counter: The loop counter i is initialized to 0 and incremented by 1 in each iteration.

4. Array Indexing: The program uses the loop counter i to access each element of the numbers array
using the syntax numbers[i].

5. Display Element: In each iteration, the program displays the current element using the syntax
Console.WriteLine($"Element {i + 1}: {numbers[i]}").

6. Loop Termination: The loop terminates when the loop counter i reaches the length of the numbers
array (numbers.Length).

Multidimensional Array

using System;

class Program

static void Main(string[] args)

// Single-dimensional array

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

Console.WriteLine("Single-dimensional array:");

foreach (int num in singleArray)

Console.Write(num + " ");

Console.WriteLine();

// Multidimensional array (2D)

int[,] multiArray = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };

Console.WriteLine("\nMultidimensional array (2D):");


for (int i = 0; i < multiArray.GetLength(0); i++)

for (int j = 0; j < multiArray.GetLength(1); j++)

Console.Write(multiArray[i, j] + " ");

Console.WriteLine();

C# program demonstrates the use of single-dimensional and multidimensional arrays.

# Program Structure

The program consists of a single class Program with a Main method, which is the entry point of the
program.

# Variables and Data Types

- singleArray: a single-dimensional integer array (int[]) that stores the elements { 1, 2, 3, 4, 5 }.

- multiArray: a multidimensional integer array (int[,]) that stores the elements { { 1, 2, 3 }, { 4, 5, 6 }, { 7,


8, 9 } }.

- i and j: integer variables (int) used as loop counters.

# Program Flow

Single-Dimensional Array

1. Array Definition: The program defines a single-dimensional integer array singleArray with five
elements: { 1, 2, 3, 4, 5 }.

2. Display Array Elements: The program uses a foreach loop to iterate over the elements of the
singleArray.

3. Display Element: In each iteration, the program displays the current element using
Console.Write(num + " ").
Multidimensional Array (2D)

1. Array Definition: The program defines a multidimensional integer array multiArray with three rows
and three columns: { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } }.

2. Get Length of Array: The program uses the GetLength method to get the number of rows and columns
in the multiArray.

3. Nested Loops: The program uses two nested for loops to iterate over the elements of the multiArray.

4. Display Element: In each iteration, the program displays the current element using
Console.Write(multiArray[i, j] + " ").

# Output

The program displays the following output:

Single-dimensional array:

12345

Multidimensional array (2D):

123

456

789

You might also like