diff --git a/2025.03.21/01Ex/Makefile b/2025.03.21/01Ex/Makefile index a6797ef..f76e57e 100644 --- a/2025.03.21/01Ex/Makefile +++ b/2025.03.21/01Ex/Makefile @@ -1,6 +1,6 @@ -FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ +CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ -Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ --Wignored-qualifiers -Winit-self -Wswitch-default -Wshadow -Wtype-limits \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ @@ -8,14 +8,17 @@ FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ -Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ -Wmissing-field-initializers -Wpointer-sign -O3 -a01.exe: main.o array_io.o solve.o init_f.o - gcc main.o solve.o array_io.o init_f.o -o a01.exe -lssp +TARGET = a01.exe +OBJ = main.o array_io.o solve.o init_f.o + +$(TARGET): $(OBJ) + gcc $(OBJ) -o $(TARGET) -lssp main.o: main.c gcc $(CFLAGS) -c main.c solve.o: solve.c - gcc $(FLAGS) -c solve.c + gcc $(CFLAGS) -c solve.c array_io.o: array_io.c gcc $(CFLAGS) -c array_io.c diff --git a/2025.03.21/02Ex/Makefile b/2025.03.21/02Ex/Makefile index 7f2d86e..2b446ca 100644 --- a/2025.03.21/02Ex/Makefile +++ b/2025.03.21/02Ex/Makefile @@ -1,6 +1,6 @@ -FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ +CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ -Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ --Wignored-qualifiers -Winit-self -Wswitch-default -Wshadow -Wtype-limits \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ @@ -8,14 +8,17 @@ FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ -Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ -Wmissing-field-initializers -Wpointer-sign -O3 -a02.exe: main.o array_io.o solve.o init_f.o - gcc main.o solve.o array_io.o init_f.o -o a02.exe -lssp +TARGET = a02.exe +OBJ = main.o array_io.o solve.o init_f.o + +$(TARGET): $(OBJ) + gcc $(OBJ) -o $(TARGET) -lssp main.o: main.c gcc $(CFLAGS) -c main.c solve.o: solve.c - gcc $(FLAGS) -c solve.c + gcc $(CFLAGS) -c solve.c array_io.o: array_io.c gcc $(CFLAGS) -c array_io.c diff --git a/2025.03.21/03Ex/Makefile b/2025.03.21/03Ex/Makefile index 1cbcf28..600251c 100644 --- a/2025.03.21/03Ex/Makefile +++ b/2025.03.21/03Ex/Makefile @@ -1,6 +1,6 @@ -FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ +CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ -Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ --Wignored-qualifiers -Winit-self -Wswitch-default -Wshadow -Wtype-limits \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ -Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ -Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ -Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ @@ -8,14 +8,17 @@ FLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ -Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ -Wmissing-field-initializers -Wpointer-sign -O3 -a03.exe: main.o array_io.o solve.o init_f.o - gcc main.o solve.o array_io.o init_f.o -o a03.exe -lssp +TARGET = a03.exe +OBJ = main.o array_io.o solve.o init_f.o + +$(TARGET): $(OBJ) + gcc $(OBJ) -o $(TARGET) -lssp main.o: main.c gcc $(CFLAGS) -c main.c solve.o: solve.c - gcc $(FLAGS) -c solve.c + gcc $(CFLAGS) -c solve.c array_io.o: array_io.c gcc $(CFLAGS) -c array_io.c diff --git a/2025.03.21/04Ex/Makefile b/2025.03.21/04Ex/Makefile new file mode 100644 index 0000000..b6053af --- /dev/null +++ b/2025.03.21/04Ex/Makefile @@ -0,0 +1,30 @@ +CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign -O3 + +TARGET = a04.exe +OBJ = main.o array_io.o solve.o init_f.o + +$(TARGET): $(OBJ) + gcc $(OBJ) -o $(TARGET) -lssp + +main.o: main.c + gcc $(CFLAGS) -c main.c + +solve.o: solve.c + gcc $(CFLAGS) -c solve.c + +array_io.o: array_io.c + gcc $(CFLAGS) -c array_io.c + +init_f.o: init_f.c + gcc $(CFLAGS) -c init_f.c + +clean: + del *.o *.exe diff --git a/2025.03.21/04Ex/a.txt b/2025.03.21/04Ex/a.txt new file mode 100644 index 0000000..db76d29 --- /dev/null +++ b/2025.03.21/04Ex/a.txt @@ -0,0 +1,3 @@ +-10 -6 -2 2 +-9 -5 -1 3 +-8 -4 0 4 diff --git a/2025.03.21/04Ex/array_io.c b/2025.03.21/04Ex/array_io.c new file mode 100644 index 0000000..f91fbac --- /dev/null +++ b/2025.03.21/04Ex/array_io.c @@ -0,0 +1,40 @@ +#include +#include "array_io.h" + +io_status read_matrix(double *a, int m, int n, const char *name) +{ + int i, j; + FILE *fp; + if (!(fp = fopen(name, "r"))) return ERROR_OPEN; + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + if (fscanf(fp, "%lf", a + i * n + j) != 1) + {fclose(fp); return ERROR_READ;} + fclose(fp); + return SUCCESS; +} + +void print_matrix(const double *a, int m, int n, int p) +{ + int mp = (m > p ? p : m); + int np = (n > p ? p : n); + int i, j; + + for (i = 0; i < mp; i++) + { + for (j = 0; j < np; j++) + printf(" %10.3e", a[i * n + j]); + printf("\n"); + } +} + +void init_matrix(double *a, int m, int n, int k) +{ + double (*q)(int, int, int, int); + double (*f[])(int, int, int, int) = {f1, f2, f3, f4}; + int i, j; + q = f[k-1]; + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + a[i * n + j] = q(m, n, i+1, j+1); +} diff --git a/2025.03.21/04Ex/array_io.h b/2025.03.21/04Ex/array_io.h new file mode 100644 index 0000000..c74c9a0 --- /dev/null +++ b/2025.03.21/04Ex/array_io.h @@ -0,0 +1,11 @@ +#ifndef ARRAY_IO_H +#define ARRAY_IO_H + +#include "io_status.h" +#include "init_f.h" + +io_status read_matrix(double *a, int m, int n, const char *name); +void print_matrix(const double *a, int m, int n, int p); +void init_matrix(double *a, int m, int n, int k); + +#endif diff --git a/2025.03.21/04Ex/init_f.c b/2025.03.21/04Ex/init_f.c new file mode 100644 index 0000000..def39b7 --- /dev/null +++ b/2025.03.21/04Ex/init_f.c @@ -0,0 +1,30 @@ +#include "init_f.h" +#include + +#define MAX(n, m) (n < m ? m : n) + +double f1(int m, int n, int i, int j) +{ + return MAX(n, m) - MAX(i, j) + 1; +} + +double f2(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return MAX(i, j); +} + +double f3(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return abs(i - j); +} + +double f4(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return 1./(i+j-1); +} diff --git a/2025.03.21/04Ex/init_f.h b/2025.03.21/04Ex/init_f.h new file mode 100644 index 0000000..40e985b --- /dev/null +++ b/2025.03.21/04Ex/init_f.h @@ -0,0 +1,9 @@ +#ifndef INIT_F_H +#define INIT_F_H + +double f1(int m, int n, int i, int j); +double f2(int m, int n, int i, int j); +double f3(int m, int n, int i, int j); +double f4(int m, int n, int i, int j); + +#endif diff --git a/2025.03.21/04Ex/io_status.h b/2025.03.21/04Ex/io_status.h new file mode 100644 index 0000000..420a981 --- /dev/null +++ b/2025.03.21/04Ex/io_status.h @@ -0,0 +1,13 @@ +#ifndef IO_STATUS_H +#define IO_STATUS_H + +#define LEN 1234 + +typedef enum _io_status +{ + SUCCESS, + ERROR_OPEN, + ERROR_READ +} io_status; + +#endif diff --git a/2025.03.21/04Ex/main.c b/2025.03.21/04Ex/main.c new file mode 100644 index 0000000..3049b78 --- /dev/null +++ b/2025.03.21/04Ex/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include "array_io.h" +#include "io_status.h" +#include "solve.h" + +/* ./a.out m n p k [filename] */ +int main(int argc, char *argv[]) +{ + double t, *a; + int m, n, p, k, res, task = 4; + char *name = 0; + + if (!((argc == 5 || argc == 6) && + sscanf(argv[1], "%d", &m) == 1 && + sscanf(argv[2], "%d", &n) == 1 && + sscanf(argv[3], "%d", &p) == 1 && + sscanf(argv[4], "%d", &k) == 1 && + k >= 0 && k <= 4 && (!(k == 0 && argc != 6)))) + { + printf("Usage: %s m n p k [filename]\n", argv[0]); + return 1; + } + if (argc == 6) name = argv[5]; + + a = (double *)malloc(m * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + + if (name) + { /* из файла */ + io_status ret; + ret = read_matrix(a, m, n, name); + do { + switch (ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Cannot open %s\n", name); + break; + case ERROR_READ: + printf("Cannot read %s\n", name); + } + free(a); + return 3; + } while (0); + } else init_matrix(a, m, n, k); + + printf("Matrix:\n"); + print_matrix(a, m, n, p); + + t = clock(); + res = t4_solve(a, m, n); + t = (clock() - t) / CLOCKS_PER_SEC; + + printf("New matrix:\n"); + print_matrix(a, m - 1, n - 1, p); + printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t); + + free(a); + return 0; +} diff --git a/2025.03.21/04Ex/solve.c b/2025.03.21/04Ex/solve.c new file mode 100644 index 0000000..373666a --- /dev/null +++ b/2025.03.21/04Ex/solve.c @@ -0,0 +1,46 @@ +#include "solve.h" +#include + +#define eps 1e-17 + +int t4_solve(double *a, int m, int n) +{ + int min_i = 0, min_j = 0; + double minimum = -1; + + for (int i = 0; i < m; i++) + { + double sum = 0; + for (int j = 0; j < n; j++) + sum += fabs(a[i*n + j]); + + if ((minimum - sum) > eps || i == 0) { minimum = sum; min_i = i; } + } + + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int i = 0; i < m; i++) + sum += fabs(a[i*n + j]); + + if ((minimum - sum) > eps || j == 0) { minimum = sum; min_j = j; } + } + + for (int l = min_j+1, del_j = 1; l < min_i*n; l++) + { + if (l == ((n * del_j) + min_j)) + del_j++; + else + a[l - del_j] = a[l]; + } + + for (int l = (min_i+1)*n, del_j = min_i; l < m*n; l++) + { + if (l == ((n * (del_j + 1)) + min_j)) + del_j++; + else + a[l - del_j - n] = a[l]; + } + + return min_i * n + min_j; +} diff --git a/2025.03.21/04Ex/solve.h b/2025.03.21/04Ex/solve.h new file mode 100644 index 0000000..4ba30db --- /dev/null +++ b/2025.03.21/04Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t4_solve(double *a, int m, int n); + +#endif diff --git a/2025.03.21/05Ex/Makefile b/2025.03.21/05Ex/Makefile new file mode 100644 index 0000000..d26fd61 --- /dev/null +++ b/2025.03.21/05Ex/Makefile @@ -0,0 +1,31 @@ +CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign -O3 + +TARGET = a05.exe +OBJ = main.o array_io.o solve.o init_f.o + +$(TARGET): $(OBJ) + gcc $(OBJ) -o $(TARGET) -lssp + + +main.o: main.c + gcc $(CFLAGS) -c main.c + +solve.o: solve.c + gcc $(CFLAGS) -c solve.c + +array_io.o: array_io.c + gcc $(CFLAGS) -c array_io.c + +init_f.o: init_f.c + gcc $(CFLAGS) -c init_f.c + +clean: + del *.o *.exe diff --git a/2025.03.21/05Ex/a.txt b/2025.03.21/05Ex/a.txt new file mode 100644 index 0000000..b186a40 --- /dev/null +++ b/2025.03.21/05Ex/a.txt @@ -0,0 +1,4 @@ +1 2 3 4 +4 25 2 1 +1 2 3 4 +4 3 2 1 diff --git a/2025.03.21/05Ex/array_io.c b/2025.03.21/05Ex/array_io.c new file mode 100644 index 0000000..f91fbac --- /dev/null +++ b/2025.03.21/05Ex/array_io.c @@ -0,0 +1,40 @@ +#include +#include "array_io.h" + +io_status read_matrix(double *a, int m, int n, const char *name) +{ + int i, j; + FILE *fp; + if (!(fp = fopen(name, "r"))) return ERROR_OPEN; + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + if (fscanf(fp, "%lf", a + i * n + j) != 1) + {fclose(fp); return ERROR_READ;} + fclose(fp); + return SUCCESS; +} + +void print_matrix(const double *a, int m, int n, int p) +{ + int mp = (m > p ? p : m); + int np = (n > p ? p : n); + int i, j; + + for (i = 0; i < mp; i++) + { + for (j = 0; j < np; j++) + printf(" %10.3e", a[i * n + j]); + printf("\n"); + } +} + +void init_matrix(double *a, int m, int n, int k) +{ + double (*q)(int, int, int, int); + double (*f[])(int, int, int, int) = {f1, f2, f3, f4}; + int i, j; + q = f[k-1]; + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + a[i * n + j] = q(m, n, i+1, j+1); +} diff --git a/2025.03.21/05Ex/array_io.h b/2025.03.21/05Ex/array_io.h new file mode 100644 index 0000000..c74c9a0 --- /dev/null +++ b/2025.03.21/05Ex/array_io.h @@ -0,0 +1,11 @@ +#ifndef ARRAY_IO_H +#define ARRAY_IO_H + +#include "io_status.h" +#include "init_f.h" + +io_status read_matrix(double *a, int m, int n, const char *name); +void print_matrix(const double *a, int m, int n, int p); +void init_matrix(double *a, int m, int n, int k); + +#endif diff --git a/2025.03.21/05Ex/init_f.c b/2025.03.21/05Ex/init_f.c new file mode 100644 index 0000000..def39b7 --- /dev/null +++ b/2025.03.21/05Ex/init_f.c @@ -0,0 +1,30 @@ +#include "init_f.h" +#include + +#define MAX(n, m) (n < m ? m : n) + +double f1(int m, int n, int i, int j) +{ + return MAX(n, m) - MAX(i, j) + 1; +} + +double f2(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return MAX(i, j); +} + +double f3(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return abs(i - j); +} + +double f4(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return 1./(i+j-1); +} diff --git a/2025.03.21/05Ex/init_f.h b/2025.03.21/05Ex/init_f.h new file mode 100644 index 0000000..40e985b --- /dev/null +++ b/2025.03.21/05Ex/init_f.h @@ -0,0 +1,9 @@ +#ifndef INIT_F_H +#define INIT_F_H + +double f1(int m, int n, int i, int j); +double f2(int m, int n, int i, int j); +double f3(int m, int n, int i, int j); +double f4(int m, int n, int i, int j); + +#endif diff --git a/2025.03.21/05Ex/io_status.h b/2025.03.21/05Ex/io_status.h new file mode 100644 index 0000000..420a981 --- /dev/null +++ b/2025.03.21/05Ex/io_status.h @@ -0,0 +1,13 @@ +#ifndef IO_STATUS_H +#define IO_STATUS_H + +#define LEN 1234 + +typedef enum _io_status +{ + SUCCESS, + ERROR_OPEN, + ERROR_READ +} io_status; + +#endif diff --git a/2025.03.21/05Ex/main.c b/2025.03.21/05Ex/main.c new file mode 100644 index 0000000..585b48a --- /dev/null +++ b/2025.03.21/05Ex/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include "array_io.h" +#include "io_status.h" +#include "solve.h" + +/* ./a.out m n p k [filename] */ +int main(int argc, char *argv[]) +{ + double t, *a; + int m, n, p, k, res, task = 5; + char *name = 0; + + if (!((argc == 5 || argc == 6) && + sscanf(argv[1], "%d", &m) == 1 && + sscanf(argv[2], "%d", &n) == 1 && + sscanf(argv[3], "%d", &p) == 1 && + sscanf(argv[4], "%d", &k) == 1 && + k >= 0 && k <= 4 && (!(k == 0 && argc != 6)))) + { + printf("Usage: %s m n p k [filename]\n", argv[0]); + return 1; + } + if (argc == 6) name = argv[5]; + + a = (double *)malloc(m * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + + if (name) + { /* из файла */ + io_status ret; + ret = read_matrix(a, m, n, name); + do { + switch (ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Cannot open %s\n", name); + break; + case ERROR_READ: + printf("Cannot read %s\n", name); + } + free(a); + return 3; + } while (0); + } else init_matrix(a, m, n, k); + + printf("Matrix:\n"); + print_matrix(a, m, n, p); + + t = clock(); + res = t5_solve(a, m, n); + t = (clock() - t) / CLOCKS_PER_SEC; + + printf("New matrix:\n"); + print_matrix(a, m, n - 1, p); + printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t); + + free(a); + return 0; +} diff --git a/2025.03.21/05Ex/solve.c b/2025.03.21/05Ex/solve.c new file mode 100644 index 0000000..c897c63 --- /dev/null +++ b/2025.03.21/05Ex/solve.c @@ -0,0 +1,30 @@ +#include "solve.h" +#include + +#define eps 1e-17 + +int t5_solve(double *a, int m, int n) +{ + int max_j = 0; + double maximum = 0; + + for (int j = 0; j < n; j++) + { + double sum = 0; + for (int i = 0; i < m; i++) + if (i != j) + sum += fabs(a[i*n + j]); + + if ((sum - maximum) > eps || j == 0) { maximum = sum; max_j = j; } + } + + for (int l = max_j+1, del_j = 1; l < m*n; l++) + { + if (l == ((n * del_j) + max_j)) + del_j++; + else + a[l - del_j] = a[l]; + } + + return max_j + 1; +} diff --git a/2025.03.21/05Ex/solve.h b/2025.03.21/05Ex/solve.h new file mode 100644 index 0000000..4d822e3 --- /dev/null +++ b/2025.03.21/05Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t5_solve(double *a, int m, int n); + +#endif diff --git a/2025.03.21/06Ex/Makefile b/2025.03.21/06Ex/Makefile new file mode 100644 index 0000000..cb45184 --- /dev/null +++ b/2025.03.21/06Ex/Makefile @@ -0,0 +1,31 @@ +CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \ +-Wempty-body -Wlogical-op -Wold-style-declaration -Wmissing-parameter-type \ +-Wignored-qualifiers -Winit-self -Wshadow -Wtype-limits \ +-Wpointer-arith -Wformat-security -Wmissing-format-attribute -Wformat=1 \ +-Wdeclaration-after-statement -Wbad-function-cast -Wnested-externs \ +-Wmissing-prototypes -Wmissing-declarations -Wold-style-definition \ +-Wcast-align -Werror -pedantic -pedantic-errors -Wfloat-equal \ +-Wwrite-strings -Wno-long-long -std=gnu99 -Wstrict-prototypes \ +-Wmissing-field-initializers -Wpointer-sign -O3 + +TARGET = a06.exe +OBJ = main.o array_io.o solve.o init_f.o + +$(TARGET): $(OBJ) + gcc $(OBJ) -o $(TARGET) -lssp + + +main.o: main.c + gcc $(CFLAGS) -c main.c + +solve.o: solve.c + gcc $(CFLAGS) -c solve.c + +array_io.o: array_io.c + gcc $(CFLAGS) -c array_io.c + +init_f.o: init_f.c + gcc $(CFLAGS) -c init_f.c + +clean: + del *.o *.exe diff --git a/2025.03.21/06Ex/a.txt b/2025.03.21/06Ex/a.txt new file mode 100644 index 0000000..512d6ab --- /dev/null +++ b/2025.03.21/06Ex/a.txt @@ -0,0 +1,3 @@ +45 3 3 45 +2 2 2 45 +3 1 1 45 diff --git a/2025.03.21/06Ex/array_io.c b/2025.03.21/06Ex/array_io.c new file mode 100644 index 0000000..f91fbac --- /dev/null +++ b/2025.03.21/06Ex/array_io.c @@ -0,0 +1,40 @@ +#include +#include "array_io.h" + +io_status read_matrix(double *a, int m, int n, const char *name) +{ + int i, j; + FILE *fp; + if (!(fp = fopen(name, "r"))) return ERROR_OPEN; + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + if (fscanf(fp, "%lf", a + i * n + j) != 1) + {fclose(fp); return ERROR_READ;} + fclose(fp); + return SUCCESS; +} + +void print_matrix(const double *a, int m, int n, int p) +{ + int mp = (m > p ? p : m); + int np = (n > p ? p : n); + int i, j; + + for (i = 0; i < mp; i++) + { + for (j = 0; j < np; j++) + printf(" %10.3e", a[i * n + j]); + printf("\n"); + } +} + +void init_matrix(double *a, int m, int n, int k) +{ + double (*q)(int, int, int, int); + double (*f[])(int, int, int, int) = {f1, f2, f3, f4}; + int i, j; + q = f[k-1]; + for (i = 0; i < m; i++) + for (j = 0; j < n; j++) + a[i * n + j] = q(m, n, i+1, j+1); +} diff --git a/2025.03.21/06Ex/array_io.h b/2025.03.21/06Ex/array_io.h new file mode 100644 index 0000000..c74c9a0 --- /dev/null +++ b/2025.03.21/06Ex/array_io.h @@ -0,0 +1,11 @@ +#ifndef ARRAY_IO_H +#define ARRAY_IO_H + +#include "io_status.h" +#include "init_f.h" + +io_status read_matrix(double *a, int m, int n, const char *name); +void print_matrix(const double *a, int m, int n, int p); +void init_matrix(double *a, int m, int n, int k); + +#endif diff --git a/2025.03.21/06Ex/init_f.c b/2025.03.21/06Ex/init_f.c new file mode 100644 index 0000000..def39b7 --- /dev/null +++ b/2025.03.21/06Ex/init_f.c @@ -0,0 +1,30 @@ +#include "init_f.h" +#include + +#define MAX(n, m) (n < m ? m : n) + +double f1(int m, int n, int i, int j) +{ + return MAX(n, m) - MAX(i, j) + 1; +} + +double f2(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return MAX(i, j); +} + +double f3(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return abs(i - j); +} + +double f4(int m, int n, int i, int j) +{ + (void)n; + (void)m; + return 1./(i+j-1); +} diff --git a/2025.03.21/06Ex/init_f.h b/2025.03.21/06Ex/init_f.h new file mode 100644 index 0000000..40e985b --- /dev/null +++ b/2025.03.21/06Ex/init_f.h @@ -0,0 +1,9 @@ +#ifndef INIT_F_H +#define INIT_F_H + +double f1(int m, int n, int i, int j); +double f2(int m, int n, int i, int j); +double f3(int m, int n, int i, int j); +double f4(int m, int n, int i, int j); + +#endif diff --git a/2025.03.21/06Ex/io_status.h b/2025.03.21/06Ex/io_status.h new file mode 100644 index 0000000..420a981 --- /dev/null +++ b/2025.03.21/06Ex/io_status.h @@ -0,0 +1,13 @@ +#ifndef IO_STATUS_H +#define IO_STATUS_H + +#define LEN 1234 + +typedef enum _io_status +{ + SUCCESS, + ERROR_OPEN, + ERROR_READ +} io_status; + +#endif diff --git a/2025.03.21/06Ex/main.c b/2025.03.21/06Ex/main.c new file mode 100644 index 0000000..79e7a8c --- /dev/null +++ b/2025.03.21/06Ex/main.c @@ -0,0 +1,67 @@ +#include +#include +#include +#include "array_io.h" +#include "io_status.h" +#include "solve.h" + +/* ./a.out m n p k [filename] */ +int main(int argc, char *argv[]) +{ + double t, *a; + int m, n, p, k, res, task = 6; + char *name = 0; + + if (!((argc == 5 || argc == 6) && + sscanf(argv[1], "%d", &m) == 1 && + sscanf(argv[2], "%d", &n) == 1 && + sscanf(argv[3], "%d", &p) == 1 && + sscanf(argv[4], "%d", &k) == 1 && + k >= 0 && k <= 4 && (!(k == 0 && argc != 6)))) + { + printf("Usage: %s m n p k [filename]\n", argv[0]); + return 1; + } + if (argc == 6) name = argv[5]; + + a = (double *)malloc(m * n * sizeof(double)); + if (!a) + { + printf("Not enough memory\n"); + return 2; + } + + if (name) + { /* из файла */ + io_status ret; + ret = read_matrix(a, m, n, name); + do { + switch (ret) + { + case SUCCESS: + continue; + case ERROR_OPEN: + printf("Cannot open %s\n", name); + break; + case ERROR_READ: + printf("Cannot read %s\n", name); + } + free(a); + return 3; + } while (0); + } else init_matrix(a, m, n, k); + + printf("Matrix:\n"); + print_matrix(a, m, n, p); + + t = clock(); + res = t6_solve(a, m, n); + t = (clock() - t) / CLOCKS_PER_SEC; + + printf("New matrix:\n"); + print_matrix(a, m, n - 1, p); + printf ("%s : Task = %d Result = %d Elapsed = %.2f\n", argv[0], task, res, t); + + free(a); + return 0; +} diff --git a/2025.03.21/06Ex/solve.c b/2025.03.21/06Ex/solve.c new file mode 100644 index 0000000..c9b421b --- /dev/null +++ b/2025.03.21/06Ex/solve.c @@ -0,0 +1,30 @@ +#include "solve.h" +#include + +#define eps 1e-17 + +int t6_solve(double *a, int m, int n) +{ + int max_j = 0, mn = (n < m) ? n : m; + double maximum = 0; + + for (int j = 0; j < mn; j++) + { + double sum = 0; + for (int i = 0; i < m; i++) + if (i != j) + sum += fabs(a[i*n + j] - a[j*n + j]); + + if ((sum - maximum) > eps || j == 0) { maximum = sum; max_j = j; } + } + + for (int l = max_j+1, del_j = 1; l < m*n; l++) + { + if (l == ((n * del_j) + max_j)) + del_j++; + else + a[l - del_j] = a[l]; + } + + return max_j + 1; +} diff --git a/2025.03.21/06Ex/solve.h b/2025.03.21/06Ex/solve.h new file mode 100644 index 0000000..50287f3 --- /dev/null +++ b/2025.03.21/06Ex/solve.h @@ -0,0 +1,6 @@ +#ifndef SOLVE_H +#define SOLVE_H + +int t6_solve(double *a, int m, int n); + +#endif