Initialization
int AB[array_size]= {value_1, value_2};
Or
int AB[]= {value_1, value_2};
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <bits/stdc++.h>
Using namespace std;
int main() {
//initialization of arrays
int n = 2;
char example1[] = "hello";
int example2[n] = {
1,
2
};
//reading arrays value
cout << example1;
int i = 0;
while (i < n) {
cout << "\n" << example2[i];
i++;
}
return 0;
}
A matrix is an array of numbers for which mathematical operations can be defined. The horizontal and vertical lines of entries of a matrix are called rows and columns, respectively.
E.G. of a Matrix of A [row, column].
A [3, 3] = 1 4 5 8 49 95 9 14 84
1. Row-wise: this involves reading the values of the first row, then the second row, and so on until the last row of the matrix.
2. Column-wise: this involves reading the values of the first column, then second column, and so on until the last column of the matrix.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include<bits/stdc++.h>
using namespace std;
int main() {
int row_n = 2;
int n = 2;
//declaration and initialization
int matrix_example[2][2] = {
{
1,
2
},
{
3,
4
}
};
//read matrix row wise
for (int row = 0; row < row_n; row++) {
for (int i = 0; i < n; i++) {
cout << "row" << row + 1 << "column" << i + 1 << " -> " << matrix_example[row][i] << "\n";
}
}
//read matrix column wise
cout << "\n";
for (int i = 0; i < n; i++) {
for (int column = 0; column < row_n; column++) {
cout << "column" << column + 1 << "row" << i + 1 << " -> " <<
matrix_example[column][i] << "\n";
}
}
return 0;
}
Basics:
Identity matrix is a square matrix of any order (n). At the diagonal we have all the elements with a value of one and the rest are zeros.
Square matrix usually has the same number of rows and columns.
Inverse matrix is denoted A-1 , A * A-1 = I
Linear recurrence involves equations that relate a term in their sequence or a multidimensional array to previous terms in their sequence using recursion.
An example that involves the use of linear recurrence is the Fibonacci sequence.
Fibonacci sequence
It involves summing previous terms to obtain a new value for the n’th term. An example when n =2, where f(n) = f(n-1)+f(n-2). f(2) = f(2-1)+f(2-2) = 1
Can be solved in the following ways:
By using recursion
By using dynamic programming
By using matrix
A flaw when using recursion is that the memory size is limited for large values. When given 0 < n < 10⁹, finding f(n) mod 999983 using dynamic programming will fail when tackling this problem.
By applying a matrix we can solve the nth Fibonacci number in O (Log n) time.
Matrices can also be applied on graphs: these include counting paths, finding the shortest path between points, and implementing Kirchhoff’s theorem, among other applications.
The formulae used for calculation.
https://brilliant.org/wiki/linear-recurrence-relations/
https://en.wikipedia.org/wiki/Fibonacci_number#Matrix_form