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 diff --git a/22_Somka/Taylor/code.c b/22_Somka/Taylor/code.c new file mode 100644 index 0000000..a7dcf37 --- /dev/null +++ b/22_Somka/Taylor/code.c @@ -0,0 +1,317 @@ +#include +#include +#define _USE_MATH_DEFINES +#include + + +double FirstSin(double x) { + return x; +} + +double NextSin(double x, int i) { + return -(x * x) / ((2 * i + 1) * 2 * i); +} + + +double FirstCos(double x) { + return 1; +} + +double NextCos(double x, int i) { + return -(x * x) / ((2 * i - 1) * 2 * i); +} + + +double FirstExp(double x) { + return 1; +} + +double NextExp(double x, int i) { + return x / i; +} + + +double FirstLn(double x) { + return (x - 1); +} + +double NextLn(double x, int i) { + return(-(i * (x - 1)) / (i + 1)); +} + + +typedef double (First)(double); +typedef double (Next)(double, int); + + +double Taylor(double x, int N, double acc, double etalon, First f, Next g) { + double sum, prev, next, diff; + diff = acc; + + prev = f(x); + sum = prev; + + int i; + for (i = 1; i < N; i++) { + if (diff >= acc) { + next = prev * g(x, i); + sum += next; + prev = next; + + diff = fabs(sum - etalon); + } + } + + return sum; +} + + +void First_Mode() { + int func, N; + double value, x, acc, 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)); + } + + else { + printf("\nВведите значение x: "); + scanf_s("%lf", &x); + } + + printf("Введите точность вычисления: "); + scanf_s("%lf", &acc); + + printf("Введите кол-во слагаемых N: "); + scanf_s("%d", &N); + + + if ((func == 1) || (func == 2)) + while (fabs(x) > 2 * M_PI) { + if (fabs(x) != x) + x = x + 2 * M_PI; + + else + x = x - 2 * M_PI; + } + + + 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; + } +} + + +void Second_Mode() { + 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)); + } + + else { + printf("\nВведите значение x: "); + scanf_s("%lf", &x); + } + + printf("Введите верхнюю границу кол-ва слагаемых N max (<= 25): "); + scanf_s("%d", &N_max); + + + if ((func == 1) || (func == 2)) + while (fabs(x) > 2 * M_PI) { + if (fabs(x) != x) + x = x + 2 * M_PI; + + else + x = x - 2 * M_PI; + } + + + switch (func) { + case 1: + etalon = sin(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + for (i = 1; i <= N_max; i++) { + value = Taylor(x, i, 0.0, etalon, FirstSin, NextSin); + diff = fabs(etalon - value); + + printf("(N = %d) ", i); + printf("Ф-ла Т.: %lf; ", value); + printf("разница: %lf\n", diff); + } + printf("\n\n"); + + break; + + case 2: + etalon = cos(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + for (i = 1; i <= N_max; i++) { + value = Taylor(x, i, 0.0, etalon, FirstCos, NextCos); + diff = fabs(etalon - value); + + printf("(N = %d) ", i); + printf("Ф-ла Т.: %lf; ", value); + printf("разница: %lf\n", diff); + } + printf("\n\n"); + + break; + + case 3: + etalon = exp(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + for (i = 1; i <= N_max; i++) { + value = Taylor(x, i, 0.0, etalon, FirstExp, NextExp); + diff = fabs(etalon - value); + + printf("(N = %d) ", i); + printf("Ф-ла Т.: %lf; ", value); + printf("разница: %lf\n", diff); + } + printf("\n\n"); + + break; + + case 4: + etalon = log(x); + + printf("\nЭталонное значение (math.h): %lf\n", etalon); + + for (i = 1; i <= N_max; i++) { + value = Taylor(x, i, 0.0, etalon, FirstLn, NextLn); + diff = fabs(etalon - value); + + printf("(N = %d) ", i); + printf("Ф-ла Т.: %lf; ", value); + printf("разница: %lf\n", diff); + } + printf("\n\n"); + + break; + + default: + printf("Такой команды нет...\n\n\n"); + break; + } +} + + +void main() { + setlocale(LC_ALL, "Russian"); + + int mode = -1; + + while (mode != 0) { + printf("Выберите режим:\n"); + printf(" 1. Первый\n 2. Второй\n 0. Выход\n>>> "); + scanf_s("%d", &mode); + printf("\n"); + + switch (mode) { + case 0: + printf("Пока!\n"); + break; + + case 1: + First_Mode(); + break; + + case 2: + Second_Mode(); + break; + + default: + printf("Такой команды нет...\n"); + break; + } + } +}