Tasks studies - laboratory
Choose one of the following tasks. Name the files with solutions depending on the selected task, respectively:
dst_gr4.c
and dst.exe
for grade 3db_gr4.c
and db.exe
. for grade 4bdb_gr4.c
and bdb.exe
for grade 5
After completing the colloquium, pack the files into a .zip archive and submit using the form.Create an Array
structure that stores the matrix. The structure should also contain the data necessary to read the values of the matrix cells.
Create a function initArray()
that returns an instance of the Array
structure. The function creates a new instance of the Array
structure and stores the matrix with a specified number of rows and columns passed as function parameters.
Create a function printArray()
that takes the Array
structure as a parameter.
The function prints the contents of the matrix in the following format:
2.00 2.00 2.00
2.00 2.00 2.00
12.00 12.00 12.00
Create the function multiplyArrays()
that accepts two instances of the Array
structure. In the function body, implement matrix multiplication according to the formula:
Take into account the following aspects:
Array
(two matrices can be multiplied only if the number of columns of matrix A is equal to the number of rows of matrix B).In the main()
method:
initArray()
function, create matrices of dimensions 2x3, 3x2, 3x3 and 4x2 with the following values1.00 2.00 3.00
4.00 5.00 2.00
1.00 4.00
3.00 1.00
5.00 4.00
1.00 2.00 3.00
4.00 5.00 2.00
4.00 5.00 2.00
1.00 2.00 3.00
4.00 5.00 2.00
4.00 5.00 2.00
1.00 2.00 3.00 4.00
4.00 5.00 2.00 1.00
multiplyArrays()
function by multiplying:printArray()
function)Modify the program so that the result of matrix multiplication is saved in the text file wyników.txt
in the form:
1.00 2.00 3.00
4.00 5.00 2.00
X
1.00 4.00
3.00 1.00
5.00 4.00
=
22.00 18.00
29.00 29.00
1.00 2.00 3.00
4.00 5.00 2.00
X
1.00 2.00 3.00
4.00 5.00 2.00
4.00 5.00 2.00
=
21.00 27.00 13.00
32.00 43.00 26.00
1.00 2.00 3.00
4.00 5.00 2.00
X
1.00 2.00 3.00 4.00
4.00 5.00 2.00 1.00
=
0.00
Create a function allocate_2d_array()
that dynamically allocates memory space for a floating-point array of the size passed as function parameters. The function returns a pointer to this array.
Create a function free_2d_array()
that frees memory space for the array passed as a function parameter.
In a comment, describe what the function parameters are for if their name does not suggest it.
Create a function print_matrix()
that prints the contents of the array to the console in the following form:
| 0.0000 | 0.1000 |
| 1.0000 | 41.1005 |
| 2.0000 | 42.1000 |
Create a function sort_by_column()
that sorts the array passed as a function argument descending by the value in the selected column. The column by which the sorting is to take place should be passed as the function’s arguemtn. e.g.
before sorting
| 1.0000 | 3.0000 | 5.0000 |
| 3.0000 | 4.0000 | 3.0000 |
| 4.0000 | 7.0000 | 1.0000 |
| 3.0000 | 1.0000 | 3.0000 |
after sorting with selection of 2 columns
| 4.0000 | 7.0000 | 1.0000 |
| 3.0000 | 4.0000 | 3.0000 |
| 1.0000 | 3.0000 | 5.0000 |
| 3.0000 | 1.0000 | 3.0000 |
after sorting with 1 column selection
| 4.0000 | 7.0000 | 1.0000 |
| 3.0000 | 1.0000 | 3.0000 |
| 3.0000 | 4.0000 | 3.0000 |
| 1.0000 | 3.0000 | 5.0000 |
Create a matrix using allocate_2d_array() and then fill it with the following values
| 1.0000 | 3.0000 | 5.0000 |
| 3.0000 | 4.0000 | 3.0000 |
| 4.0000 | 7.0000 | 1.0000 |
| 3.0000 | 1.0000 | 3.0000 |
Test the sort_by_column() function on the following cases:
Modify the program so that the program’s output is saved to the file results.txt
Create a palindrome()
function that checks if the characters stored in the array are palindromes (a word or string of characters that reads the same forwards and backwards) and then returns true if they are or false if they are not. The function accepts an array of any length and, when checking, omits empty characters from the end of the array. e.g.
[a, b, a, 0\] true
[a, b, b, a] true
[a, b, c, a] false
Create a function print_palindrome()
that takes an arbitrary size array as an argument and prints the contents of the array in the following format along with information whether the characters in the array form a palindrome or not.
[a, b, b, a] true
Test the print_palindrome()
function on the following arrays:
[k, a, j, a, k]
[1, 2, 3, 3, 2, 1]
[k, 1, u]
Modify the program so that the program’s output is saved to the file results.txt