2 DM Array_ new
2 DM Array_ new
Course 2
Multi-Dimensional Array
المصفوفات متعددة االبعاد
البمجة اساسيات ر
First Class
المرحلة االوىل
2024-2023
1
احمد صبيح.د Programming Fundamentals/ First Class
لتطبيق البرامج مباشر على شبكة االنترنت يمكن الدخول على الرابط التالي
https://www.programiz.com/cpp-programming/online-compiler/
بدل عبارة
#include <iostream>
ستكون العبارة
#include <iostream.h>
2
احمد صبيح.د Programming Fundamentals/ First Class
int x[3][4];
Elements in two-dimensional
array in C++ Programming
Three-dimensional arrays also work in a similar way. For example:
float x[2][4][3];
3
احمد صبيح.د Programming Fundamentals/ First Class
2 x 4 x 3 = 24
Like a normal array, we can initialize a multidimensional array in more than one way.
The above method is not preferred. A better way to initialize this array with the same
array elements is given below:
This array has 2 rows and 3 columns, which is why we have two rows of elements with
3 elements each.
4
احمد صبيح.د Programming Fundamentals/ First Class
Output
2 -5
4 0
9 1
In the above example, we have initialized a two-dimensional int array
named test that has 3 "rows" and 2 "columns".
Here, we have used the nested for loop to display the array elements.
the outer loop from i == 0 to i == 2 access the rows of the array
the inner loop from j == 0 to j == 1 access the columns of the array
Finally, we print the array elements in each iteration.
5
احمد صبيح.د Programming Fundamentals/ First Class
#include <iostream>
using namespace std;
int main() {
int numbers[2][3];
cout << "Enter 6 numbers: " << endl;
// Storing user input in the array
6
احمد صبيح.د Programming Fundamentals/ First Class
5
The numbers are:
numbers[0][0]: 5 numbers[0][1]: 1 numbers[0][2]: 4
numbers[1][0]: 4 numbers[1][1]: 8 numbers[1][2]: 5
Square Matrix
A square matrix is a matrix that has an equal number of rows and columns. In mathematics, n ×
n matrix is called the square matrix of order n.
Diameters in Square Matrix When row = column: int arr[3][3]
0 1 2
0 (0,0) (0,1) (0,2)
1 (1,0) (1,1) (1,2) Main Diagonal i=j
2 (2,0) (2,1) (2,2)
0 1 2
0 (0,0) (0,1) (0,2)
1 (1,0) (1,1) (1,2) Secondary Diagonal i + j = n-1
2 (2,0) (2,1) (2,2)
0 1 2
0 (0,0) (0,1) (0,2)
1 (1,0) (1,1) (1,2)
Upper Diagonal i<j
2 (2,0) (2,1) (2,2)
0 1 2
0 (0,0) (0,1) (0,2)
1 (1,0) (1,1) (1,2) Lower Diagonal i>j
2 (2,0) (2,1) (2,2)
7
احمد صبيح.د Programming Fundamentals/ First Class
8
احمد صبيح.د Programming Fundamentals/ First Class
H.W
Q1: Write a C++ program to perform the following operations on a square matrix 4*4:
1. Replace the main diagonal elements with the value 15.
2. Replace the secondary diagonal elements with the value 15.
3. Replace the upper diagonal elements with 16.
4. Replace the lower diagonal elements with 15.
Q2: Write a C++ program to perform the following operations on a square matrix 5*5:
1. Find the sum of the diagonal elements.
2. Find the sum of the secondary diagonal elements.
3. Find the sum of the upper diagonal elements.
4. Find the sum of the lower diagonal elements.
9
احمد صبيح.د Programming Fundamentals/ First Class
1- Scalar Matrix
A scalar matrix is a square matrix where all the off-diagonal elements (elements that are not on
the main diagonal) are zero, and the remaining diagonal elements are all equal to the same scalar
value (a single number).
Example:
[[a, 0, 0],
[0, a, 0],
[0, 0, a]]
In this example, a is the scalar value, and all other elements are zero.
2- Diagonal Matrix
A diagonal matrix is a square matrix where all the off-diagonal elements are zero, but the
diagonal elements can have different values.
Example:
[[1, 0, 0],
[0, 5, 0],
[0, 0, -2]]
Here, the diagonal elements are 1, 5, and -2, while all other elements are zero.
3- Identity Matrix
An identity matrix (denoted by I) is a special type of diagonal matrix where all the diagonal
elements are equal to 1.
Example:
[[1, 0, 0],
[0, 1, 0],
[0, 0, 1]]
The identity matrix plays a fundamental role in linear algebra operations, similar to the number 1
in arithmetic.
4- Symmetric Matrix
A square matrix is symmetric if its elements are mirrored across the main diagonal. In other
words, for any element a<sub>ij</sub> at row i and column j, the corresponding element
a<sub>ji</sub> at row j and column i must be equal.
Example:
[[2, 3, 1],
[3, 1, 4],
[1, 4, 5]]
10
احمد صبيح.د Programming Fundamentals/ First Class
In this example, notice that a<sub>12</sub> (3) is the same as a<sub>21</sub> (3),
a<sub>13</sub> (1) is the same as a<sub>31</sub> (1), and so on.
Key Points:
All identity matrices are diagonal matrices, but not all diagonal matrices are identity
matrices (an identity matrix has specific diagonal values of 1). Scalar [] = Identity []* a.
Symmetric matrices can be diagonal matrices, but not all diagonal matrices are
symmetric.
a diagonal matrix is a matrix where non-zero elements only occur on the main diagonal
11
احمد صبيح.د Programming Fundamentals/ First Class
H.W
Write C++ program to:
1-Display the matrix arr[3][3]= {{5,15,20},{9,13,30}, {8,16,20} }.
2-Convert it to Diagonal, Print the new Matrix. (All Diagonal = Different, Other Elements = 0)
3- Convert it to Identity, Print the new Matrix. (All Diagonal = 1, Other Elements = 0)
4- Convert it to Scalar, Print the new Matrix. (Diagonal * 3, Other Elements = 0)
5- Convert it to Symmetric, Print the new Matrix. (Upper Diagonal = Lower Diagonal)
6- Transpose Matrix, Print the new Matrix. (Row becomes Column)
12
احمد صبيح.د Programming Fundamentals/ First Class
#include <iostream>
using namespace std;
int main() {
int A[n][n], B[n][n], result[n][n];
int rowsA, colsA, rowsB, colsB;
return 0;
}
14
احمد صبيح.د Programming Fundamentals/ First Class
Multiplication of Matrix
To multiply two matrices 𝐴 and 𝐵, the number of columns in matrix 𝐴 must equal the number of
rows in matrix 𝐵.
If 𝐴 is of dimensions 𝑚×𝑛 and 𝐵 is of dimensions 𝑛×𝑝, the resulting matrix C will have
dimensions 𝑚×𝑝.
Matrix A [3][2]
|1 4|
|2 5|
|3 6|
Matrix B [2][3]
|7 8 9|
| 10 11 12 |
15
احمد صبيح.د Programming Fundamentals/ First Class
| 47 52 57 |
| 64 71 78 |
| 81 90 99 |
16
احمد صبيح.د Programming Fundamentals/ First Class
// Matrix B (2x3)
int B[2][3] = {
{7, 8, 9},
{10, 11, 12}
};
// Matrix multiplication
for(int i = 0; i < 3; i++)
{
for(int j = 0; j < 3; j++)
{
C[i][j] = 0; // Initialize C[i][j] to 0
for(int k = 0; k < 2; k++)
{
C[i][j] += A[i][k] * B[k][j];
// K is The common side between A and B (to connect them)
}
}
}
return 0; }
17
احمد صبيح.د Programming Fundamentals/ First Class
طريقة اخرى
int main() {
int A[n][n], B[n][n], result[n][n];
int rowsA, colsA, rowsB, colsB;
return 0;
}
19