All tasks are done!
This commit is contained in:
parent
ab0fbf59d0
commit
48b359f34c
131 changed files with 2713 additions and 438 deletions
|
@ -1,5 +1,5 @@
|
||||||
#include "init_f.h"
|
#include "init_f.h"
|
||||||
#include <math.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define MAX(n, m) (n < m ? m : n)
|
#define MAX(n, m) (n < m ? m : n)
|
||||||
|
|
|
@ -7,8 +7,7 @@ typedef enum _io_status
|
||||||
{
|
{
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
ERROR_OPEN,
|
ERROR_OPEN,
|
||||||
ERROR_READ,
|
ERROR_READ
|
||||||
ERROR_MEM
|
|
||||||
} io_status;
|
} io_status;
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -51,7 +51,7 @@ int main(int argc, char *argv[])
|
||||||
} while (0);
|
} while (0);
|
||||||
} else init_matrix(a, n, m, k);
|
} else init_matrix(a, n, m, k);
|
||||||
|
|
||||||
printf("Initial matrix:\n");
|
printf("Matrix:\n");
|
||||||
print_matrix(a, n, m, p);
|
print_matrix(a, n, m, p);
|
||||||
|
|
||||||
t = clock();
|
t = clock();
|
|
@ -1,5 +1,5 @@
|
||||||
#include "init_f.h"
|
#include "init_f.h"
|
||||||
#include <math.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define MAX(n, m) (n < m ? m : n)
|
#define MAX(n, m) (n < m ? m : n)
|
||||||
|
|
|
@ -7,8 +7,7 @@ typedef enum _io_status
|
||||||
{
|
{
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
ERROR_OPEN,
|
ERROR_OPEN,
|
||||||
ERROR_READ,
|
ERROR_READ
|
||||||
ERROR_MEM
|
|
||||||
} io_status;
|
} io_status;
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -51,7 +51,7 @@ int main(int argc, char *argv[])
|
||||||
} while (0);
|
} while (0);
|
||||||
} else init_matrix(a, n, m, k);
|
} else init_matrix(a, n, m, k);
|
||||||
|
|
||||||
printf("Initial matrix:\n");
|
printf("Matrix:\n");
|
||||||
print_matrix(a, n, m, p);
|
print_matrix(a, n, m, p);
|
||||||
|
|
||||||
t = clock();
|
t = clock();
|
|
@ -1,5 +1,5 @@
|
||||||
#include "init_f.h"
|
#include "init_f.h"
|
||||||
#include <math.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define MAX(n, m) (n < m ? m : n)
|
#define MAX(n, m) (n < m ? m : n)
|
||||||
|
|
|
@ -7,8 +7,7 @@ typedef enum _io_status
|
||||||
{
|
{
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
ERROR_OPEN,
|
ERROR_OPEN,
|
||||||
ERROR_READ,
|
ERROR_READ
|
||||||
ERROR_MEM
|
|
||||||
} io_status;
|
} io_status;
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -140,7 +140,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
else init_matrix(x, m, 1, k_x);
|
else init_matrix(x, m, 1, k_x);
|
||||||
|
|
||||||
printf("Initial matrix A:\n");
|
printf("Matrix:\n");
|
||||||
print_matrix(a, n, m, p);
|
print_matrix(a, n, m, p);
|
||||||
printf("Initial vector b:\n");
|
printf("Initial vector b:\n");
|
||||||
print_matrix(b, n, 1, p);
|
print_matrix(b, n, 1, p);
|
|
@ -1,5 +1,5 @@
|
||||||
#include "init_f.h"
|
#include "init_f.h"
|
||||||
#include <math.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#define MAX(n, m) (n < m ? m : n)
|
#define MAX(n, m) (n < m ? m : n)
|
||||||
|
|
|
@ -7,8 +7,7 @@ typedef enum _io_status
|
||||||
{
|
{
|
||||||
SUCCESS,
|
SUCCESS,
|
||||||
ERROR_OPEN,
|
ERROR_OPEN,
|
||||||
ERROR_READ,
|
ERROR_READ
|
||||||
ERROR_MEM
|
|
||||||
} io_status;
|
} io_status;
|
||||||
|
|
||||||
#endif
|
#endif
|
|
@ -140,7 +140,7 @@ int main(int argc, char *argv[])
|
||||||
}
|
}
|
||||||
else init_matrix(x, m, 1, k_x);
|
else init_matrix(x, m, 1, k_x);
|
||||||
|
|
||||||
printf("Initial matrix A:\n");
|
printf("Matrix:\n");
|
||||||
print_matrix(a, n, m, p);
|
print_matrix(a, n, m, p);
|
||||||
printf("Initial vector b:\n");
|
printf("Initial vector b:\n");
|
||||||
print_matrix(b, n, 1, p);
|
print_matrix(b, n, 1, p);
|
30
2025.03.14/05Ex/init_f.c
Normal file
30
2025.03.14/05Ex/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 n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
13
2025.03.14/05Ex/io_status.h
Normal file
13
2025.03.14/05Ex/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
|
|
@ -92,13 +92,13 @@ int main(int argc, char *argv[])
|
||||||
} while (0);
|
} while (0);
|
||||||
} else init_matrix(b, m, n, k_b);
|
} else init_matrix(b, m, n, k_b);
|
||||||
|
|
||||||
printf("Initial matrix A:\n");
|
printf("Matrix A:\n");
|
||||||
print_matrix(a, n, m, p);
|
print_matrix(a, n, m, p);
|
||||||
printf("Initial matrix b:\n");
|
printf("Matrix B:\n");
|
||||||
print_matrix(b, m, n, p);
|
print_matrix(b, m, n, p);
|
||||||
|
|
||||||
t = clock();
|
t = clock();
|
||||||
t5_solve(a, b, n, m);
|
res = t5_solve(a, b, n, m);
|
||||||
t = (clock() - t) / CLOCKS_PER_SEC;
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
5
2025.03.14/06Ex/a.txt
Normal file
5
2025.03.14/06Ex/a.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
1 2 3 4
|
||||||
|
4 3 2 1
|
||||||
|
1 2 3 4
|
||||||
|
4 3 2 1
|
||||||
|
1 2 3 4
|
4
2025.03.14/06Ex/b.txt
Normal file
4
2025.03.14/06Ex/b.txt
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
1
|
||||||
|
2
|
||||||
|
3
|
||||||
|
4
|
30
2025.03.14/06Ex/init_f.c
Normal file
30
2025.03.14/06Ex/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 n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
9
2025.03.14/06Ex/init_f.h
Normal file
9
2025.03.14/06Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef INIT_F_H
|
||||||
|
#define INIT_F_H
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j);
|
||||||
|
double f2(int n, int m, int i, int j);
|
||||||
|
double f3(int n, int m, int i, int j);
|
||||||
|
double f4(int n, int m, int i, int j);
|
||||||
|
|
||||||
|
#endif
|
13
2025.03.14/06Ex/io_status.h
Normal file
13
2025.03.14/06Ex/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
|
110
2025.03.14/06Ex/main.c
Normal file
110
2025.03.14/06Ex/main.c
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
#include "io_status.h"
|
||||||
|
#include "solve.h"
|
||||||
|
|
||||||
|
/* ./a.out n m p k_a [filename_a] k_b [filename_b] */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double t, res, *a, *b;
|
||||||
|
int n, m, p, k_a, k_b, task = 6;
|
||||||
|
char *name_a = 0, *name_b = 0;
|
||||||
|
if (!((argc == 6 || argc == 7 || argc == 8) &&
|
||||||
|
sscanf(argv[1], "%d", &n) == 1 &&
|
||||||
|
sscanf(argv[2], "%d", &m) == 1 &&
|
||||||
|
sscanf(argv[3], "%d", &p) == 1 &&
|
||||||
|
sscanf(argv[4], "%d", &k_a) == 1 &&
|
||||||
|
(k_a >= 0 && k_a <= 4) &&
|
||||||
|
(!(k_a == 0 && argc == 6)) &&
|
||||||
|
((k_a == 0 && sscanf(argv[6], "%d", &k_b) == 1) ||
|
||||||
|
(k_a != 0 && sscanf(argv[5], "%d", &k_b) == 1)) &&
|
||||||
|
(k_b >= 0 && k_b <= 4) &&
|
||||||
|
(!(k_b == 0 && argc == 6)) &&
|
||||||
|
(!((k_a == 0 && k_b == 0) && argc != 8))))
|
||||||
|
{
|
||||||
|
printf("Usage: %s n m p k_a [filename_a] k_b [filename_b]\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (argc != 6)
|
||||||
|
{
|
||||||
|
int i_b = 6;
|
||||||
|
if (k_a == 0) {name_a = argv[5];i_b++;}
|
||||||
|
if (k_b == 0) name_b = argv[i_b];
|
||||||
|
}
|
||||||
|
|
||||||
|
a = (double *)malloc(n * m * sizeof(double));
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
b = (double *)malloc(m * n * sizeof(double));
|
||||||
|
if (!b)
|
||||||
|
{
|
||||||
|
free(a);
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name_a)
|
||||||
|
{ /* из файла */
|
||||||
|
io_status ret;
|
||||||
|
ret = read_matrix(a, n, m, name_a);
|
||||||
|
do {
|
||||||
|
switch (ret)
|
||||||
|
{
|
||||||
|
case SUCCESS:
|
||||||
|
continue;
|
||||||
|
case ERROR_OPEN:
|
||||||
|
printf("Cannot open %s\n", name_a);
|
||||||
|
break;
|
||||||
|
case ERROR_READ:
|
||||||
|
printf("Cannot read %s\n", name_a);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
free(a);
|
||||||
|
free(b);
|
||||||
|
return 3;
|
||||||
|
} while (0);
|
||||||
|
} else init_matrix(a, n, m, k_a);
|
||||||
|
|
||||||
|
if (name_b)
|
||||||
|
{
|
||||||
|
io_status ret;
|
||||||
|
ret = read_matrix(b, m, n, name_b);
|
||||||
|
do {
|
||||||
|
switch (ret)
|
||||||
|
{
|
||||||
|
case SUCCESS:
|
||||||
|
continue;
|
||||||
|
case ERROR_OPEN:
|
||||||
|
printf("Cannot open %s\n", name_b);
|
||||||
|
break;
|
||||||
|
case ERROR_READ:
|
||||||
|
printf("Cannot read %s\n", name_b);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
free(a);
|
||||||
|
free(b);
|
||||||
|
return 3;
|
||||||
|
} while (0);
|
||||||
|
} else init_matrix(b, m, n, k_b);
|
||||||
|
|
||||||
|
printf("Matrix A:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
printf("Matrix B:\n");
|
||||||
|
print_matrix(b, m, n, p);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
res = t6_solve(a, b, n, m);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
free(b);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
38
2025.03.14/06Ex/solve.c
Normal file
38
2025.03.14/06Ex/solve.c
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include "math.h"
|
||||||
|
|
||||||
|
#define eps 1e-9
|
||||||
|
|
||||||
|
int compare(const double a, const double b)
|
||||||
|
{
|
||||||
|
if (a - b > eps) return 1;
|
||||||
|
else if (b - a > eps) return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double t6_solve(const double *A, const double *b, int n, int m)
|
||||||
|
{
|
||||||
|
double temp, summa, maximum = 0;
|
||||||
|
int i, j, k;
|
||||||
|
|
||||||
|
if (n <= 0 || m <= 0) return 0;
|
||||||
|
|
||||||
|
|
||||||
|
for (k = 0; k < n; k++)
|
||||||
|
{
|
||||||
|
summa = 0;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
{
|
||||||
|
temp = 0;
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
temp += A[i * m + j] * b[j * n + k];
|
||||||
|
|
||||||
|
if (k == i) temp--;
|
||||||
|
summa += fabs(temp);
|
||||||
|
}
|
||||||
|
if (compare(summa, maximum) == 1) maximum = summa;
|
||||||
|
}
|
||||||
|
|
||||||
|
return maximum;
|
||||||
|
}
|
||||||
|
|
7
2025.03.14/06Ex/solve.h
Normal file
7
2025.03.14/06Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
int compare(const double a, const double b);
|
||||||
|
double t6_solve(const double *A, const double *b, int n, int m);
|
||||||
|
|
||||||
|
#endif
|
40
2025.03.14/07Ex/array_io.c
Normal file
40
2025.03.14/07Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_matrix(double *a, int n, int m, const char *name)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||||
|
{fclose(fp); return ERROR_READ;}
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_matrix(const double *a, int n, int m, int p)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
int mp = (m > p ? p : m);
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < np; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < mp; j++)
|
||||||
|
printf(" %10.3e", a[i * m + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_matrix(double *a, int n, int m, 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 < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
a[i * m + j] = q(n, m, i+1, j+1);
|
||||||
|
}
|
11
2025.03.14/07Ex/array_io.h
Normal file
11
2025.03.14/07Ex/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 n, int m, const char *name);
|
||||||
|
void print_matrix(const double *a, int n, int m, int p);
|
||||||
|
void init_matrix(double *a, int n, int m, int k);
|
||||||
|
|
||||||
|
#endif
|
30
2025.03.14/07Ex/init_f.c
Normal file
30
2025.03.14/07Ex/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 n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
9
2025.03.14/07Ex/init_f.h
Normal file
9
2025.03.14/07Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef INIT_F_H
|
||||||
|
#define INIT_F_H
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j);
|
||||||
|
double f2(int n, int m, int i, int j);
|
||||||
|
double f3(int n, int m, int i, int j);
|
||||||
|
double f4(int n, int m, int i, int j);
|
||||||
|
|
||||||
|
#endif
|
13
2025.03.14/07Ex/io_status.h
Normal file
13
2025.03.14/07Ex/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.14/07Ex/main.c
Normal file
67
2025.03.14/07Ex/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 n m p k [filename] */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double t, res, *a;
|
||||||
|
int n, m, p, k, task = 7;
|
||||||
|
char *name = 0;
|
||||||
|
|
||||||
|
if (!((argc == 5 || argc == 6) &&
|
||||||
|
sscanf(argv[1], "%d", &n) == 1 &&
|
||||||
|
sscanf(argv[2], "%d", &m) == 1 &&
|
||||||
|
sscanf(argv[3], "%d", &p) == 1 &&
|
||||||
|
sscanf(argv[4], "%d", &k) == 1 &&
|
||||||
|
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
|
||||||
|
{
|
||||||
|
printf("Usage: %s n m p k [filename]\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (argc == 6) name = argv[5];
|
||||||
|
|
||||||
|
a = (double *)malloc(n * m * sizeof(double));
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name)
|
||||||
|
{ /* из файла */
|
||||||
|
io_status ret;
|
||||||
|
ret = read_matrix(a, n, m, 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, n, m, k);
|
||||||
|
|
||||||
|
printf("Matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
res = t7_solve(a, n, m);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
printf("New matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
32
2025.03.14/07Ex/solve.c
Normal file
32
2025.03.14/07Ex/solve.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define eps 1e-9
|
||||||
|
|
||||||
|
int compare(const double a, const double b)
|
||||||
|
{
|
||||||
|
if (a - b > eps) return 1;
|
||||||
|
else if (b - a > eps) return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double t7_solve(double *a, int n, int m)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
double cur = 0, maximum = 0;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (i == 0 || i == n-1 || j == 0 || j == m-1)
|
||||||
|
if (compare(fabs(a[i*m + j]), maximum) == 1)
|
||||||
|
maximum = fabs(a[i*m + j]);
|
||||||
|
|
||||||
|
for (i = 1; i < n-1; i++)
|
||||||
|
for (j = 1; j < m-1; j++)
|
||||||
|
{
|
||||||
|
cur = (a[(i-1)*m + j] + a[(i+1)*m + j] + a[i*m + (j-1)] + a[i*m + (j+1)]) / 5;
|
||||||
|
if (compare(fabs(cur), maximum) == 1) maximum = fabs(cur);
|
||||||
|
a[i*m + j] = cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
return maximum;
|
||||||
|
}
|
7
2025.03.14/07Ex/solve.h
Normal file
7
2025.03.14/07Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
int compare(const double a, const double b);
|
||||||
|
double t7_solve(double *a, int n, int m);
|
||||||
|
|
||||||
|
#endif
|
5
2025.03.14/07Ex/t.txt
Normal file
5
2025.03.14/07Ex/t.txt
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
1 2 3 4
|
||||||
|
5 6 7 8
|
||||||
|
9 10 11 12
|
||||||
|
13 14 15 16
|
||||||
|
17 18 19 20
|
3
2025.03.14/08Ex/a.txt
Normal file
3
2025.03.14/08Ex/a.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
1 2 3 4
|
||||||
|
1 1 1 1
|
||||||
|
2 2 2 2
|
40
2025.03.14/08Ex/array_io.c
Normal file
40
2025.03.14/08Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_matrix(double *a, int n, int m, const char *name)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||||
|
{fclose(fp); return ERROR_READ;}
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_matrix(const double *a, int n, int m, int p)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
int mp = (m > p ? p : m);
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < np; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < mp; j++)
|
||||||
|
printf(" %10.3e", a[i * m + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_matrix(double *a, int n, int m, 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 < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
a[i * m + j] = q(n, m, i+1, j+1);
|
||||||
|
}
|
11
2025.03.14/08Ex/array_io.h
Normal file
11
2025.03.14/08Ex/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 n, int m, const char *name);
|
||||||
|
void print_matrix(const double *a, int n, int m, int p);
|
||||||
|
void init_matrix(double *a, int n, int m, int k);
|
||||||
|
|
||||||
|
#endif
|
30
2025.03.14/08Ex/init_f.c
Normal file
30
2025.03.14/08Ex/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 n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
9
2025.03.14/08Ex/init_f.h
Normal file
9
2025.03.14/08Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef INIT_F_H
|
||||||
|
#define INIT_F_H
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j);
|
||||||
|
double f2(int n, int m, int i, int j);
|
||||||
|
double f3(int n, int m, int i, int j);
|
||||||
|
double f4(int n, int m, int i, int j);
|
||||||
|
|
||||||
|
#endif
|
13
2025.03.14/08Ex/io_status.h
Normal file
13
2025.03.14/08Ex/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.14/08Ex/main.c
Normal file
67
2025.03.14/08Ex/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 n m p k [filename] */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double t, res, *a;
|
||||||
|
int n, m, p, k, task = 8;
|
||||||
|
char *name = 0;
|
||||||
|
|
||||||
|
if (!((argc == 5 || argc == 6) &&
|
||||||
|
sscanf(argv[1], "%d", &n) == 1 &&
|
||||||
|
sscanf(argv[2], "%d", &m) == 1 &&
|
||||||
|
sscanf(argv[3], "%d", &p) == 1 &&
|
||||||
|
sscanf(argv[4], "%d", &k) == 1 &&
|
||||||
|
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
|
||||||
|
{
|
||||||
|
printf("Usage: %s n m p k [filename]\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (argc == 6) name = argv[5];
|
||||||
|
|
||||||
|
a = (double *)malloc(n * m * sizeof(double));
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name)
|
||||||
|
{ /* из файла */
|
||||||
|
io_status ret;
|
||||||
|
ret = read_matrix(a, n, m, 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, n, m, k);
|
||||||
|
|
||||||
|
printf("Matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
res = t8_solve(a, n, m);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
printf("New matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
32
2025.03.14/08Ex/solve.c
Normal file
32
2025.03.14/08Ex/solve.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define eps 1e-9
|
||||||
|
|
||||||
|
int compare(const double a, const double b)
|
||||||
|
{
|
||||||
|
if (a - b > eps) return 1;
|
||||||
|
else if (b - a > eps) return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double t8_solve(double *a, int n, int m)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
double cur = 0, maximum = 0;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (i == 0 || i == n-1 || j == 0 || j == m-1)
|
||||||
|
if (compare(fabs(a[i*m + j]), maximum) == 1)
|
||||||
|
maximum = fabs(a[i*m + j]);
|
||||||
|
|
||||||
|
for (i = n-2; i > 0; i--)
|
||||||
|
for (j = m-2; j > 0; j--)
|
||||||
|
{
|
||||||
|
cur = (a[(i+1)*m + j] + a[(i-1)*m + j] + a[i*m + (j-1)] + a[i*m + (j+1)]) / 5;
|
||||||
|
if (compare(fabs(cur), maximum) == 1) maximum = fabs(cur);
|
||||||
|
a[i*m + j] = cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
return maximum;
|
||||||
|
}
|
7
2025.03.14/08Ex/solve.h
Normal file
7
2025.03.14/08Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
int compare(const double a, const double b);
|
||||||
|
double t8_solve(double *a, int n, int m);
|
||||||
|
|
||||||
|
#endif
|
3
2025.03.14/09Ex/a.txt
Normal file
3
2025.03.14/09Ex/a.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
1 2 3 4
|
||||||
|
1 1 1 1
|
||||||
|
2 2 2 2
|
40
2025.03.14/09Ex/array_io.c
Normal file
40
2025.03.14/09Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_matrix(double *a, int n, int m, const char *name)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||||
|
{fclose(fp); return ERROR_READ;}
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_matrix(const double *a, int n, int m, int p)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
int mp = (m > p ? p : m);
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < np; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < mp; j++)
|
||||||
|
printf(" %10.3e", a[i * m + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_matrix(double *a, int n, int m, 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 < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
a[i * m + j] = q(n, m, i+1, j+1);
|
||||||
|
}
|
11
2025.03.14/09Ex/array_io.h
Normal file
11
2025.03.14/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 n, int m, const char *name);
|
||||||
|
void print_matrix(const double *a, int n, int m, int p);
|
||||||
|
void init_matrix(double *a, int n, int m, int k);
|
||||||
|
|
||||||
|
#endif
|
30
2025.03.14/09Ex/init_f.c
Normal file
30
2025.03.14/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 n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
9
2025.03.14/09Ex/init_f.h
Normal file
9
2025.03.14/09Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef INIT_F_H
|
||||||
|
#define INIT_F_H
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j);
|
||||||
|
double f2(int n, int m, int i, int j);
|
||||||
|
double f3(int n, int m, int i, int j);
|
||||||
|
double f4(int n, int m, int i, int j);
|
||||||
|
|
||||||
|
#endif
|
13
2025.03.14/09Ex/io_status.h
Normal file
13
2025.03.14/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.14/09Ex/main.c
Normal file
67
2025.03.14/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 n m p k [filename] */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double t, res, *a;
|
||||||
|
int n, m, p, k, task = 9;
|
||||||
|
char *name = 0;
|
||||||
|
|
||||||
|
if (!((argc == 5 || argc == 6) &&
|
||||||
|
sscanf(argv[1], "%d", &n) == 1 &&
|
||||||
|
sscanf(argv[2], "%d", &m) == 1 &&
|
||||||
|
sscanf(argv[3], "%d", &p) == 1 &&
|
||||||
|
sscanf(argv[4], "%d", &k) == 1 &&
|
||||||
|
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
|
||||||
|
{
|
||||||
|
printf("Usage: %s n m p k [filename]\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (argc == 6) name = argv[5];
|
||||||
|
|
||||||
|
a = (double *)malloc(n * m * sizeof(double));
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name)
|
||||||
|
{ /* из файла */
|
||||||
|
io_status ret;
|
||||||
|
ret = read_matrix(a, n, m, 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, n, m, k);
|
||||||
|
|
||||||
|
printf("Matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
res = t9_solve(a, n, m);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
printf("New matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
32
2025.03.14/09Ex/solve.c
Normal file
32
2025.03.14/09Ex/solve.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define eps 1e-9
|
||||||
|
|
||||||
|
int compare(const double a, const double b)
|
||||||
|
{
|
||||||
|
if (a - b > eps) return 1;
|
||||||
|
else if (b - a > eps) return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double t9_solve(double *a, int n, int m)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
double cur = 0, maximum = 0;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (i == 0 || i == n-1 || j == 0 || j == m-1)
|
||||||
|
if (compare(fabs(a[i*m + j]), maximum) == 1)
|
||||||
|
maximum = fabs(a[i*m + j]);
|
||||||
|
|
||||||
|
for (i = 1; i < n-1; i++)
|
||||||
|
for (j = m-2; j > 0; j--)
|
||||||
|
{
|
||||||
|
cur = (a[(i-1)*m + j] + a[(i+1)*m + j] + a[i*m + (j-1)] + a[i*m + (j+1)]) / 5;
|
||||||
|
if (compare(fabs(cur), maximum) == 1) maximum = fabs(cur);
|
||||||
|
a[i*m + j] = cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
return maximum;
|
||||||
|
}
|
7
2025.03.14/09Ex/solve.h
Normal file
7
2025.03.14/09Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
int compare(const double a, const double b);
|
||||||
|
double t9_solve(double *a, int n, int m);
|
||||||
|
|
||||||
|
#endif
|
3
2025.03.14/10Ex/a.txt
Normal file
3
2025.03.14/10Ex/a.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
1 2 3 4
|
||||||
|
1 1 1 1
|
||||||
|
2 2 2 2
|
40
2025.03.14/10Ex/array_io.c
Normal file
40
2025.03.14/10Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_matrix(double *a, int n, int m, const char *name)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||||
|
{fclose(fp); return ERROR_READ;}
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_matrix(const double *a, int n, int m, int p)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
int mp = (m > p ? p : m);
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < np; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < mp; j++)
|
||||||
|
printf(" %10.3e", a[i * m + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_matrix(double *a, int n, int m, 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 < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
a[i * m + j] = q(n, m, i+1, j+1);
|
||||||
|
}
|
11
2025.03.14/10Ex/array_io.h
Normal file
11
2025.03.14/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 n, int m, const char *name);
|
||||||
|
void print_matrix(const double *a, int n, int m, int p);
|
||||||
|
void init_matrix(double *a, int n, int m, int k);
|
||||||
|
|
||||||
|
#endif
|
30
2025.03.14/10Ex/init_f.c
Normal file
30
2025.03.14/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 n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
9
2025.03.14/10Ex/init_f.h
Normal file
9
2025.03.14/10Ex/init_f.h
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
#ifndef INIT_F_H
|
||||||
|
#define INIT_F_H
|
||||||
|
|
||||||
|
double f1(int n, int m, int i, int j);
|
||||||
|
double f2(int n, int m, int i, int j);
|
||||||
|
double f3(int n, int m, int i, int j);
|
||||||
|
double f4(int n, int m, int i, int j);
|
||||||
|
|
||||||
|
#endif
|
13
2025.03.14/10Ex/io_status.h
Normal file
13
2025.03.14/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.14/10Ex/main.c
Normal file
67
2025.03.14/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 n m p k [filename] */
|
||||||
|
int main(int argc, char *argv[])
|
||||||
|
{
|
||||||
|
double t, res, *a;
|
||||||
|
int n, m, p, k, task = 10;
|
||||||
|
char *name = 0;
|
||||||
|
|
||||||
|
if (!((argc == 5 || argc == 6) &&
|
||||||
|
sscanf(argv[1], "%d", &n) == 1 &&
|
||||||
|
sscanf(argv[2], "%d", &m) == 1 &&
|
||||||
|
sscanf(argv[3], "%d", &p) == 1 &&
|
||||||
|
sscanf(argv[4], "%d", &k) == 1 &&
|
||||||
|
k >= 0 && k <= 4 && (!(k == 0 && argc != 6))))
|
||||||
|
{
|
||||||
|
printf("Usage: %s n m p k [filename]\n", argv[0]);
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
if (argc == 6) name = argv[5];
|
||||||
|
|
||||||
|
a = (double *)malloc(n * m * sizeof(double));
|
||||||
|
if (!a)
|
||||||
|
{
|
||||||
|
printf("Not enough memory\n");
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (name)
|
||||||
|
{ /* из файла */
|
||||||
|
io_status ret;
|
||||||
|
ret = read_matrix(a, n, m, 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, n, m, k);
|
||||||
|
|
||||||
|
printf("Matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
|
||||||
|
t = clock();
|
||||||
|
res = t10_solve(a, n, m);
|
||||||
|
t = (clock() - t) / CLOCKS_PER_SEC;
|
||||||
|
|
||||||
|
printf("New matrix:\n");
|
||||||
|
print_matrix(a, n, m, p);
|
||||||
|
printf ("%s : Task = %d Result = %e Elapsed = %.2f\n", argv[0], task, res, t);
|
||||||
|
|
||||||
|
free(a);
|
||||||
|
return 0;
|
||||||
|
}
|
32
2025.03.14/10Ex/solve.c
Normal file
32
2025.03.14/10Ex/solve.c
Normal file
|
@ -0,0 +1,32 @@
|
||||||
|
#include "solve.h"
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
#define eps 1e-9
|
||||||
|
|
||||||
|
int compare(const double a, const double b)
|
||||||
|
{
|
||||||
|
if (a - b > eps) return 1;
|
||||||
|
else if (b - a > eps) return -1;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
double t10_solve(double *a, int n, int m)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
double cur = 0, maximum = 0;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (i == 0 || i == n-1 || j == 0 || j == m-1)
|
||||||
|
if (compare(fabs(a[i*m + j]), maximum) == 1)
|
||||||
|
maximum = fabs(a[i*m + j]);
|
||||||
|
|
||||||
|
for (i = n-2; i > 0; i--)
|
||||||
|
for (j = 1; j < m-1; j++)
|
||||||
|
{
|
||||||
|
cur = (a[(i+1)*m + j] + a[(i-1)*m + j] + a[i*m + (j-1)] + a[i*m + (j+1)]) / 5;
|
||||||
|
if (compare(fabs(cur), maximum) == 1) maximum = fabs(cur);
|
||||||
|
a[i*m + j] = cur;
|
||||||
|
}
|
||||||
|
|
||||||
|
return maximum;
|
||||||
|
}
|
7
2025.03.14/10Ex/solve.h
Normal file
7
2025.03.14/10Ex/solve.h
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
#ifndef SOLVE_H
|
||||||
|
#define SOLVE_H
|
||||||
|
|
||||||
|
int compare(const double a, const double b);
|
||||||
|
double t10_solve(double *a, int n, int m);
|
||||||
|
|
||||||
|
#endif
|
3
2025.03.14/11Ex/a.txt
Normal file
3
2025.03.14/11Ex/a.txt
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
1 1 1 1
|
||||||
|
1 2 3 4
|
||||||
|
2 2 2 2
|
40
2025.03.14/11Ex/array_io.c
Normal file
40
2025.03.14/11Ex/array_io.c
Normal file
|
@ -0,0 +1,40 @@
|
||||||
|
#include <stdio.h>
|
||||||
|
#include "array_io.h"
|
||||||
|
|
||||||
|
io_status read_matrix(double *a, int n, int m, const char *name)
|
||||||
|
{
|
||||||
|
int i, j;
|
||||||
|
FILE *fp;
|
||||||
|
if (!(fp = fopen(name, "r"))) return ERROR_OPEN;
|
||||||
|
for (i = 0; i < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
if (fscanf(fp, "%lf", a + i * m + j) != 1)
|
||||||
|
{fclose(fp); return ERROR_READ;}
|
||||||
|
fclose(fp);
|
||||||
|
return SUCCESS;
|
||||||
|
}
|
||||||
|
|
||||||
|
void print_matrix(const double *a, int n, int m, int p)
|
||||||
|
{
|
||||||
|
int np = (n > p ? p : n);
|
||||||
|
int mp = (m > p ? p : m);
|
||||||
|
int i, j;
|
||||||
|
|
||||||
|
for (i = 0; i < np; i++)
|
||||||
|
{
|
||||||
|
for (j = 0; j < mp; j++)
|
||||||
|
printf(" %10.3e", a[i * m + j]);
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void init_matrix(double *a, int n, int m, 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 < n; i++)
|
||||||
|
for (j = 0; j < m; j++)
|
||||||
|
a[i * m + j] = q(n, m, i+1, j+1);
|
||||||
|
}
|
11
2025.03.14/11Ex/array_io.h
Normal file
11
2025.03.14/11Ex/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 n, int m, const char *name);
|
||||||
|
void print_matrix(const double *a, int n, int m, int p);
|
||||||
|
void init_matrix(double *a, int n, int m, int k);
|
||||||
|
|
||||||
|
#endif
|
30
2025.03.14/11Ex/init_f.c
Normal file
30
2025.03.14/11Ex/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 n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
return MAX(n, m) - MAX(i, j) + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
double f2(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return MAX(i, j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f3(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return abs(i - j);
|
||||||
|
}
|
||||||
|
|
||||||
|
double f4(int n, int m, int i, int j)
|
||||||
|
{
|
||||||
|
(void)n;
|
||||||
|
(void)m;
|
||||||
|
return 1./(i+j-1);
|
||||||
|
}
|
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue