From 8175cbbdbf6b7a01c1569949510603073e55525d Mon Sep 17 00:00:00 2001 From: rhinemann Date: Sat, 9 Mar 2024 17:32:59 +0200 Subject: [PATCH] Initial commit --- Lab_2/.idea/.gitignore | 8 +++ Lab_2/.idea/Lab_2.iml | 2 + Lab_2/.idea/misc.xml | 4 ++ Lab_2/.idea/modules.xml | 8 +++ Lab_2/Lab_2_loops.c | 49 +++++++++++++++ Lab_2/Lab_2_mem.c | 34 +++++++++++ Lab_2/range.sh | 11 ++++ Lab_2/test.sh | 6 ++ Lab_3/code.c | 90 ++++++++++++++++++++++++++++ Lab_3/run.sh | 4 ++ Lab_4/code.c | 129 ++++++++++++++++++++++++++++++++++++++++ Lab_4/run.sh | 5 ++ Lab_4/test.py | 42 +++++++++++++ Lab_5/code.c | 109 +++++++++++++++++++++++++++++++++ Lab_5/run.sh | 5 ++ Lab_5/test.py | 48 +++++++++++++++ MKR/code.c | 32 ++++++++++ MKR/run.sh | 5 ++ 18 files changed, 591 insertions(+) create mode 100644 Lab_2/.idea/.gitignore create mode 100644 Lab_2/.idea/Lab_2.iml create mode 100644 Lab_2/.idea/misc.xml create mode 100644 Lab_2/.idea/modules.xml create mode 100644 Lab_2/Lab_2_loops.c create mode 100644 Lab_2/Lab_2_mem.c create mode 100644 Lab_2/range.sh create mode 100644 Lab_2/test.sh create mode 100644 Lab_3/code.c create mode 100644 Lab_3/run.sh create mode 100644 Lab_4/code.c create mode 100644 Lab_4/run.sh create mode 100644 Lab_4/test.py create mode 100644 Lab_5/code.c create mode 100644 Lab_5/run.sh create mode 100644 Lab_5/test.py create mode 100644 MKR/code.c create mode 100644 MKR/run.sh diff --git a/Lab_2/.idea/.gitignore b/Lab_2/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/Lab_2/.idea/.gitignore @@ -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 diff --git a/Lab_2/.idea/Lab_2.iml b/Lab_2/.idea/Lab_2.iml new file mode 100644 index 0000000..f08604b --- /dev/null +++ b/Lab_2/.idea/Lab_2.iml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/Lab_2/.idea/misc.xml b/Lab_2/.idea/misc.xml new file mode 100644 index 0000000..79b3c94 --- /dev/null +++ b/Lab_2/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/Lab_2/.idea/modules.xml b/Lab_2/.idea/modules.xml new file mode 100644 index 0000000..b5c98d2 --- /dev/null +++ b/Lab_2/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/Lab_2/Lab_2_loops.c b/Lab_2/Lab_2_loops.c new file mode 100644 index 0000000..cdde99c --- /dev/null +++ b/Lab_2/Lab_2_loops.c @@ -0,0 +1,49 @@ +#include +#include + +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; +} diff --git a/Lab_2/Lab_2_mem.c b/Lab_2/Lab_2_mem.c new file mode 100644 index 0000000..f691ffb --- /dev/null +++ b/Lab_2/Lab_2_mem.c @@ -0,0 +1,34 @@ +#include +#include + +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; +} diff --git a/Lab_2/range.sh b/Lab_2/range.sh new file mode 100644 index 0000000..c4ddc9b --- /dev/null +++ b/Lab_2/range.sh @@ -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 diff --git a/Lab_2/test.sh b/Lab_2/test.sh new file mode 100644 index 0000000..c7fd2eb --- /dev/null +++ b/Lab_2/test.sh @@ -0,0 +1,6 @@ +#!/bin/bash + +for i in {1..10} +do + echo $((5**$i)); +done diff --git a/Lab_3/code.c b/Lab_3/code.c new file mode 100644 index 0000000..222fb3a --- /dev/null +++ b/Lab_3/code.c @@ -0,0 +1,90 @@ +#include + +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; +} diff --git a/Lab_3/run.sh b/Lab_3/run.sh new file mode 100644 index 0000000..16e028d --- /dev/null +++ b/Lab_3/run.sh @@ -0,0 +1,4 @@ +#!/bin/sh + +gcc code.c -o code -l ncurses +./code diff --git a/Lab_4/code.c b/Lab_4/code.c new file mode 100644 index 0000000..f6f0e59 --- /dev/null +++ b/Lab_4/code.c @@ -0,0 +1,129 @@ +#include +#include +#include + +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"); + } + } + } +} diff --git a/Lab_4/run.sh b/Lab_4/run.sh new file mode 100644 index 0000000..d114e14 --- /dev/null +++ b/Lab_4/run.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +gcc code.c -o code -lm + +./code diff --git a/Lab_4/test.py b/Lab_4/test.py new file mode 100644 index 0000000..083ed7c --- /dev/null +++ b/Lab_4/test.py @@ -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") diff --git a/Lab_5/code.c b/Lab_5/code.c new file mode 100644 index 0000000..ca8f7d1 --- /dev/null +++ b/Lab_5/code.c @@ -0,0 +1,109 @@ +#include +#include + +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; +} diff --git a/Lab_5/run.sh b/Lab_5/run.sh new file mode 100644 index 0000000..d114e14 --- /dev/null +++ b/Lab_5/run.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +gcc code.c -o code -lm + +./code diff --git a/Lab_5/test.py b/Lab_5/test.py new file mode 100644 index 0000000..c69a23a --- /dev/null +++ b/Lab_5/test.py @@ -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")''' diff --git a/MKR/code.c b/MKR/code.c new file mode 100644 index 0000000..1c79715 --- /dev/null +++ b/MKR/code.c @@ -0,0 +1,32 @@ +#include +#include + +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; +} diff --git a/MKR/run.sh b/MKR/run.sh new file mode 100644 index 0000000..d114e14 --- /dev/null +++ b/MKR/run.sh @@ -0,0 +1,5 @@ +#!/bin/sh + +gcc code.c -o code -lm + +./code