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 a matrix of any size. The structure should contain the data necessary to iterate over the cells of the matrix.

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. The memory space for storing the matrix is ​​allocated dynamically.

Create a function detArray() that accepts an instance of the Array structure. The function should return a number calculated as follows:

Transforming the matrix to obtain an upper triangular form according to the formula:

\[A_{jk} = A_{jk} - \frac{A_{ji}}{A_{ii}} * A_{ik}\]

Individual indices change according to the sequence:

  1. i = 1,…,n-1
  2. j = i+1,…,n
  3. k = i+1,…,n

After the transformation, it is possible to calculate the value by multiplying the elements on the diagonal. Below, the number 1 indicates the elements from which the product must be calculated in the case of a 4x4 matrix.

[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]

The function should return appropriate messages if the matrix is ​​not square.

Create a function printArray() that takes an Array structure as a parameter. The function prints the contents of the array from the passed structure in the following format (columns signed with subsequent letters of the alphabet):

A B C
2.00 2.00 2.00
2.00 2.00 2.00
12.00 12.00 12.00

In the main() method:

A B C
1.00 2.00 3.00
4.00 5.00 5.00
1.00 2.00 4.00
A B C
1.00 2.00 3.00
1.00 2.00 2.00
1.00 2.00 3.00
A B C D
1.00 2.00 3.00 5.00
4.00 5.00 5.00 1.00

Check the detArray() function using the above examples.

EK_4

Modify the program so that the program results displayed in the console are also saved in the results.txt file.

A B C
1.00 2.00 3.00
4.00 5.00 5.00
1.00 2.00 4.00
det = -3.00

A B C
1.00 2.00 3.00
1.00 2.00 2.00
1.00 2.00 3.00
det = 0.00

A B C D
1.00 2.00 3.00 5.00
4.00 5.00 5.00 1.00
Calculating the determinant is possible for a square matrix
det = 0.00







Good

EK_3

Create a function allocate_2d_array() which 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() which 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() which 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() which sorts the array passed as a function argument ascending by the value in the selected column. The column to be sorted by should be passed as an argument to the function. 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
| 3.0000 | 1.0000 | 3.0000 |
| 1.0000 | 3.0000 | 5.0000 |
| 3.0000 | 4.0000 | 3.0000 |
| 4.0000 | 7.0000 | 1.0000 |

after sorting with 1 column selection
| 1.0000 | 3.0000 | 5.0000 |
| 3.0000 | 4.0000 | 3.0000 |
| 3.0000 | 1.0000 | 3.0000 |
| 4.0000 | 7.0000 | 1.0000 |

Create a matrix using the allocate_2d_array() function 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 function palindrome() 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 omits empty characters when checking. 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 array of any size 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

Write a program that reads characters from the keyboard from the user and then checks whether the characters entered by them are a palindrome. The program should read characters until the option to end character entry is selected.

Do you want to enter a character? 1
enter character: a
Do you want to enter a character? 1
enter character: b
Do you want to enter a character? 1
enter character: a
Do you want to enter a character? 0
[a, b, a] true

EK_4

Modify the program so that the program’s output is saved to the file results.txt. Each time we run the program, the history is appended to the end of the file

Do you want to enter a character? 1
enter a character: a
Do you want to enter a character? 1
enter a character: b
Do you want to enter a character? 1
enter a character: a
Do you want to enter a character? 0
[a, b, a] true
# Effect EK_03: ## DST: The student is able to correctly prepare programs of 20-50 lines of code, solving 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, solving 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, solving 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, solving simple problems, using the capabilities of standard libraries for this purpose 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