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

Write a function genPassword() that takes 3 parameters: number of letters in the password, number of digits in the password, number of special characters in the password.

ASCII table ASCII code 33 = !
ASCII code 34 = "
ASCII code 35 = #
ASCII code 36 = $
ASCII code 37 = %
ASCII code 38 = &
ASCII code 39 = '
ASCII code 40 = (
ASCII code 41 = )
ASCII code 42 = *
ASCII code 43 = +
ASCII code 44 = ,
ASCII code 45 = -
ASCII code 46 = .
ASCII code 47 = /
ASCII code 48 = 0
ASCII code 49 = 1
ASCII code 50 = 2
ASCII code 51 = 3
ASCII code 52 = 4
ASCII code 53 = 5
ASCII code 54 = 6
ASCII code 55 = 7
ASCII code 56 = 8
ASCII code 57 = 9
ASCII code 58 = :
ASCII code 59 = ;
ASCII code 60 = <
ASCII code 61 = =
ASCII code 62 = >
ASCII code 63 = ?
ASCII code 64 = @
ASCII code 65 = A
ASCII code 66 = B
ASCII code 67 = C
ASCII code 68 = D
ASCII code 69 = E
ASCII code 70 = F
ASCII code 71 = G
ASCII code 72 = H
ASCII code 73 = I
ASCII code 74 = J
ASCII code 75 = K
ASCII code 76 = L
ASCII code 77 = M
ASCII code 78 = N
ASCII code 79 = O
ASCII code 80 = P
ASCII code 81 = Q
ASCII code 82 = R
ASCII code 83 = S
ASCII code 84 = T
ASCII code 85 = U
ASCII code 86 = V
ASCII code 87 = W
ASCII code 88 = X
ASCII code 89 = Y
ASCII code 90 = Z
ASCII code 91 = [
ASCII code 92 = \
ASCII code 93 = ]
ASCII code 94 = ^
ASCII code 95 = _
ASCII code 96 = `
ASCII code 97 = a
ASCII code 98 = b
ASCII code 99 = c
ASCII code 100 = d
ASCII code 101 = e
ASCII code 102 = f
ASCII code 103 = g
ASCII code 104 = h
ASCII code 105 = i
ASCII code 106 = j
ASCII code 107 = k
ASCII code 108 = l
ASCII code 109 = m
ASCII code 110 = n
ASCII code 111 = o
ASCII code 112 = p
ASCII code 113 = q
ASCII code 114 = r
ASCII code 115 = s
ASCII code 116 = t
ASCII code 117 = u
ASCII code 118 = v
ASCII code 119 = in
ASCII code 120 = x
ASCII code 121 = y
ASCII code 122 = z
ASCII code 123 = {
ASCII code 124 = |
ASCII code 125 = }
ASCII code 126 = ~

The function prints a randomly generated password in the console containing a specified number of characters. e.g. 3 letters 2 digits 1 special character

A4?yH0

The position of letters, digits and special characters is random.

In the main() method, implement the menu:

1. Generate a new password.
2. Exit the program.

The program should run until the option to exit the program is selected. After selecting the Generate new password option, the user is asked about the number of letters in the password, the number of digits in the password and the number of special characters. After entering the data, the password is displayed in the console. Sample session:


1. Generate a new password.
2. Exit the program.

Select options: 1
How many letters: 1
How many digits: 1
How many special characters: 1
z!4

1. Generate a new password.

2. Exit the program.

Select options: 1
How many letters: 2
How many digits: 0
How many special characters: 2
*a#Z

1. Generate a new password.

2. Exit the program.

Select options: 2

Exit the program.

4.0

Create the program db_macierzodleglosci.

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

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

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

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

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

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

Create the function distnaceMatrix() which 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 the 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. Output

After selecting option 1 the program asks the user for the size of the matrix, allocates space for this 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 moment.

5.0

Create the bdb_TicTacToe program

  1. Create an Array structure that stores a dynamically allocated square matrix that stores characters. The structure should contain data about the size of the matrix.

  2. Create the initArray() function 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. By default, the matrix is ​​filled with \0 values.

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

  4. Create the makeMove() function that takes as a parameter the Array structure, the row number, column number, and the player’s symbol. If the move is valid (we do not exceed the range of the array and the field is free - it contains the symbol \0), the function writes the symbol in the appropriate place and returns 1. Otherwise, the function returns -1. If the array is completely filled, the function returns 2 (a tie)

  5. Create the printArray() function that takes as a parameter the Array structure. The function prints the contents of the matrix in the following format:

X O X X
X X X
X X
O
  1. Create a function checkBoard() that accepts an instance of the Array structure. In the body of the function, implement a check to see if one of the players has filled a row, column or diagonal with their symbol. If so, the function returns this symbol.

  2. In the main() method, create a menu in which the user is asked about:

Example session:

Enter board size: 2
Enter number of players: 2
Enter symbol Player 1: X
Enter symbol Player 2: X
Symbol occupied, Enter symbol Player 2: O
= ...

===============================================
Player O's move
Enter row and column number (e.g. 1 1): 2 1
= ...
= ...