Compare commits

...

2 Commits

Author SHA1 Message Date
Rhinemann f602fec98a Merge pull request 'Initial commit' (#1) from master into main
Reviewed-on: #1
2024-03-09 17:34:28 +02:00
rhinemann 8175cbbdbf Initial commit 2024-03-09 17:32:59 +02:00
18 changed files with 591 additions and 0 deletions

8
Lab_2/.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

2
Lab_2/.idea/Lab_2.iml Normal file
View File

@ -0,0 +1,2 @@
<?xml version="1.0" encoding="UTF-8"?>
<module classpath="CMake" type="CPP_MODULE" version="4" />

4
Lab_2/.idea/misc.xml Normal file
View File

@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CMakeWorkspace" PROJECT_DIR="$PROJECT_DIR$" />
</project>

8
Lab_2/.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/Lab_2.iml" filepath="$PROJECT_DIR$/.idea/Lab_2.iml" />
</modules>
</component>
</project>

49
Lab_2/Lab_2_loops.c Normal file
View File

@ -0,0 +1,49 @@
#include <stdio.h>
#include <math.h>
int main() {
int operation_counter = 0;
int n;
printf("Input n (must be a natural number): ");
scanf("%d", &n);
double result = 0;
operation_counter += 1; // result = 0
operation_counter += 2; // for loop
for (int i = 1; i <= n; i++)
{
double denominator = 1;
operation_counter += 1; // denominator = 1
operation_counter += 2; // for loop
for (int j = 1; j <= i; j++)
{
denominator *= j + cos(j);
operation_counter += 5; // denominator *= j + cos(j), j <= i, j++
}
double power_of_four = 1;
operation_counter += 1; // power_of_four = 1
operation_counter += 2; // for loop
for (int k = 1; k <= i; k++)
{
power_of_four *= 4;
operation_counter += 3; // power_of_four *= 4, k <= i, k++
}
result += (power_of_four - i) / denominator;
operation_counter += 5; // result += (power_of_four - i) / denominator, i <= n, i++
}
printf("Result = %.7f\n", result);
printf("Number of operations = %d\n", operation_counter);
return 0;
}

34
Lab_2/Lab_2_mem.c Normal file
View File

@ -0,0 +1,34 @@
#include <stdio.h>
#include <math.h>
int main()
{
int operation_counter = 0;
int n;
printf("Input n (must be a natural number): ");
scanf("%d", &n);
double result = 0;
double denominator = 1;
double power_of_four = 1;
operation_counter += 3; // power_of_four = 1, denominator = 1, result = 0
operation_counter += 2; // for loop
for (int i = 1; i <= n; i++)
{
denominator *= i + cos(i);
power_of_four *= 4;
result += (power_of_four - i) / denominator;
operation_counter += 9; // result += (power_of_four - i) / denominator, power_of_four *= 4, denominator *= i + cos(i), i <= n, i++
}
printf("Result = %.7f\n", result);
printf("Number of operations = %d\n", operation_counter);
return 0;
}

11
Lab_2/range.sh Normal file
View File

@ -0,0 +1,11 @@
#!/bin/bash
for i in {1..5}
do
echo "Bash Brace Expansion: "$i;
done
for i in $(seq 1 0.5 5)
do
echo "Sequence: "$i;
done

6
Lab_2/test.sh Normal file
View File

@ -0,0 +1,6 @@
#!/bin/bash
for i in {1..10}
do
echo $((5**$i));
done

90
Lab_3/code.c Normal file
View File

@ -0,0 +1,90 @@
#include <ncurses.h>
struct vec2 {
int x;
int y;
};
struct borders {
int top;
int bottom;
int left;
int right;
};
int main()
{
initscr(); // initialise screen
clear(); // clear the empty buffer just in case
noecho(); // disable the direct typing to the terminal so it doesn't mess with the application
// here we define utility variables
// borders for a pen point
struct borders b;
b.bottom = LINES;
b.top = -1;
b.left = -1;
b.right = COLS;
// pen point position
struct vec2 position;
position.x = COLS;
position.y = LINES - 1;
// pen point direction
struct vec2 direction;
direction.x = -1;
direction.y = 0;
// here we start the drawing
for (int i = 0; i < COLS*LINES; i++)
{
// move & paint
position.x += direction.x;
position.y += direction.y;
mvaddch(position.y, position.x, '*');
// flush the buffer to show result on the screen
refresh();
// check if we are about to meet the border
if (position.x + direction.x <= b.left) { // if yes, then...
// turn right
direction.x = 0;
direction.y = -1;
// grab the border and pull it towards us as bit
b.bottom--;
}
// and then repeat for every single border we have...
else if (position.y + direction.y <= b.top) {
direction.x = 1;
direction.y = 0;
b.left++;
}
else if (position.x + direction.x >= b.right) {
direction.x = 0;
direction.y = 1;
b.top++;
}
else if (position.y + direction.y >= b.bottom) {
direction.x = -1;
direction.y = 0;
b.right--;
}
// and we wait... wait... wait...
napms(20);
}
// hold the buffer until the keypress
getch();
// finish & cleanup
endwin();
return 0;
}

4
Lab_3/run.sh Normal file
View File

@ -0,0 +1,4 @@
#!/bin/sh
gcc code.c -o code -l ncurses
./code

129
Lab_4/code.c Normal file
View File

@ -0,0 +1,129 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int binary_search(float s_key, int r, int c, float matrix[r][c])
{
short int mid, found;
float value;
found = 0;
for (short int i = 0; i < r; i++) {
short int start = 0;
short int end = c - 1;
while (start <= end) {
mid = round((start + end) / 2);
value = matrix[i][mid];
if (value == s_key) {
printf("%.3f found at (%d, %d)\n", s_key, i+1, mid+1);
found = 1;
break;
} else if (value < s_key) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
if (found == 0) {
printf("%.3f not found.\n", s_key);
} else {
return 0;
}
}
void line_pr(short int len)
{
for (short int i = 0; i < len; i++) {
printf("-");
}
}
int main ()
{
short int rows, columns, indent, c_indent, line_len;
float key, max_num;
char cont;
printf("Input the number of rows and columns separated with a space: ");
scanf("%hd %hd", &rows, &columns);
float array[rows][columns];
for (short int i = 0; i < rows; i++) {
for (short int j = 0; j < columns; j++) {
printf("Input a value for %hd %hd: ", i, j);
scanf("%f", &array[i][j]);
}
}
max_num = 0;
for (short int i = 0; i < rows; i++) {
if(array[i][0] > max_num){
max_num = array[i][0];
}
}
indent = floor(log10(fabs(max_num))) + 6;
if (rows >= columns) {
c_indent = floor(log10(rows));
} else {
c_indent = floor(log10(columns));
}
c_indent +=2;
line_len = c_indent + indent * columns + columns * 2 + 1;
printf("%*c|", c_indent, *" ");
for (short int i = 1; i <= columns; i++) {
printf("%*d |", indent, i);
}
printf("\n");
line_pr(line_len);
printf("\n");
for (short int i = 0; i < rows; i++) {
printf("%*d|", c_indent, i+1);
for (short int j = 0; j < columns; j++) {
printf("%*.3f |", indent, array[i][j]);
}
printf("\n");
line_pr(line_len);
printf("\n");
}
while(1) {
printf("Input the element to search for: ");
scanf("%f", &key);
binary_search(key, rows, columns, array);
while (1) {
printf("Would you like to search again [y/n]: ");
scanf(" %c", &cont);
if (cont == 'y') {
break;
} else if (cont == 'n') {
return 0;
} else {
printf("Improper answer, try again.\n");
}
}
}
}

5
Lab_4/run.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/sh
gcc code.c -o code -lm
./code

42
Lab_4/test.py Normal file
View File

@ -0,0 +1,42 @@
#!/bin/python3
import random
import time
import os
#r, c = map(int, input("R c: ").split())
arr_size = 100
arr = [i + round(random.random(), 3) for i in range(arr_size, 0, -5)]
#a = [[ round((random.random() - 0.5) * 20, 3) for i in range(ar_size[0]) ] for j in range(ar_size[1])]
#a = [ [ round((random.random() - 0.5) * 20, 3) for i in range(round(ar_size[0] / 2)) ] * 2 for j in range(ar_size[1]) ]
#a[0][4] = 5
#a[0][7] = 5
for i in range(5, -1, -1):
print(f"Starting in {i}...")
time.sleep(1)
'''for i in range(row):
for j in range(4):
os.system(f"xdotool type --delay 2 -- {i}")
os.system("xdotool key Return")'''
map(str, arr)
for i in range(0, 10):
for j in range(5):
os.system(f"xdotool type --delay 2 -- {arr[i]}")
os.system("xdotool key Return")
# print(arr[i], end = " ")
for j in range(5):
os.system(f"xdotool type --delay 2 -- {arr[i+1]}")
os.system("xdotool key Return")
# print(arr[i+1], end = " ")
# print("\n")

109
Lab_5/code.c Normal file
View File

@ -0,0 +1,109 @@
#include <stdio.h>
#include <stdlib.h>
void line_pr(short int len)
{
for (short int i = 0; i < len; i++) {
printf("-");
}
}
int main ()
{
short int rows, columns, indent, c_indent, line_len;
int temp;
printf("Input the number of rows and columns separated with a space: ");
scanf("%hd %hd", &rows, &columns);
int array[rows][columns];
for (short int i = 0; i < rows; i++) {
for (short int j = 0; j < columns; j++) {
printf("Input a value for %hd %hd: ", i + 1, j + 1);
scanf("%d", &array[i][j]);
}
}
printf("Matrix before sorting:\n");
indent = 6;
if (rows >= columns) {
c_indent = floor(log10(rows));
} else {
c_indent = floor(log10(columns));
}
c_indent +=2;
line_len = c_indent + indent * columns + columns * 2 + 1;
printf("%*c|", c_indent, *" ");
for (short int i = 1; i <= columns; i++) {
printf("%*d |", indent, i);
}
printf("\n");
line_pr(line_len);
printf("\n");
for (short int i = 0; i < rows; i++) {
printf("%*d|", c_indent, i+1);
for (short int j = 0; j < columns; j++) {
printf("%*d |", indent, array[i][j]);
}
printf("\n");
line_pr(line_len);
printf("\n");
}
for (short int i = 2; i < columns; i+=2) {
temp = array[0][i];
short int j = 0;
while (array[0][j] < temp) {
j+=2;
}
for (short int k = i - 2; k >= j; k-=2) {
array[0][k+2] = array[0][k];
}
array[0][j] = temp;
}
printf("Matrix after sorting:\n");
printf("%*c|", c_indent, *" ");
for (short int i = 1; i <= columns; i++) {
printf("%*d |", indent, i);
}
printf("\n");
line_pr(line_len);
printf("\n");
for (short int i = 0; i < rows; i++) {
printf("%*d|", c_indent, i+1);
for (short int j = 0; j < columns; j++) {
printf("%*d |", indent, array[i][j]);
}
printf("\n");
line_pr(line_len);
printf("\n");
}
return 0;
}

5
Lab_5/run.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/sh
gcc code.c -o code -lm
./code

48
Lab_5/test.py Normal file
View File

@ -0,0 +1,48 @@
#!/bin/python3
import random
import time
import os
#r, c = map(int, input("R c: ").split())
#arr_size = 100
arr = [random.randint(0, 100) for i in range(50)]
#a = [[ round((random.random() - 0.5) * 20, 3) for i in range(ar_size[0]) ] for j in range(ar_size[1])]
#a = [ [ round((random.random() - 0.5) * 20, 3) for i in range(round(ar_size[0] / 2)) ] * 2 for j in range(ar_size[1]) ]
#a[0][4] = 5
#a[0][7] = 5
print(len(arr))
for i in range(5, -1, -1):
print(f"Starting in {i}...")
time.sleep(1)
map(str, arr)
for i in arr:
for j in range(2):
os.system(f"xdotool type --delay 2 -- {i}")
os.system("xdotool key Return")
'''
for i in range(0, 10):
for j in range(3):
os.system(f"xdotool type --delay 2 -- {arr[i]}")
os.system("xdotool key Return")
# print(arr[i], end = " ")
for j in range(3):
os.system(f"xdotool type --delay 2 -- {arr[i+1]}")
os.system("xdotool key Return")
# print(arr[i+1], end = " ")
for j in range(3):
os.system(f"xdotool type --delay 2 -- {arr[i+2]}")
os.system("xdotool key Return")
# print("\n")'''

32
MKR/code.c Normal file
View File

@ -0,0 +1,32 @@
#include <stdio.h>
#include <math.h>
int main ()
{
double x, power, result;
int n, neg_one;
printf("Input n (must be an integer): ");
scanf(" %d", &n);
printf("Input x (a number with a floating decimal point): ");
scanf(" %lf", &x);
result = 1;
neg_one = -1;
for (int i = 0; i < n; i++) {
power = pow(x, 2*i + 1);
neg_one *= -1;
result *= (power*neg_one) / (2*i + 1);
}
// result_cos *= x;
printf("Y = %f\n", result);
return 0;
}

5
MKR/run.sh Normal file
View File

@ -0,0 +1,5 @@
#!/bin/sh
gcc code.c -o code -lm
./code