Initial commit
This commit is contained in:
129
Lab_4/code.c
Normal file
129
Lab_4/code.c
Normal 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
5
Lab_4/run.sh
Normal file
@@ -0,0 +1,5 @@
|
||||
#!/bin/sh
|
||||
|
||||
gcc code.c -o code -lm
|
||||
|
||||
./code
|
||||
42
Lab_4/test.py
Normal file
42
Lab_4/test.py
Normal 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")
|
||||
Reference in New Issue
Block a user