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 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:

\[C_{ij} = \Sigma_{k=1}^{p} A_{ik} B_{kj}\]

Take into account the following aspects:

In the main() method:

1.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

EK_4

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







Good

EK_3

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:

EK_4

Modify the program so that the program’s output is saved to the file results.txt






Sufficient

EK_3

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]

EK_4

Modify the program so that the program’s output is saved 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 data structures and programming constructs available in the C language that they have learned. ## DB: The student is able to correctly prepare programs of 20-50 lines of code that solve intermediate problems, using for this purpose all the 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 for this purpose all the data structures and programming constructs available in the C language that he/she has learned.Effect # EK_04: ## DST: The student is able to correctly prepare programs of 20-50 lines of code that solve simple problems, using for this purpose the capabilities of standard programming libraries available in the C language used in classes. ## DB: The student is able to correctly prepare programs of 20-50 lines of code that solve intermediate problems, using the capabilities of standard programming libraries available in the C language 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 the C language, including those that the student learned independently