Starting from:
$29

$26.10

Homework 5. Matrices

5. Matrices

5.01. Write a program that reads a matrix of integers from
the console, then a number, and prints all the positions at
which that number appears in the matrix.
The matrix definition on the console will contain a line with
two positive integer numbers R and C – the number of rows and
columns in the matrix – followed by R lines, each containing
C numbers (separated by spaces), representing each row of the
matrix.
The number you will need to find the positions of will be
entered on a single line, after the matrix.
You should print each position on a single line – first print
the row, then the column at which the number appears.
If the number does not appear in the matrix, print not found.
Use vectors.
5.02. Write a program that reads two integer matrices (2D
arrays) from the console and compares them element by
element. For better code reusability, you could do the
comparison in a function, which returns true if they are
equal and false if not.
Each matrix definition on the console will contain a line
with a positive integer number R – the number of rows in the
matrix – followed by R lines containing the numbers in the
matrix, separated by spaces.
The matrices will have at most 10 rows and at most 10
columns.
Print equal if the matrices match, and not equal if they
don’t match.
Example input Expected output
2 3
1 2 3
4 5 6
3
0 2
2 3
1 2 3
4 2 2
2
0 1
1 1
1 2
2 3
1 -2 -3
4 -5 6
5
not found
5.03. We say that in the elements of the matrix there is
a local minimum ( ) for , if the elements that stay on it's
left, right, up and down position are bigger than it. Write a
program that finds and outputs all the elements of the matrix
for which has a local minimum.
5.04. Write a program which checks and prints the numbers of
those rows of a matrix containing at least one negative
element.
5.05. Write a program which:
a) inputs the elements of a square real matrix with size
by rows ;
b) from the matrix constructs a vector ,
where and the first elements of coincide with the
first column of , the second elements of with the second
Example input Expected output
1
1 2 3
1
1 2 3
equal
2
1 2 3
2 1 3
2
1 2 3
2 1 3
equal
2
11
21
2
11
21
equal
2
1 2
4 5 6
2
1 2
4 5 6 7
not equal
2
1 2 3
4 5 6
2
1 2
3 4
not equal
AnXm
aij A
A A
A
NxN
A B ⃗= (b1, b2, . . . , bm)
m = N2 N B ⃗
A N B
column of and etc., the last elements of coincide with
the last column of ;
c) sorts in ascending order the elements of ;
d) constructs a new square matrix with size , where the
elements from the first row coincide with the first
elements of , the elements from the second row coincide with
the second elements of < , etc., the elements from the
last row coincide with the last elements of ;
e) prints matrix by rows.

5.06. Let A be a n-th order square matrix. Write a program
which finds:

a) the sum of the elements over the main diagonal;

b) the product of the elements under the main diagonal;

c) the sum of the positive elements from the main diagonal;

d) the product of the elements from the secondary diagonal;

e) the sum of the elements outside the secondary diagonal.

5.07. Write a program which erases the k-th column of the
matrix . Deleting means moving the columns with one to the
left and reducing the number of columns by one.
5.08. Write a program which finds the sum of the squares of
those elements in a real matrix of n-th order, which are
special. A special element is element for which the following
inequality is true:
.
5.09. It is given the set of pairs:
, where and , are integer
numbers. Write a program which checks whether the set M
defines a function. Hint: The set M defines a function
whenever for every is satisfied .
5.10. Let be three matrices and be two vectors.
Write a program which finds:
A N B
A
B ⃗
C NxN
N
B ⃗
N B ⃗
N B ⃗
C
AnXm
a[i, j] ≥
a[i − 1,j] + a[i, j − 1] + a[i, j + 1] + a[i + 1,j]
4
M
M = {(x1, y1), (x2, y2), . . . , (xn, yn)} xi yi i = 1,...,n
xi = xj yi = yj
Y = A, B,C X, Y
a) ;
b) ;
c) ;
d) , where is the transposed matrix of .
5.11. Write a program which finds the maximum number, which
occurs int a given n-th order matrix more than once.
5.12. We say that two columns of a matrix are alike if the
sets of numbers that make up the columns are equal. Write a
program that finds the numbers of all the columns of matrix
with size that are alike.
5.13. A matrix has a saddle point in if is a minimum
element in the i-th row and a maximum element in the j-th
column of . Write a program that determines if a saddle
point exists in a given matrix .
5.14. A matrix has a saddle point in if is a minimum
element in the i-th row and a maximum element in the j-th
column of . Write a program that finds all saddle points in
a given matrix .
5.15. A square matrix of -th order is given. Write a
program, which determines if there exist a number k, such
that the k-th column matches the k-th row of the matrix .
5.16. It is given a sequence of real numbers .
Write a program, which constructs and prints on the console
the square matrix:
5.17. Given a real square matrix of order . Write a
program which finds:
C = A + B
Y = A . X
C = A . B
A = AT AT A
A
A
N × N
A aij aij
A
A
A aij aij
A
A
A N
A
a1, a2, a3, . . . , an
A′ =
a1 a2 a3 . . . an−2 an−1 an
a2 −a3 a4 . . . an−1 an a1
a3 a4 a5 . . . an a1 a2 . . . an−2 an−1 an . . . an−5 an−4 an−3
an−1 an a1 . . . an−4 an−3 an−2
an a1 a2 . . . an−3 an−2 an−1
A N


