0% found this document useful (0 votes)
22 views13 pages

Arrays

The document provides an overview of arrays in programming, explaining their purpose to store multiple values in a single variable. It covers various methods for declaring and initializing arrays, accessing and modifying elements, and looping through arrays, including multidimensional arrays. Additionally, it demonstrates how to find the maximum, minimum, and sum of array elements using examples in C#.

Uploaded by

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

Arrays

The document provides an overview of arrays in programming, explaining their purpose to store multiple values in a single variable. It covers various methods for declaring and initializing arrays, accessing and modifying elements, and looping through arrays, including multidimensional arrays. Additionally, it demonstrates how to find the maximum, minimum, and sum of array elements using examples in C#.

Uploaded by

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

Arrays

Sumaiya Tanjil Khan


Lecturer
Department of CSE
Uttara University
❖ Arrays are used to store multiple values in a single variable, instead of declaring separate variables for each
value.

Method-1:
int[4] myNumbers;

myNumbers[0] = 25;

myNumbers[1] = 50;

myNumbers[2] = 75;

myNumbers[3] = 100;

Method-2:
int[] myNumbers = {25, 50, 75, 100};

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};


Access the Elements of an Array
❖ To access an array element, refer to its index number.
❖ Array indexes start with 0: [0] is the first element. [1] is the second element, etc.
❖ Example:

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Console.WriteLine(cars[0]);
Change an Array Element
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

cars[0] = "Opel";

Console.WriteLine(cars[0]);
Array Length
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

Console.WriteLine(cars.Length);
Declaration and Initialization of Array
// Create an array of four elements, and add values later

string[] cars = new string[4];

// Create an array of four elements and add values right away

string[] cars = new string[4] {"Volvo", "BMW", "Ford", "Mazda"};


Loop Through an Array

string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};

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

Console.WriteLine(cars[i]);

}
There is also a foreach loop, which is used exclusively to loop through elements in an array:
Syntax:
foreach (type variableName in arrayName)
{
// code block to be executed
}

Example:
string[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
foreach (string i in cars)
{
Console.WriteLine(i);
}
int[] myNumbers = {5, 1, 8, 9};

Array.Sort(myNumbers);

foreach (int i in myNumbers)

Console.WriteLine(i);

}
using System;
using System.Linq;
namespace MyApplication
{
class Program
{
static void Main(string[] args)
{
int[] myNumbers = {5, 1, 8, 9};
Console.WriteLine(myNumbers.Max()); // returns the largest value
Console.WriteLine(myNumbers.Min()); // returns the smallest value
Console.WriteLine(myNumbers.Sum()); // returns the sum of elements
}
Multidimensional Arrays
❖ A multidimensional array is basically an array of arrays.
❖ Arrays can have any number of dimensions.
❖ A 2D array is also known as a matrix (a table of rows and columns).

int[,] numbers = { {1, 4, 2}, {3, 6, 8} };

Console.WriteLine(numbers[0, 2]); // Outputs 2


Loop Through a 2D Array
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };
Output:
1
4
foreach (int i in numbers) 2
3
{
6
Console.WriteLine(i); 8

}
int[,] numbers = { {1, 4, 2}, {3, 6, 8} };

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

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

Console.WriteLine(numbers[i, j]);

You might also like