Tasks studies - laboratory
Project maintained by dawidolko Hosted on GitHub Pages — Theme by dawidolko
Exam 2 Overview
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
- EK_01 Knows the basic concepts related to computer programming. - variable - types of variables - local and global variables - pointer - reference - compiler - IDE - conditional statements - library - algorithm - naming conventions
- EK_02 Knows basic data structures and selected programming constructs available in C. - arrays - structures - unions - if else, switch - for, while, do while - functions
- EK_03 Is able to use appropriate data structures and programming constructs available in C when creating programs that solve given problems.
- EK_04 Is able to create programs using standard programming libraries available in C.
In addition, the following is assessed:
- Code readability
- Adherence to naming conventions
- Knowledge of the strengths and weaknesses of the implemented solution (scalability, computational and memory costs)
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:
- a pointer to an array of floating-point numbers
- the number of columns
- the number of rows
- the number by which the matrix will be multiplied The function multiplies the matrix by a value and does not return a value, but updates the values in the array passed as the first argument.
Write the function print_matrix(), which takes the following parameters:
- a pointer to an array.
- number of columns
- number of rows The function prints the array in the following form:
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:
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:
- a pointer to a one-dimensional array containing floating-point numbers
- the size of the array
- the index of the element to be duplicated
- the number of times the duplicated element is repeated
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:
- the number of matrix columns
- the number of matrix rows
- a pointer to pointers that store floating point numbers
Create a function initArray() that returns an instance of the Array structure and takes the following parameters:
- number of rows of the matrix
- number of columns of the matrix
The function creates a new instance of the
Arraystructure to which it assigns the number of rows and columns. Then it reserves a place in memory to store a two-dimensional array and assigns a pointer to this place in the appropriate field of theArraystructure. The complete instance is returned by the function.
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:
- when it is possible to multiply a matrix by a matrix
- the function should return a new matrix that is the result of multiplying these matrices
In the main() method:
- Create two variables of type
Array. - Use the
initArray()function to create 3x2 and 2x3 matrices - Call the
fillArray()function to fill the matrices with values - Call the
multiplyArrays()function passing the created matrices - Print the result of matrix multiplication in the console (
printArray()function)
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;
}