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 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 a matrix with a specified number of rows and columns passed as function parameters.

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

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

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

The 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, multiply 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.

more information

Create the function printArray() which 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

In the main() method:

1.00 2.00 3.00
4.00 5.00 5.00
1.00 2.00 4.00
1.00 2.00 3.00
1.00 2.00 2.00
1.00 2.00 3.00
1.00 2.00 3.00 5.00
4.00 5.00 5.00 1.00

EK_4

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

1.00 2.00 3.00
4.00 5.00 5.00
1.00 2.00 4.00
det = -3.00

1.00 2.00 3.00
1.00 2.00 2.00
1.00 2.00 3.00
det = 0.00

1.00 2.00 3.00 5.00
4.00 5.00 5.00 1.00
Calculation of the value is possible for a square matrix
det = 0.00







Good

EK_3

Write a function allocate_2d_array() that dynamically allocates space in memory for a matrix of floating-point numbers with the size passed as function parameters. The function returns a pointer to this matrix.

Write a function free_2d_array() that frees space in memory for the matrix passed as a function parameter.

Describe in a comment what the function parameters are responsible for if their name does not suggest it.

Write a function print_matrix() that prints the contents of the matrix in the console in the following form:

| 0.00 | 0.10 |
| 1.00 | 4331.10 |
| 2.00 | 42.10 |

Create a function euclidDistance() that takes two arrays of any length as function parameters. The function calculates the Euclidean distance based on the array values ​​according to the formula:

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

Create a function distnaceMatrix() that calculates the distances between successive rows of the matrix passed as a function parameter and enters the calculated values ​​into a new matrix. e.g. having a matrix:

| 0.00 | 0.00 |
| 0.00 | 1.00 |
| 0.00 | 2.00 |

the result of the operation will be a 3x3 matrix where in cell [1,1] is the distance between row 1 and 1, in cell [1,2] the distance between row 1 and 2, in cell … in cell [3,3] the distance between row 3 and 3:

| 0.00      | 1.00      | 2.00      |
| 1.00      | 0.00      | 1.00      |
| 2.00      | 1.00      | 0.00      |

In the main() method, create the following menu:

1. Enter matrix
2. Display matrix
3. Distance matrix
4. Exit

After selecting option 1, the program asks the user for the size of the matrix, allocates space for the matrix, and then the user fills the matrix with data.

1 selected - Enter matrix.
Enter number of rows: 2
Enter number of columns: 2
tab[1][1] = 1
tab[1][2] = 3
tab[2][1] = 3
tab[2][2] = 2

After selecting option 2, the program displays the matrix using the print_matrix() function

After selecting option 3, the program displays the distance matrix of the matrix currently stored in memory.

The program should run until the exit option is selected. The memory should be freed at the appropriate time.

EK_4

Modify the program so that the history of user actions is saved to the file wyniki.txt.







Sufficient

EK_3

Create 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) after this element 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!” and increase the size of the array so that it stores all the elements.

e.g.: I duplicate the element at index 2, 2 times.

['a', '1', 'g', 'c', 'c', '\0', '\0', '\0']

['a', '1', 'g', 'g', 'g', 'c', 'c', '\0', '\0']

Create a function print_array() that prints the elements of the array in the following format:

['a', '1', 'g', 'c', 'c']

Test the function on 3 selected examples.

EK_4

Modify the program so that the result of the program’s operation is saved in the file results.txt in the following form:

['a', '1', 'g', 'c', 'c']
duplicate element 3 twice
['a', '1', 'g', 'g', 'g', 'c', 'c']
# Effect EK_03: ## DST: The student is able to correctly prepare programs of 20-50 lines of code, solving simple problems, using for this purpose 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 for this purpose 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 the data structures and programming constructs available in the C language that have been learned. Effect # EK_04: ## DST: The student is able to correctly prepare programs of 20-50 lines of code that solve simple problems, using the capabilities of standard programming libraries available in the C language used in class. ## 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 class. ## BDB: The student is able to correctly prepare programs of 50-100 lines of code that solve intermediate problems, using the capabilities of standard programming libraries available in the C language, including those that the student has learned independently