Programming C

Tasks studies - laboratory


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

C

Choose one of the tasks below.

After completing the colloquium, pack the files into a .zip archive and submit using the form.

3.0

Create the dst_palindrome program.

Create the checkPalindrom() function that accepts a string of characters (array of characters terminated by an empty character). The function counts how many characters are in the correct position to create a palindrome (a word read forwards and backwards that sounds the same) and returns that number. If the string of characters is a palindrome, then it returns the number of letters in the word. e.g.

In the main() method, write a program that checks the function using the following examples:

"kajak" the function should return 5
"Ala alA" the function should return 7
" zrzegorz " the function should return 6
" ala " the function should return 7

In the further part of the main() method, after displaying the test cases, add a fragment that will ask the user to select an option from the following menu:

1. Enter a word.
2. Exit the program.

The program should run until the option exit the program is selected. After selecting the option enter a word, the word entered by the user is read (maximum 100 characters). Then the program checks if the characters entered by the user are palindromes using the created function and prints the result. Sample session:

"kajak" - 5 palindrome
"Ala alA" - 7 palindrome
" zrzegorz " - 6
" ala " - 7 palinfrom

1. Enter a word.

2. Exit the program.

Select options: 1
Enter a word: alaska
alaska - 2

1. Enter a word.

2. Exit the program.

Select options: 1
Enter a word: ala
ala - 3 palindrome

1. Enter a word.

2. Exit the program.

Select options: 2

Exit the program.

4.0

Create the db_sortowanie program.

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

Create the free_2d_array() function that frees memory space for the matrix passed as the function parameter.

In a comment, describe what the function parameters are responsible for if their name does not suggest it.

Create the print_matrix() function that prints the contents of the matrix 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 the function argument ascending by the value in the selected column. The column by which the sorting should take place should be passed as the function argument. 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:

5.0

Create the bdb_mnozięmarzy program

  1. Create an Array structure that stores a dynamically allocated two-dimensional array of floating-point numbers. The structure should contain data about the size of the matrix.

  2. 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 the specified number of rows and columns passed as parameters to the function. By default, the matrix is ​​filled with 0 values.

  3. Create a function freeArray() that takes as a parameter a pointer to the Array structure. The function deallocates the space that stores the dynamically allocated space for the matrix. If everything runs successfully, the function returns true.

  4. Create a function fillArray() that fills the matrix with values ​​provided by the user. Before entering the value, the user should receive the following message informing which cell to fill.

[1][1] = 1
[1][2] = 2
[2][1] = 1
[2][2] =
  1. Create a function printArray() that takes as a parameter the Array structure.
[1][1] = 1
[1][2] = 2
[2][1] = 1
[2][2] =
  1. Create a function printArray() that takes as a parameter the Array structure. The function prints the contents of the matrix in the following format:
2.000 2.000 2.000
2.000 2.000 2.000
12.000 12.000 12.000
  1. Create the function multiplyArrays() that accepts two instances of the Array structure. In the body of the function, implement matrix multiplication according to the formula:
\[C_{ij} = \Sigma_{k=1}^{p} A_{ik} B_{kj}\]

The method returns a new Array structure that is the result of multiplying the passed matrices.

Take into account the following aspects:

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

Multiply the initialized matrices using the created method. Save the result of the multiplication in the file wynik.txt:

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

Create a menu that allows the user to create two matrices, fill them with values, and then display the result of multiplying these matrices.

Enter number of columns: 2
Enter number of rows: 2
[1][1] = 1
[1][2] = 1
[2][1] = 1
[2][2] = 1

1.00 1.00
1.00 1.00

Enter number of columns: 2
Enter number of rows: 2
[1][1] = 1
[1][2] = 1
[2][1] = 1
[2][2] = 1

1.00 1.00
1.00 1.00

Multiplication result
1.00 1.00
1.00 1.00 1.00