From f907d8292498bd1db1a0c47647ee7dcaa81ca729 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D1=80=D1=8C=D1=8F?= Date: Wed, 11 Dec 2024 18:44:23 +0000 Subject: [PATCH 1/4] Dasha --- 22_Somka/2D_Array/14.c | 64 ++++++++++++++++++++++++++++++++++++++++++ 22_Somka/2D_Array/25.c | 58 ++++++++++++++++++++++++++++++++++++++ 22_Somka/KR/1.c | 46 ++++++++++++++++++++++++++++++ 22_Somka/KR/2.c | 47 +++++++++++++++++++++++++++++++ 4 files changed, 215 insertions(+) create mode 100644 22_Somka/2D_Array/14.c create mode 100644 22_Somka/2D_Array/25.c create mode 100644 22_Somka/KR/1.c create mode 100644 22_Somka/KR/2.c diff --git a/22_Somka/2D_Array/14.c b/22_Somka/2D_Array/14.c new file mode 100644 index 0000000..b20912d --- /dev/null +++ b/22_Somka/2D_Array/14.c @@ -0,0 +1,64 @@ +/// 14 задание массивы +#include +#include +#include +#include "locale.h" +#include "time.h" + +int main() { + int** mt; + int** mt1; + int m, n; + int min=1000000; + int num = 0; + setlocale(LC_ALL, "Russian"); + printf("Введите размеры матрицы \n"); + scanf_s("%d %d", &m, &n); + mt = (int**)malloc(m*sizeof(int*)); + for (int i = 0; i < m; i++) { + mt[i] = (int*)malloc(n * sizeof(int)); + } + srand(time(NULL)); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + mt[i][j] = rand() % 101-50; + printf("%d ", mt[i][j]); + } + printf("\n"); + } + printf("\n"); + for (int i = 0; i < n; i++) { + int cmin = 100000; + for (int j = 0; j < m; j++) { + if (mt[j][i] < cmin) { + cmin = mt[j][i]; + } + } + if (cmin < min) { + min = cmin; + num = i; + } + } + mt1 = (int**)malloc(m * sizeof(int*)); + for (int i = 0; i < m; i++) { + mt1[i] = (int*)malloc((n-1) * sizeof(int)); + } + for (int i = 0; i < m; i++) { + int k = 0; + for (int j = 0; j < n; j++) { + if (j != num) { + mt1[i][k] = mt[i][j]; + k++; + } + } + } + + for (int i = 0; i < m; i++) { + for (int j = 0; j < (n-1); j++) { + printf("%d ",mt1[i][j]); + } + printf("\n"); + } + + +} \ No newline at end of file diff --git a/22_Somka/2D_Array/25.c b/22_Somka/2D_Array/25.c new file mode 100644 index 0000000..e7a4f5c --- /dev/null +++ b/22_Somka/2D_Array/25.c @@ -0,0 +1,58 @@ +/// 25 задание +#include +#include +#include +#include "locale.h" +#include "time.h" + +int main() { + int** mt; + int m, n; + int sum = 0; + int magic = 1; + setlocale(LC_ALL, "Russian"); + printf("Введите размеры матрицы \n"); + scanf_s("%d %d", &m, &n); + mt = (int**)malloc(m*sizeof(int*)); + for (int i = 0; i < m; i++) { + mt[i] = (int*)malloc(n * sizeof(int)); + } + srand(time(NULL)); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + mt[i][j] = rand() % 101-50; + printf("%d ", mt[i][j]); + } + printf("\n"); + } + printf("\n"); + for (int i = 0; i < m; i++) { + sum = sum + mt[i][0]; + } + for (int i = 0; i < m; i++) { + int c = 0; + for (int j = 0; j < n; j++) { + c = c + mt[i][j]; + } + if (sum != c) { + magic = 0; + break; + } + } + for (int i = 0; i < m; i++) { + int c = 0; + for (int j = 0; j < n; j++) { + c = c + mt[j][i]; + } + if (sum != c) { + magic = 0; + break; + } + } + if (magic == 1) { + printf("Квадрат магический"); + } + if (magic == 0) { + printf("Квадрат не является магическим"); + } +} \ No newline at end of file diff --git a/22_Somka/KR/1.c b/22_Somka/KR/1.c new file mode 100644 index 0000000..cf48bfb --- /dev/null +++ b/22_Somka/KR/1.c @@ -0,0 +1,46 @@ +#include +#include +#include +#include "locale.h" + +int main() { + int s1, s2; + int* m1; + int* m2; + int* c; + int k = 0; + int n = 0; + setlocale(LC_ALL, "Russian"); + printf("Введите колличество элементов массива "); + scanf_s("%d", &s1); + m1 = (int*)malloc(s1 * sizeof(int)); + for (int i = 0; i < s1; i++) { + scanf_s("%d", &m1[i]); + } + printf("Введите колличество элементов массива "); + scanf_s("%d", &s2); + m2 = (int*)malloc(s2 * sizeof(int)); + for (int i = 0; i < s2; i++) { + scanf_s("%d", &m2[i]); + } + for (int i = 0; i < s1; i++) { + for (int j=0; j < s2; j++) { + if (m1[i] == m2[j]) { + k += 1; + } + } + } + c = (int*)malloc(k * sizeof(int)); + for (int i = 0; i < s1; i++) { + for (int j=0; j < s2; j++) { + if (m1[i] == m2[j]) { + c[n] = m1[i]; + n++; + } + } + } + for (int i = 0; i < k; i++) { + printf("%d ", c[i]); + } + +} \ No newline at end of file diff --git a/22_Somka/KR/2.c b/22_Somka/KR/2.c new file mode 100644 index 0000000..d142ba8 --- /dev/null +++ b/22_Somka/KR/2.c @@ -0,0 +1,47 @@ +#include +#include +#include +#include "locale.h" +#include "time.h" + +int main() { + int** mt; + int m, n; + int sum=0; + int mid; + int max=0; + int num=0; + int k = 0; + setlocale(LC_ALL, "Russian"); + printf("Введите размеры матрицы \n"); + scanf_s("%d %d", &m, &n); + mt = (int**)malloc(m*sizeof(int*)); + for (int i = 0; i < m; i++) { + mt[i] = (int*)malloc(n * sizeof(int)); + } + srand(time(NULL)); + for (int i = 0; i < m; i++) { + for (int j = 0; j < n; j++) { + mt[i][j] = rand() % 100; + sum = sum + mt[i][j]; + printf("%d ", mt[i][j]); + } + printf("\n"); + } + mid = sum / (m * n); + for (int i = 0; i < n; i++) { + k = 0; + for (int j = 0; j < m; j++) { + if (mt[j][i] > mid) { + k++; + } + } + if (k > max) { + max = k; + num = i+1; + } + + } + printf("%d %d %d", mid, num, max); + +} \ No newline at end of file From f03f5e15a645069b57cff7c19d3008ccb41f0560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D1=80=D1=8C=D1=8F?= Date: Wed, 11 Dec 2024 21:47:12 +0000 Subject: [PATCH 2/4] taylor --- 22_Somka/Taylor/code.c | 252 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 252 insertions(+) create mode 100644 22_Somka/Taylor/code.c diff --git a/22_Somka/Taylor/code.c b/22_Somka/Taylor/code.c new file mode 100644 index 0000000..d99bfa9 --- /dev/null +++ b/22_Somka/Taylor/code.c @@ -0,0 +1,252 @@ +#include +#include +#include + + +double FirstSin(double x) { + return x; +} + +int JumpSin(){ // шаг в цикле + return 2; +} + +double NextSin(double x, int i) { + return -x*x/((i-1)*i); +} + + +double FirstCos(double x) { + return 1; +} + +int JumpCos(){ + return 2; +} + +double NextCos(double x, int i) { + i--; + return -x*x/((i-1)*i); +} + + +double FirstExp(double x) { + return 1; +} + +int JumpExp(){ + return 1; +} + +double NextExp(double x, int i) { + i--; + return x/i; +} + + +double FirstCh(double x) { + return 1; +} + +int JumpCh(){ + return 2; +} + +double NextCh(double x, int i) { + i--; + return x*x/((i-1)*i); +} + + +typedef double (First)(double); +typedef int (Jump)(); +typedef double (Next)(double, int); + + +double Taylor(double x, int N, First f, Jump h, Next g) { + double prev, next, summ; + int i, jump; + + prev = f(x); summ = 0.0; + + jump = h(); + + for (i=1; i Date: Wed, 11 Dec 2024 21:49:57 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=D0=B1=D0=B0=D0=B3=D0=B8=20=D0=BF=D0=BE?= =?UTF-8?q?=D1=84=D0=B8=D0=BA=D1=81=D0=B8=D0=BB=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 22_Somka/Taylor/code.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/22_Somka/Taylor/code.c b/22_Somka/Taylor/code.c index d99bfa9..1f6e727 100644 --- a/22_Somka/Taylor/code.c +++ b/22_Somka/Taylor/code.c @@ -7,7 +7,7 @@ double FirstSin(double x) { return x; } -int JumpSin(){ // шаг в цикле +int JumpSin(){ return 2; } From fdacf2a18a4dbf8e9d55491007a6cc7e110dda12 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=94=D0=B0=D1=80=D1=8C=D1=8F?= Date: Tue, 17 Dec 2024 15:12:05 +0000 Subject: [PATCH 4/4] Taylor fix --- 22_Somka/Taylor/code.c | 403 ++++++++++++++++++++++++----------------- 1 file changed, 234 insertions(+), 169 deletions(-) diff --git a/22_Somka/Taylor/code.c b/22_Somka/Taylor/code.c index 1f6e727..a7dcf37 100644 --- a/22_Somka/Taylor/code.c +++ b/22_Somka/Taylor/code.c @@ -1,5 +1,6 @@ #include -#include +#include +#define _USE_MATH_DEFINES #include @@ -7,12 +8,8 @@ double FirstSin(double x) { return x; } -int JumpSin(){ - return 2; -} - double NextSin(double x, int i) { - return -x*x/((i-1)*i); + return -(x * x) / ((2 * i + 1) * 2 * i); } @@ -20,13 +17,8 @@ double FirstCos(double x) { return 1; } -int JumpCos(){ - return 2; -} - double NextCos(double x, int i) { - i--; - return -x*x/((i-1)*i); + return -(x * x) / ((2 * i - 1) * 2 * i); } @@ -34,219 +26,292 @@ double FirstExp(double x) { return 1; } -int JumpExp(){ - return 1; -} - double NextExp(double x, int i) { - i--; - return x/i; + return x / i; } -double FirstCh(double x) { - return 1; -} - -int JumpCh(){ - return 2; +double FirstLn(double x) { + return (x - 1); } -double NextCh(double x, int i) { - i--; - return x*x/((i-1)*i); +double NextLn(double x, int i) { + return(-(i * (x - 1)) / (i + 1)); } typedef double (First)(double); -typedef int (Jump)(); typedef double (Next)(double, int); -double Taylor(double x, int N, First f, Jump h, Next g) { - double prev, next, summ; - int i, jump; - - prev = f(x); summ = 0.0; - - jump = h(); +double Taylor(double x, int N, double acc, double etalon, First f, Next g) { + double sum, prev, next, diff; + diff = acc; - for (i=1; i= acc) { + next = prev * g(x, i); + sum += next; prev = next; + + diff = fabs(sum - etalon); } } - return summ; + return sum; } void First_Mode() { int func, N; - double x, diff; + double value, x, acc, etalon, diff; - printf("\nSelect function\n1. sin(x)\n2. cos(x)\n3. exp(x)\n4. Ch(x)\nEnter: "); - scanf("%d", &func); + printf("1. sin(x)\n2. cos(x)\n3. exp(x)\n4. ln(x)\n"); + do { + printf("Выберите функцию: "); + scanf_s("%d", &func); + } while (!(1 <= func <= 4)); - printf("\nInput x: "); - scanf("%lf", &x); + if (func == 4) { + printf("\n0 < x < 2 (!)\n"); - printf("Input N: "); - scanf("%d", &N); + do { + printf("Введите значение x: "); + scanf_s("%lf", &x); + } while ((x < 0) || (x > 2)); + } - printf("\n"); + else { + printf("\nВведите значение x: "); + scanf_s("%lf", &x); + } - switch (func){ - case 1: - printf("Taylor: sin(%lf) ~= %lf\n", x, Taylor(x, N, FirstSin, JumpSin, NextSin)); - printf("math.h: sin(%lf) = %lf\n", x, sin(x)); - - diff = (sin(x)-Taylor(x, N, FirstSin, JumpSin, NextSin)); - if (diff < 0) - diff = -diff; - - printf("Difference = %lf\n", diff); - break; + printf("Введите точность вычисления: "); + scanf_s("%lf", &acc); - case 2: - printf("Taylor: cos(%lf) ~= %lf\n", x, Taylor(x, N, FirstCos, JumpCos, NextCos)); - printf("math.h: cos(%lf) = %lf\n", x, cos(x)); - - diff = (cos(x)-Taylor(x, N, FirstCos, JumpCos, NextCos)); - if (diff < 0) - diff = -diff; - - printf("Difference = %lf\n", diff); - break; + printf("Введите кол-во слагаемых N: "); + scanf_s("%d", &N); - case 3: - printf("Taylor: exp(%lf) ~= %lf\n", x, Taylor(x, N, FirstExp, JumpExp, NextExp)); - printf("math.h: exp(%lf) = %lf\n", x, exp(x)); - diff = (exp(x)-Taylor(x, N, FirstExp, JumpExp, NextExp)); - if (diff < 0) - diff = -diff; - - printf("Difference = %lf\n", diff); - break; + if ((func == 1) || (func == 2)) + while (fabs(x) > 2 * M_PI) { + if (fabs(x) != x) + x = x + 2 * M_PI; - case 4: - printf("Taylor: Ch(%lf) ~= %lf\n", x, Taylor(x, N, FirstCh, JumpCh, NextCh)); - printf("math.h: Ch(%lf) = %lf\n", x, cosh(x)); + else + x = x - 2 * M_PI; + } - diff = (cosh(x)-Taylor(x, N, FirstCh, JumpCh, NextCh)); - if (diff < 0) - diff = -diff; - - printf("Difference = %lf\n", diff); - break; - default: - printf("Function is not defined\n"); - break; + switch (func) { + case 1: + etalon = sin(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + value = Taylor(x, N, acc, etalon, FirstSin, NextSin); + printf("Ф-ла Тейлора: %lf\n", value); + + diff = fabs(etalon - value); + + printf("Разница: %lf\n\n\n", diff); + + break; + + case 2: + etalon = cos(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + value = Taylor(x, N, acc, etalon, FirstCos, NextCos); + printf("Ф-ла Тейлора: %lf\n", value); + + diff = fabs(etalon - value); + + printf("Разница: %lf\n\n\n", diff); + + break; + + case 3: + etalon = exp(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + value = Taylor(x, N, acc, etalon, FirstExp, NextExp); + printf("Ф-ла Тейлора: %lf\n", value); + + diff = fabs(etalon - value); + + printf("Разница: %lf\n\n\n", diff); + + break; + + case 4: + etalon = log(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + value = Taylor(x, N, acc, etalon, FirstLn, NextLn); + printf("Ф-ла Тейлора: %lf\n", value); + + diff = fabs(etalon - value); + + printf("Разница: %lf\n\n\n", diff); + + break; + + default: + printf("Такой команды нет...\n\n\n"); + break; } - printf("\n"); } void Second_Mode() { - int func, N_max; - double x, diff; - int i; + int func, N_max; int i; + double value, x, etalon, diff; + + printf("1. sin(x)\n2. cos(x)\n3. exp(x)\n4. ln(x)\n"); + do { + printf("Выберите функцию для серийного эксперимента: "); + scanf_s("%d", &func); + } while (!(1 <= func <= 4)); + + if (func == 4) { + printf("\n0 < x < 2 (!)\n"); + + do { + printf("Введите значение x: "); + scanf_s("%lf", &x); + } while ((x < 0) || (x > 2)); + } - printf("\nSelect function\n1. sin(x)\n2. cos(x)\n3. exp(x)\n4. Ch(x)\nEnter: "); - scanf("%d", &func); + else { + printf("\nВведите значение x: "); + scanf_s("%lf", &x); + } - printf("\nInput x: "); - scanf("%lf", &x); + printf("Введите верхнюю границу кол-ва слагаемых N max (<= 25): "); + scanf_s("%d", &N_max); - printf("Input max N (<= 25): "); - scanf("%d", &N_max); - printf("\n"); + if ((func == 1) || (func == 2)) + while (fabs(x) > 2 * M_PI) { + if (fabs(x) != x) + x = x + 2 * M_PI; - switch (func){ - case 1: - printf("sin(x):\n"); - for (i=1; i>> "); + scanf_s("%d", &mode); + printf("\n"); switch (mode) { - case 1: - First_Mode(); - break; - - case 2: - Second_Mode(); - break; - - case 0: - printf("Bye!\n"); - mode = 0; - break; - - default: - printf("Command is not defined\n"); - break; + case 0: + printf("Пока!\n"); + break; + + case 1: + First_Mode(); + break; + + case 2: + Second_Mode(); + break; + + default: + printf("Такой команды нет...\n"); + break; } } -} \ No newline at end of file +}