Programming C

Tasks studies - laboratory


Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko

Exam 2

Choose one of the following tasks. Name the files with solutions depending on the selected task, respectively:

Very good

EK_3

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:

\[a_{jk} = a_{jk} - a_{ji}/a_{ii} * a_{ik}\]

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.

more information

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:

EK_4

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

Good

EK_3

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:

\[d(p,q)=\sqrt{\sum_{i=1}^{n}\left(q_{i}-p_{i}\right)^2}\]

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.

EK_4

Modify the program so that the calculated results are saved to the file wyniki.txt.

Dostawczny

EK_3

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.

EK_4

In the main method, write a program that saves the results from the previous task to the file results.txt

# Effect EK_03: ## DST: The student is able to correctly prepare programs of 20-50 lines of code that solve simple problems, using some of the learned data structures and programming constructs available in the C language. ## DB: The student is able to correctly prepare programs of 20-50 lines of code that solve intermediate problems, using all of the learned data structures and programming constructs available in the C language. ## BDB: The student is able to correctly prepare programs of 50-100 lines of code that solve intermediate problems, using all of the learned data structures and programming constructs available in the C language. Effect # EK_04: ## DST: The student is able to correctly prepare programs of 20-50 lines of code that solve simple problems, using to the capabilities of standard programming libraries available in C used in classes. ## DB: The student is able to correctly prepare programs of 20-50 lines of code, solving intermediate problems, using the capabilities of standard programming libraries available in C used in classes. ## BDB: The student is able to correctly prepare programs of 50-100 lines of code, solving intermediate problems, using the capabilities of standard programming libraries available in C, including those that the student learned independently