8, 9, 10 tasks are done
This commit is contained in:
parent
241f1bbc73
commit
509787808a
34 changed files with 1229 additions and 44 deletions
56
2025.03.21/01Ex/a.txt
Normal file
56
2025.03.21/01Ex/a.txt
Normal file
|
@ -0,0 +1,56 @@
|
|||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
|
@ -1,3 +1,56 @@
|
|||
-10 -6 -2 2
|
||||
-9 -5 -1 3
|
||||
-8 -4 0 4
|
||||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
||||
|
|
|
@ -1,3 +1,56 @@
|
|||
-10 -6 -2 2
|
||||
-9 -5 -1 3
|
||||
-8 -4 0 4
|
||||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
||||
|
|
|
@ -1,3 +1,56 @@
|
|||
-10 -6 -2 2
|
||||
-9 -5 -1 3
|
||||
-8 -4 0 4
|
||||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
||||
|
|
|
@ -12,7 +12,7 @@ TARGET = a05.exe
|
|||
OBJ = main.o array_io.o solve.o init_f.o
|
||||
|
||||
$(TARGET): $(OBJ)
|
||||
gcc $(OBJ) -o $(TARGET) -lssp
|
||||
gcc $(OBJ) -o $(TARGET) -lm -lssp
|
||||
|
||||
|
||||
main.o: main.c
|
||||
|
|
|
@ -1,4 +1,56 @@
|
|||
1 2 3 4
|
||||
4 25 2 1
|
||||
1 2 3 4
|
||||
4 3 2 1
|
||||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
#include "solve.h"
|
||||
#include <math.h>
|
||||
|
||||
#define eps 1e-17
|
||||
#include <float.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int t5_solve(double *a, int m, int n)
|
||||
{
|
||||
|
@ -14,8 +14,8 @@ int t5_solve(double *a, int m, int n)
|
|||
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; }
|
||||
|
||||
if ((sum - maximum) > DBL_EPSILON * fmax(fabs(maximum), fabs(sum))) { maximum = sum; max_j = j; }
|
||||
}
|
||||
|
||||
for (int l = max_j+1, del_j = 1; l < m*n; l++)
|
||||
|
|
|
@ -1,3 +1,56 @@
|
|||
45 3 3 45
|
||||
2 2 2 45
|
||||
3 1 1 45
|
||||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
||||
|
|
|
@ -1,3 +1,56 @@
|
|||
45 3 3 45
|
||||
2 2 2 45
|
||||
3 1 1 45
|
||||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
||||
|
|
|
@ -9,10 +9,11 @@ CFLAGS = -mfpmath=sse -fstack-protector-all -W -Wall -Wextra -Wunused \
|
|||
-Wmissing-field-initializers -Wpointer-sign -O3
|
||||
|
||||
TARGET = a08.exe
|
||||
OBJ = main.o array_io.o solve.o init_f.o
|
||||
OBJ = main.o array_io.o init_f.o
|
||||
|
||||
$(TARGET): $(OBJ)
|
||||
gcc $(OBJ) -o $(TARGET) -lssp
|
||||
$(TARGET): $(OBJ) solve.o slow.o
|
||||
gcc $(OBJ) solve.o -o $(TARGET) -lssp
|
||||
gcc $(OBJ) slow.o -o sa08.exe -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c main.c
|
||||
|
@ -20,6 +21,9 @@ main.o: main.c
|
|||
solve.o: solve.c
|
||||
gcc $(CFLAGS) -c solve.c
|
||||
|
||||
slow.o: slow.c
|
||||
gcc $(CFLAGS) -c slow.c
|
||||
|
||||
array_io.o: array_io.c
|
||||
gcc $(CFLAGS) -c array_io.c
|
||||
|
||||
|
|
|
@ -1,3 +1,56 @@
|
|||
-10 -6 -2 2
|
||||
-9 -5 -1 3
|
||||
-8 -4 0 4
|
||||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
||||
|
|
49
2025.03.21/08Ex/slow — копия.c
Normal file
49
2025.03.21/08Ex/slow — копия.c
Normal file
|
@ -0,0 +1,49 @@
|
|||
#include "solve.h"
|
||||
#include <math.h>
|
||||
|
||||
#define eps 1e-17
|
||||
|
||||
int t8_solve(double *a, int m, int n)
|
||||
{
|
||||
int max_i = 0, max_j = 0;
|
||||
double maximum = 0;
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
double sum_j = 0;
|
||||
for (int k = 0; k < m; k++)
|
||||
sum_j += fabs(a[k*n + j]);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
double sum_i = 0;
|
||||
sum_j -= fabs(a[i*n + j]);
|
||||
|
||||
for (int k = 0; k < n; k++) if (k != j)
|
||||
sum_i += fabs(a[i*n + k]);
|
||||
|
||||
if (((sum_j + sum_i) - maximum) > eps)
|
||||
maximum = (sum_j + sum_i), max_i = i, max_j = j;
|
||||
|
||||
sum_j += fabs(a[i*n + j]);
|
||||
}
|
||||
}
|
||||
|
||||
for (int l = max_j+1, del_j = 1; l < max_i*n; l++)
|
||||
{
|
||||
if (l == ((n * del_j) + max_j))
|
||||
del_j++;
|
||||
else
|
||||
a[l - del_j] = a[l];
|
||||
}
|
||||
|
||||
for (int l = (max_i+1)*n, del_j = max_i; l < m*n; l++)
|
||||
{
|
||||
if (l == ((n * (del_j + 1)) + max_j))
|
||||
del_j++;
|
||||
else
|
||||
a[l - del_j - n] = a[l];
|
||||
}
|
||||
|
||||
return max_i * n + max_j;
|
||||
}
|
44
2025.03.21/08Ex/slow.c
Normal file
44
2025.03.21/08Ex/slow.c
Normal file
|
@ -0,0 +1,44 @@
|
|||
#include "solve.h"
|
||||
#include <math.h>
|
||||
|
||||
#define eps 1e-17
|
||||
|
||||
int t8_solve(double* a, int m, int n)
|
||||
{
|
||||
int i, j, k, ind_max_i = 0, ind_max_j = 0;
|
||||
double sumstr = 0, sum, max = 0;
|
||||
int l = 0;
|
||||
if ((m == 0) || (n == 0)) return 0;
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
sumstr = 0;
|
||||
for (j = 0; j < n; j++) sumstr += fabs(a[i * n + j]);
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
sum = sumstr;
|
||||
for (k = 0; k < m; k++)
|
||||
{
|
||||
if (k == i) sum -= fabs(a[k * n + j]);
|
||||
else sum += fabs(a[k * n + j]);
|
||||
}
|
||||
if (sum > max)
|
||||
{
|
||||
max = sum;
|
||||
ind_max_i = i;
|
||||
ind_max_j = j;
|
||||
}
|
||||
}
|
||||
}
|
||||
for (i = 0; i < m; i++)
|
||||
{
|
||||
for (j = 0; j < n; j++)
|
||||
{
|
||||
if ((j != ind_max_j) && (i != ind_max_i))
|
||||
{
|
||||
a[l] = a[i * n + j];
|
||||
l++;
|
||||
}
|
||||
}
|
||||
}
|
||||
return ind_max_i * n + ind_max_j;
|
||||
}
|
|
@ -8,26 +8,48 @@ int t8_solve(double *a, int m, int n)
|
|||
int max_i = 0, max_j = 0;
|
||||
double maximum = 0;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
double sum = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
if (i != j)
|
||||
sum += fabs(a[i*n + j]);
|
||||
|
||||
if ((sum - maximum) > eps) { maximum = sum; max_i = i; }
|
||||
}
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
double sum = 0;
|
||||
double sum_j = 0;
|
||||
for (int k = 0; k < m; k++)
|
||||
sum_j += fabs(a[k*n + j]);
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
if (i != j)
|
||||
sum += fabs(a[i*n + j]);
|
||||
{
|
||||
double sum_i = 0;
|
||||
double aij = fabs(a[i*n + j]);
|
||||
sum_j -= aij;
|
||||
|
||||
if ((sum - maximum) > eps) { maximum = sum; max_j = j; }
|
||||
if (j == 0)
|
||||
{
|
||||
double num = a[i*n];
|
||||
for (int k = 1; k < n; k++)
|
||||
sum_i += fabs(a[i*n + k]);
|
||||
|
||||
a[i*n] = copysign(fabs(num) + sum_i, num);
|
||||
}
|
||||
else
|
||||
sum_i = fabs(a[i*n]) - fabs(a[i*n + j]);
|
||||
|
||||
if (((sum_j + sum_i) - maximum) > eps)
|
||||
maximum = (sum_j + sum_i), max_i = i, max_j = j;
|
||||
|
||||
sum_j += aij;
|
||||
}
|
||||
}
|
||||
|
||||
if (max_j != 0)
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
double orig = a[i*n];
|
||||
double num = fabs(orig);
|
||||
|
||||
for (int j = 1; j < n; j++)
|
||||
num -= fabs(a[i*n + j]);
|
||||
|
||||
a[i*n] = copysign(num, orig);
|
||||
}
|
||||
|
||||
for (int l = max_j+1, del_j = 1; l < max_i*n; l++)
|
||||
{
|
||||
if (l == ((n * del_j) + max_j))
|
||||
|
|
30
2025.03.21/09Ex/Makefile
Normal file
30
2025.03.21/09Ex/Makefile
Normal file
|
@ -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 = a09.exe
|
||||
OBJ = main.o solve.o array_io.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
|
56
2025.03.21/09Ex/a.txt
Normal file
56
2025.03.21/09Ex/a.txt
Normal file
|
@ -0,0 +1,56 @@
|
|||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
40
2025.03.21/09Ex/array_io.c
Normal file
40
2025.03.21/09Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
11
2025.03.21/09Ex/array_io.h
Normal file
11
2025.03.21/09Ex/array_io.h
Normal file
|
@ -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
|
30
2025.03.21/09Ex/init_f.c
Normal file
30
2025.03.21/09Ex/init_f.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "init_f.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
9
2025.03.21/09Ex/init_f.h
Normal file
9
2025.03.21/09Ex/init_f.h
Normal file
|
@ -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
|
13
2025.03.21/09Ex/io_status.h
Normal file
13
2025.03.21/09Ex/io_status.h
Normal file
|
@ -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
|
67
2025.03.21/09Ex/main.c
Normal file
67
2025.03.21/09Ex/main.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#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 = 9;
|
||||
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 = t9_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;
|
||||
}
|
45
2025.03.21/09Ex/solve.c
Normal file
45
2025.03.21/09Ex/solve.c
Normal file
|
@ -0,0 +1,45 @@
|
|||
#include "solve.h"
|
||||
#include <math.h>
|
||||
|
||||
#define eps 1e-17
|
||||
|
||||
int t9_solve(double *a, int m, int n)
|
||||
{
|
||||
int max_i = 0, max_j = 0;
|
||||
double maximum = 0;
|
||||
|
||||
for (int j = 0; j < n; j++)
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
double sum_i = 0;
|
||||
double sum_j = 0;
|
||||
double aij = a[i*n + j];
|
||||
|
||||
for (int k = 0; k < n; k++) if (k != j)
|
||||
sum_i += fabs(a[i*n + k] - aij);
|
||||
|
||||
for (int k = 0; k < m; k++) if (i != k)
|
||||
sum_j += fabs(a[k*n + j] - aij);
|
||||
|
||||
if (((sum_j + sum_i) - maximum) > eps)
|
||||
maximum = (sum_j + sum_i), max_i = i, max_j = j;
|
||||
}
|
||||
|
||||
for (int l = max_j+1, del_j = 1; l < max_i*n; l++)
|
||||
{
|
||||
if (l == ((n * del_j) + max_j))
|
||||
del_j++;
|
||||
else
|
||||
a[l - del_j] = a[l];
|
||||
}
|
||||
|
||||
for (int l = (max_i+1)*n, del_j = max_i; l < m*n; l++)
|
||||
{
|
||||
if (l == ((n * (del_j + 1)) + max_j))
|
||||
del_j++;
|
||||
else
|
||||
a[l - del_j - n] = a[l];
|
||||
}
|
||||
|
||||
return max_i * n + max_j;
|
||||
}
|
6
2025.03.21/09Ex/solve.h
Normal file
6
2025.03.21/09Ex/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
int t9_solve(double *a, int m, int n);
|
||||
|
||||
#endif
|
30
2025.03.21/10Ex/Makefile
Normal file
30
2025.03.21/10Ex/Makefile
Normal file
|
@ -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 = a10.exe
|
||||
OBJ = main.o solve.o array_io.o init_f.o
|
||||
|
||||
$(TARGET): $(OBJ)
|
||||
gcc $^ -o $@ -lm -lssp
|
||||
|
||||
main.o: main.c
|
||||
gcc $(CFLAGS) -c $^
|
||||
|
||||
solve.o: solve.c
|
||||
gcc $(CFLAGS) -c $^
|
||||
|
||||
array_io.o: array_io.c
|
||||
gcc $(CFLAGS) -c $^
|
||||
|
||||
init_f.o: init_f.c
|
||||
gcc $(CFLAGS) -c $^
|
||||
|
||||
clean:
|
||||
del *.o *.exe
|
56
2025.03.21/10Ex/a.txt
Normal file
56
2025.03.21/10Ex/a.txt
Normal file
|
@ -0,0 +1,56 @@
|
|||
-6
|
||||
5
|
||||
-23
|
||||
-3
|
||||
35
|
||||
2
|
||||
35
|
||||
5
|
||||
52
|
||||
-5
|
||||
34
|
||||
4
|
||||
-63
|
||||
6
|
||||
54
|
||||
63
|
||||
3
|
||||
-63
|
||||
6
|
||||
34
|
||||
35
|
||||
64
|
||||
3
|
||||
3
|
||||
46
|
||||
3
|
||||
6
|
||||
33
|
||||
43
|
||||
-6
|
||||
4
|
||||
2
|
||||
5
|
||||
22
|
||||
-25
|
||||
22
|
||||
5
|
||||
6
|
||||
-25
|
||||
2
|
||||
5
|
||||
23
|
||||
-5
|
||||
-2
|
||||
35
|
||||
23
|
||||
42
|
||||
-34
|
||||
-2
|
||||
52
|
||||
54
|
||||
-34
|
||||
56
|
||||
75
|
||||
-57
|
||||
7
|
40
2025.03.21/10Ex/array_io.c
Normal file
40
2025.03.21/10Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#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);
|
||||
}
|
11
2025.03.21/10Ex/array_io.h
Normal file
11
2025.03.21/10Ex/array_io.h
Normal file
|
@ -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
|
30
2025.03.21/10Ex/init_f.c
Normal file
30
2025.03.21/10Ex/init_f.c
Normal file
|
@ -0,0 +1,30 @@
|
|||
#include "init_f.h"
|
||||
#include <stdlib.h>
|
||||
|
||||
#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);
|
||||
}
|
9
2025.03.21/10Ex/init_f.h
Normal file
9
2025.03.21/10Ex/init_f.h
Normal file
|
@ -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
|
13
2025.03.21/10Ex/io_status.h
Normal file
13
2025.03.21/10Ex/io_status.h
Normal file
|
@ -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
|
67
2025.03.21/10Ex/main.c
Normal file
67
2025.03.21/10Ex/main.c
Normal file
|
@ -0,0 +1,67 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <time.h>
|
||||
#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 = 10;
|
||||
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 = t10_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;
|
||||
}
|
71
2025.03.21/10Ex/solve.c
Normal file
71
2025.03.21/10Ex/solve.c
Normal file
|
@ -0,0 +1,71 @@
|
|||
#include "solve.h"
|
||||
#include <math.h>
|
||||
#include <float.h>
|
||||
#include <stdio.h>
|
||||
|
||||
int t10_solve(double *a, int m, int n)
|
||||
{
|
||||
int min_i = 0, min_j = 0;
|
||||
double minimum = DBL_MAX;
|
||||
double inn = 1./n, inm = 1./m;
|
||||
|
||||
for (int i = 0; i < m; i++)
|
||||
{
|
||||
double cur = 0;
|
||||
double sum = 0;
|
||||
double sq_sum = 0;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
double temp = a[i*n + j];
|
||||
sum += temp;
|
||||
sq_sum += temp * temp;
|
||||
}
|
||||
|
||||
cur = sq_sum - (sum * sum) * inn;
|
||||
if ((minimum - cur) > DBL_EPSILON * fmax(fabs(minimum), fabs(cur))) {
|
||||
minimum = cur;
|
||||
min_i = i;
|
||||
}
|
||||
}
|
||||
|
||||
minimum = DBL_MAX;
|
||||
for (int j = 0; j < n; j++)
|
||||
{
|
||||
const int BS = 32;
|
||||
double cur = 0;
|
||||
double sum = 0;
|
||||
double sq_sum = 0;
|
||||
|
||||
for (int ii = 0; ii < m; ii += BS)
|
||||
for (int i = ii; i < ii + BS && i < m; i++)
|
||||
{
|
||||
double temp = a[i*n + j];
|
||||
sum += temp;
|
||||
sq_sum += temp * temp;
|
||||
}
|
||||
|
||||
cur = sq_sum - (sum * sum) * inm;
|
||||
if ((minimum - cur) > DBL_EPSILON * fmax(fabs(minimum), fabs(cur))) {
|
||||
minimum = cur;
|
||||
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;
|
||||
}
|
6
2025.03.21/10Ex/solve.h
Normal file
6
2025.03.21/10Ex/solve.h
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef SOLVE_H
|
||||
#define SOLVE_H
|
||||
|
||||
int t10_solve(double *a, int m, int n);
|
||||
|
||||
#endif
|
Loading…
Add table
Add a link
Reference in a new issue