5.18. It's given a real square matrix , , where
. Write a program which checks if is orthonormal,
where we define a matrix to be orthonormal if the scalar
product of any two different rows of is equal to 0 and the
*scalar product* of any row with itself is equal to 1.
5.19. Write a program which determines whether the square
matrix is a magic square, i.e. such that the sum of the
elements of all rows and columns is the same.
5.20. Write a program which asks the user for a natural
number and generates (also prints) eight matrices in
the pattern of the given example:
- for (note how all the numbers are aligned!):
a)
0 0 0 0
0 1 0 0
0 0 2 0
0 0 0 3
b)
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
c)
1 5 9 13
2 6 10 14
3 7 11 15
4 8 12 16
a) max
1≤i≤n
{min aij
1≤j≤n
}
b) max
1≤j≤n
{min aij
1≤i≤n
}
c) min
1≤i≤n
{max aij
1≤j≤n
}
d ) max
1≤j≤n
{min aij
1≤i≤n
}
A N × N
2 ≤ N ≤ 10 A
A
A
n n × n
n = 4
d)
1 2 3 4
0 1 2 3
0 0 1 2
0 0 0 1
e)
1 8 9 16
2 7 10 15
3 6 11 14
4 5 12 13
f)
1 3 6 10
2 5 9 13
4 8 12 15
7 11 14 16
g)
7 11 14 16
4 8 12 15
2 5 9 13
1 3 6 10
h)
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
5.21. Game of Life (Conway's Game of Life) is a game for zero
players and consists of a surface, divided in cells. Every
cell is dead or alive. The game is played in turns. Every
turn depends on the state of the game in the previous turn
following these rules:
- Every cell with less than two living neighbours dies (of
loneliness);
- Every living cell with more than three living neighbours
dies (too crowded);
- Every living cell with two or three living neighbours
stays alive for the next turn;
- Every dead cell with exactly three living neighbours
comes back to live.
Your task is to simulate this game. The initial state of the
surface should be hard-coded. On every turn the state of the
cells should be written to the console: 'X' for a living
cell, '.' for a dead cell.
- [Conway's Game of Life](https://en.wikipedia.org/wiki/
Conway%27s_Game_of_Life "Markdown Tutorial")
- [Simulation](https://bitstorm.org/gameoflife/ "Markdown
Tutorial")
- Some configurations:
![alt text](https://github.com/andy489/
Data_Structures_and_Algorithms_CPP/blob/master/assets/
Conway's%20Game%20of%20Life%2001.gif)
5.22. You are given numbers and a matrix that
consists of numbers 0 through 9. Your task is to create a
program that says how many islands and peninsulas are there.
These are the rules:
- 0 is water;
- 1-9 is the height of the ground;
- Islands are pieces of land connected by different grounds.
Example of an island:
00000000000000000
00000000110000000
00000001211000000
00000012321100000
00000123432110000
00000012332211000
00000001211000000
00000000100000000
00000000000000000
- Peninsulas are like islands but on the edge of the map.
Example of a peninsula:
98620000000000
76542110000000
84321000000000
52321100000000
32100000000000
21000000000000
10000000000000
n, m n × m
- Note that we don't actually care if the land is an island
or peninsula. We care about the number of different connected
lands.
5.23. We are given the coefficients and
. Write a program which solves the system of linear
equations:

github.com/andy489
Example input Expected output
10 5
4321000131
3210010010
2100121000
2100010000
1000000001
4
aij
(1 ≤ i ≤ n,1 ≤ j ≤ n)
bi
(i = 1,...,n)
a11x1 + a12x2 + a13x3 + . . . + a1nxn = b1
a21x1 + a22x2 + a23x3 + . . . + a2nxn = b2
a31x1 + a32x2 + a33x3 + . . . + a3nxn = b3 . . .
an1x1 + an2x2 + an3x3 + . . . + annxn = bn

More products