Programming C

Tasks studies - laboratory


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

Exam 2

Laboratory – two practical tests at the computer:

Effect EK_04:

DST: The student is able to correctly create a program that solves simple problems using appropriate data structures and programming constructs available in the C language.

DB: The student is able to correctly create programs that solve intermediate-advanced problems using appropriate data structures and programming constructs available in the C language.

BDB: The student is able to correctly create programs that solve advanced problems using appropriate data structures and programming constructs available in the C language.

Effect EK_05:

DST: The student is able to correctly create a program that solves simple problems using the capabilities of standard libraries available in the C language.

DB: The student is able to correctly create programs that solve intermediate-advanced problems using the capabilities of standard libraries available in the C language.

BDB: The student is able to correctly create programs that solve advanced problems using the capabilities of standard libraries available in the C language.

PPWJC

In addition, the following is assessed:

Sentences

You should be able to read data from text files and write data to text files.

SUFFICIENT

Multiplying a matrix by a number

Write the function matrix_multiplication(). Which takes the following parameters:

Write the function print_matrix(), which takes the following parameters:

0.0000 0.1000
1.0000 1.1000
2.0000 2.1000

In the main() function, initialize the following arrays and test the matrix_multiplication() function.

0.0000 0.1000 0.0000 0.2000
1.0000 1.1000 X 2 = 2.0000 2.2000
2.0000 2.1000 4.0000 4.2000
1.0000 2.0000 3.0000 3.0000 6.0000 9.0000
3.0000 2.0000 1.0000 X 3 = 9.0000 6.0000 3.0000

GOOD

Calculating Euclidean distance for two vectors.

Create a function euclidDistance() that takes two arrays of length 3 as function parameters. The function calculates the Euclidean distance between points specified by the array values ​​according to the formula:

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

Then return the calculated value.

Create a variable that stores the following values ​​X, Y, and Z:

X Y Z
1 1 1
2 1 1
3 3 3
1 2 3

In the main function, calculate and print the distances between all vectors, i.e. between 1 and 1, between 1 and 2, between 1 and 3 …. between 4 and 4.

Duplicating the selected element n times

Create the duplicateRow() function that takes 4 parameters:

If the repetition of elements exceeds the size of the array, print the message “Warning: Elements exceeded size from array!” in the console. Test the function.

Checking if the strings are the same

Create the compare() function that takes two arrays of the same length containing characters and the number of characters. The function returns 0 if the words are the same or the number of letters they differ in.

VERY GOOD

Matrix multiplication

Create an Array structure that stores:

Create a function initArray() that returns an instance of the Array structure and takes the following parameters:

Create a function printArray() that 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

Create a function fillArray() that takes an instance of the Array class and asks the user to fill in the contents of the array. Example session:

Array[0][0] = 1
Array[0][1] = 1
Array[1][0] = 1
Array[1][1] = 1
Array[2][0] = 11
Array[2][1] = 1

Create a function multiplyArrays() that accepts two instances of the Array structure. In the body of the function, implement matrix multiplication. Consider the following aspects:

In the main() method:

Comparing arrays.

Sorting data in an array.

https://blog.etrapez.pl/rozwiazywanie-ukladow-rownan-metoda-macierzy-odwrotnej/

#include <stdio.h>
#include <stdlib.h>
typedef struct array{
int row;
int col;
float **tab;
} array; void matrix_multiplication(float **tab, unsigned int col, int row, float x)
{
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
 tab[i][j] *= x; // tab[i] = tab[i] * x
 }
 }
}
void print_matrix(array x)
{
 for (int i = 0; i < x.row; i++)
 {
 for (int j = 0; j < x.col; j++)
 {
 printf("%f\t", x.tab[i][j]);
 }
 printf("\n");
 }
}
array createMatrix(int ​​row, int col){
 array x;
 x.col = col;
 x.row = row;
 float **tab3;
 tab3 = calloc(4, sizeof(float *));
 for (int i = 0; i < 4; i++)
 {
 tab3[i] = calloc(2, sizeof(float));
 }
 x.tab = tab3;
}
int main(int argc, char const *argv[])
{
 asd board;

 int row = 4;
 int col = 2;
 float tab[4][2] = { {1, 2}, {2, 3}, {3, 4}, {4, 5} };
 float tab2[4][2] = {1, 2, 2, 3, 3, 4, 4, 5};
 // matrix_multiplication(tab, 2, 4, 4);
 float **tab3;
 tab3 = calloc(4, sizeof(float *));
 for (int i = 0; i < 4; i++)
 {
 tab3[i] = calloc(2, sizeof(float));
 }
 for (int i = 0; i < row; i++)
 {
 for (int j = 0; j < col; j++)
 {
 printf("enter the value of tab[%d][%d]", i, j);
 floatval;
 scanf("%f", &val);
 tab3[i][j] = val;
 }
 }
 print_matrix(tab3, 4, 2);
 return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct array{
 int row;
 int col;
 float **tab;
} board;
void matrix_multiplication(array tab, float x)
{
 for (int i = 0; i < tab.row; i++)
 {
 for (int j = 0; j < tab.col; j++)
 {
 tab.tab[i][j] *= x; // tab[i] = tab[i] * x
 }
 }
}
void print_matrix(array x)
{
 for (int i = 0; i < x.row; i++)
 {
 for (int j = 0; j < x.col; j++)
 {
 printf("%f\t", x.tab[i][j]);
 }
 printf("\n");
 }
}
array createMatrix(int ​​row, int col){
 array x;
 x.col = col;
 x.row = row;
 float **tab3;
 tab3 = calloc(4, sizeof(float *));
 for (int i = 0; i < 4; i++)
 {
 tab3[i] = calloc(2, sizeof(float));
 }
 x.tab = tab3;
 return x;
}
int main(int argc, char const *argv[])
{
 array asd = createMatrix(4,2);
 for (int i = 0; i < asd.row; i++)
 {
 for (int j = 0; j < asd.col; j++)
 {
 printf("enter the value of tab[%d][%d]", i, j);
 floatval;
 scanf("%f", &val);
 asd.tab[i][j] = val;
 }
 }
 print_matrix(asd);
 return 0;
}