Multidimensional Arrays: Ali Shakir Alahmed

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 21

Duhok Polytechnic University

Shekhan Technical Institute


Information Technology Department

Multidimensional Arrays

Ali Shakir Alahmed

2022–2023
What is Multidimensional Array?
• Multidimensional arrays have more than one
dimension (2, 3, …)
– The most important multidimensional arrays are the
2-dimensional
• Known as matrices
• Example of matrix of integers with 2 rows and 4
columns:
0 1 2 3

0 5 0 -2 4
1 5 6 7 8
Multi Dimensional Arrays Definition

Arrays can have more than one dimension. For example, a two-dimensional
array of four rows and two columns. It might have two, three, etc.
dimensions.

Duhok Polytechnic University 3


Multi Dimensional Arrays Definition

Int [,] A=new int [3,4]


15 14 13 12
[0,0] [0,1] [0,2] [0,3]
10 3 5 6
[1,0] [1,1] [1,2] [1,3]
21 31 2 4 [2,0] [2,1] [2,2] [3,3]
static void Main(string[] args)
{
int[,] A ={

{13,14,15,16},
{20,21,22,23},
{31,32,33,34}

};

Console.WriteLine(A[0, 2]); 15

}}
Two-Dimensional Array: Declaration Example

An example for declaration of two-dimensional array, which can use


initial assignment values, such as:

double[ , ] hillHeight = { { 1, 2, 3, 4 }, { 2, 3, 4, 5 }, { 3, 4, 5, 6 } };

4 Columns
Row 0

3 Rows
Row 1

Row 2

Column 1 Column 2 Column 3 Column 4

Duhok Polytechnic University 6


Two-Dimensional Array: Declaration Example

Another example for declaration of two-dimensional array, is


shown as below:
int[,] intArray = new int[3,2] { { 1, 1}, { 1, 2}, { 1, 3} };

Duhok Polytechnic University 7


Two-Dimensional Array Initialization
We initialize two-dimensional arrays in the same way as
we initialize one-dimensional arrays. We can list the
element values straight after the declaration:
int[,] matrix =
{
{1, 2, 3, 4}, // row 0 values
{5, 6, 7, 8}, // row 1 values
};
// The matrix size is 2 x 4 (2 rows, 4 cols)
Accessing the Elements of a Multidimensional Array
Matrices have two dimensions and respectively we access each
element by using two indices: one for the rows and one for the
columns
Let’s examine the next example:
int[,] matrix ={ {1, 2, 3, 4},
{5, 6, 7, 8},};
The array matrix has 8 elements, stored in 2 rows and 4
columns. Each element can be accessed in the following way:
matrix[0, 0] matrix[0, 1] matrix[0, 2] matrix[0, 3]
matrix[1, 0] matrix[1, 1] matrix[1, 2] matrix[1, 3]
Length of Multidimensional Arrays
Each dimension of a multidimensional array has its own length,
which can be accessed during the execution of the program. Let’s
look at an example for a two-dimensional array:
int[,] matrix = {{1, 2, 3, 4},{5, 6, 7, 8}, };
We can get the number of the rows of this two-dimensional array
by using matrix.GetLength(0) and the number of all columns
per row with matrix.GetLength(1).
So, in this case matrix.GetLength(0) returns 2 and
matrix.GetLength(1) returns 4.
Reading and
Printing
Matrices
Printing Matrices – Example
In the next example we will demonstrate how we can print two-dimensional arrays to
the console:
// Declare and initialize a matrix of size 2 x 4
static void Main(string[] args)
{
int[,] matrix ={{1, 2, 3, 4}, // row 0 values
{5, 6, 7, 8},}; // row 1 value}
// Print the matrix on the console
for (int row = 0; row < matrix.GetLength(0); row++)
{
for (int col = 0; col < matrix.GetLength(1); col++)
{
Console.Write(matrix[row, col]);
} 1234
Console.WriteLine();
5678
}}}
Two-Dimensional Array: GetLength Example

using System;

class Program
{
public static void Main()
{
int[,] A = new int[,]{{10, 20, 30},{ 40, 50, 60} };

for(int r = 0; r < A.GetLength(0); r++) Console Window


{ 10 20 30
for(int c = 0; c < A.GetLength(1); c++) 40 50 60
Console.Write(A[r, c]+ “ “);
Console.WriteLine();
}
}
}

Duhok Polytechnic University 13


Two-Dimensional Array: Sum and Average

using System;
class Pogram
{ Console Window
static void Main() 123456
{ int[,] A={{1, 2, 3},{4, 5, 6}};
Total: 21
int total = 0;
for (int r = 0; r < A.GetLength(0); r++) Average: 3.5
{ for (int c = 0; c < A.GetLength(1); c++)
{ Console.Write(A[r, c]+” “);
total + = A[r, c];
}
}
Console.WriteLine(“\nTotal: {0}”, total);
Console.WriteLine(“Average: {0}”, (total/6.0));
}
}

Duhok Polytechnic University 14


Reading Matrix – Example

• Create a matrix from the console


int rows = int.Parse(Console.ReadLine());
int columns = int.Parse(Console.ReadLine());
int[,] matrix = new int[rows, columns];
for (int row=0; row<rows; row++)
{
for (int column=0; column< columns; column++)
{
Console.Write("matrix[{0},{1}] = ", row,
column);
matrix[row, column] =
int.Parse(Console.ReadLine()); }
}
Reading And Printing a matrix on the console:
In this example how to read a two-dimensional array from the console.
First, we read the values (lengths) of the two-dimensions and then by
using two nested loops we assign the value of each element (and in the
end we print out the values of the array):
using System;
namespace ConsoleApplication6
{ class Program
{ static void Main(string[] args)
{
Console.Write("Enter the number of the rows: ");
int rows = int.Parse(Console.ReadLine());
Console.Write("Enter the number of the columns:
");
int cols = int.Parse(Console.ReadLine());
int[,] matrix = new int[rows, cols];
Console.WriteLine("Enter the cells of the matrix:");
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < cols; col++)
{
Console.Write("matrix[{0},{1}] = ", row, col);
matrix[row, col] =
int.Parse(Console.ReadLine());
}
}
for (int row = 0; row < matrix.GetLength(0);
row++)
{
for (int col = 0; col < matrix.GetLength(1);
col++)
{
Console.Write(" " + matrix[row, col]);
}
Console.WriteLine();
}
Find sum of Main diagonal of a matrix
public static void Main()
{
int i,j,sum=0;
int[,] arr1 = new int[5,5];
Console.Write("Input elements in the first matrix :\n");
for(i=0;i<5;i++)
{
for(j=0;j<5;j++)
{ Console.Write("element - [{0}],[{1}] : ",i,j);
arr1[i,j]=int.Parse(Console.ReadLine()); } }

for(i=0;i<n;i++)
{ sum= sum+arr1[i,i]; }
Console.Write("Addition of the main Diagonal elements is :{0}\
n",sum);
Find sum of Second diagonal of a matrix
public static void Main()
{ int i,j,sum=0,n,m=0;
int[,] arr1 = new int[5,5];
Console.Write("Input elements in the matrix:\n");
for(i=0; i<5;i++)
{ for(j=0;j<5;j++)
{
arr1[i,j]= int.Parse (Console.ReadLine());
}
}
// calculate the sum of second diagonals
for(i=0;i<5;i++)
{ for(j=0;j<5;j++)
{
if (i+j == 4)
{ sum= sum+arr1[i,j]; }
}
}
Console.Write("Addition of the second Diagonal elements is :
{0}\n",sum);
}
}
HOME WORK

1-Find the sum of rows an columns of a


Matrix?
2-Print elements of rows an columns of a
Matrix?

You might also like