Tasks studies - laboratory
Choose one of the following tasks. Name the files with solutions depending on the selected task, respectively:
dst_gr2.c
and dst.exe
for grade 3db_gr2.c
and db.exe
. for grade 4bdb_gr2.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 contain the data necessary for iterating over the matrix cells and the matrix determinant.
Create a detArray()
function that accepts instances of the Array
structure. In the function body, implement the matrix transformation to obtain the upper triangular form according to the formula:
where the individual indices change according to the sequence:
After the transformation, it is possible to calculate the determinant of the matrix by multiplying the elements on the diagonal. Below, the number 1 indicates the elements from which the product must be calculated.
[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]
The function should return messages if the matrix is not square.
Create a function initArray()
that returns an instance of the Array
structure. The function creates a new instance of the Array
structure and stores a 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 array from the passed structure in the following format:
2.00 2.00 2.00
2.00 2.00 2.00
12.00 12.00 12.00
Create a function fillArray()
that takes an instance of the Array
class and asks the user to fill in the contents of the array. After filling in the contents, the determinant of the matrix is calculated and the appropriate field of the Array
structure is filled.
Example session:
Array[0][0] = 2.0
Array[0][1] = 4.42
Array[1][0] = 5.2
Array[1][1] = 6.0
Array[2][0] = 9
Array[2][1] = 22.4
In the main()
method:
Array
.initArray()
function to create matrices of dimensions 3x2 and 2x3fillArray()
function to fill the matrices with values. After filling the values, the determinant of the matrix should be calculated.Save the result of matrix multiplication in the text file wyników.txt
in the form:
1.00 2.00 3.00
4.00 5.00 2.00
1.00 4.00
3.00 1.00
5.00 4.00
=
22.00 18.00
29.00 29.00
Write the function print_matrix()
which prints the contents of the matrix (the matrix should be allocated dynamically using appropriate functions) of floating-point numbers in the following of the form:
| 0.00 | 0.10 |
| 1.00 | 1.10 |
| 2.00 | 2.10 |
Create a function euclidDistance()
that takes two arrays of arbitrary length as function parameters. The function calculates the Euclidean distance between points specified by the array values according to the formula:
Then return the calculated value.
Create variables that hold the following test arrays:
| 0.00 | 0.10 |
| 1.00 | 1.10 |
| 2.00 | 2.10 |
| 0.00 | 0.10 | 5.10 |
| 1.00 | 1.10 | 8.10 |
| 2.00 | 2.10 | 1.10 |
Write a function calculate_distances()
that calculates and prints the distances between all rows of the matrix passed as an argument to the function, i.e. between row 1 and 1, between row 1 and 2, between row 1 and 3 …. between row 3 and 3.
sample result:
row 1 row 1 = 0.00
row 1 row 2 = 3.32
...
row 3 row 3 = 3.5.2
Call the function calculate_distances()
for each of the test matrices to get the results.
Modify the program so that the calculated results are saved to the file wyniki.txt
.
Write the function duplicate_element()
which duplicates the element at the specified index (value passed as a function parameter) n times (value passed as a function parameter) in a one-dimensional array of characters. If the number of elements exceeds the size of the array, print “Warning: number of elements exceed Array size!”.
e.g.: I duplicate the element at index 2 twice.
['a', '1', 'g', 'c', 'c', '\0', '\0', '\0']
['a', '1', 'g', 'g', 'g', 'c', 'c', '\0', '\0']
Write a function print_array()
that prints the elements of an array in the following format:
['a', '1', 'g', 'c', 'c']
Test the function on 3 selected examples.
In the main method, write a program that saves the results from the previous task to the file results.txt