дз 1 семестр / ДЗ 4
.docxДомашняя работа №4
Задание 1
Текст программы:
#include <iostream>
#include <locale>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru");
int A[] = { 5, 57, 41, 11, 42, 23, 13, 82, 3, 61 };
for (int i = 0; i < 10; i++)
{
cout << "A[" << i << "] = " << A[i] << endl;
}
float s = 0;
for (int i = 0; i < 10; i++)
{
s += A[i];
}
float sr = s / 10;
cout << "Среднее арифметическое = " << sr << endl;
float min = 100;
for (int i = 0; i < 10; i++)
{
float r = fabs(A[i] - sr);
if (r < min)
min = r;
}
//cout << "Минимальная разница по модулю = " << min << endl;
for (int i = 0; i < 10; i++)
{
if ((min + A[i] == sr) || (A[i] - min == sr))
cout << "Элемент, наиболее близкий к среднему арифметическому значению = " << A[i];
}
_getch();
}
Схема алгоритма:
Задание 2
Текст программы:
#include <iostream>
#include <locale>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru");
int A[] = {93, 5, 8, 14, 37, 61, 62, 90, 69, 34};
int ch = 0, nch = 0;
for (int i = 0; i < 10; i++)
{
if (A[i] % 2 == 0)
ch += 1;
else nch += 1;
}
cout << "Количество чётных элементов в массиве = " << ch << endl;
cout << "Количество нечётных элементов в массиве = " << nch << endl;
_getch();
}
Схема алгоритма:
Задание 3
Текст программы:
#include <iostream>
#include <locale>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru");
int Y[10][20]{ { 95, 62, 67, 65, 1, 100, 24, 8, 16, 92, 68, 8, 14, 3, 56, 35, 90, 87, 13, 19 }, { 94, 95, 90, 19, 8, 19, 24, 75, 40, 59, 43, 19, 39, 67, 25, 1, 13, 98, 15, 58 }, {49, 72, 38, 7, 10, 74, 41, 25, 13, 29, 96, 94, 22, 89, 58, 61, 46, 48, 46, 41}, {91, 95, 9, 26, 79, 100, 48, 28, 20, 57, 74, 61, 61, 36, 53, 24, 60, 41, 2, 6}, {31, 80, 81, 30, 72, 12, 5, 86, 77, 52, 62, 93, 51, 21, 14, 22, 55, 7, 47, 72}, {31, 76, 60, 87, 50, 23, 3, 1, 89, 83, 67, 50, 4, 92, 45, 82, 57, 5, 77, 14}, {85, 58, 52, 43, 37, 70, 31, 85, 50, 12, 44, 96, 32, 78, 88, 34, 18, 47, 17, 38}, {46, 73, 42, 8, 52, 76, 37, 13, 40, 40, 34, 86, 38, 91, 39, 8, 51, 90, 76, 82}, {29, 11, 49, 73, 10, 78, 88, 18, 98, 60, 94, 7, 66, 44, 64, 52, 5, 74, 34, 17}, {32, 25, 91, 72, 29, 9, 62, 13, 87, 20, 35, 100, 47, 23, 55, 93, 71, 76, 46, 63} };
int x = 0;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 20; j++)
{
if ((Y[i][j] % 2 != 0) && (j % 2 != 0))
x += 1;
}
}
cout << "Количество элементов с чётными индексами и нечётными значениями = " << x;
_getch();
}
Схема алгоритма:
Задание 4
Текст программы:
#include <iostream>
#include <locale>
#include <conio.h>
#include <math.h>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru");
int A[10][10]{ {51, 64, 79, 97, 27, 6, 27, 69, 48, 34}, {21, 25, 76, 64, 6, 44, 29, 11, 22, 12}, {30, 86, 95, 100, 82, 98, 57, 6, 56, 42}, {9, 17, 58, 3, 57, 10, 39, 26, 31, 33}, {44, 59, 95, 47, 97, 76, 69, 20, 20, 94}, {40, 30, 43, 78, 85, 56, 31, 81, 73, 53}, {62, 95, 81, 76, 72, 67, 62, 83, 38, 45}, {62, 51, 42, 65, 68, 52, 24, 52, 6, 21}, {20, 99, 99, 29, 69, 3, 15, 11, 93, 69}, {40, 90, 39, 7, 80, 29, 77, 90, 59, 89} };
int max = 0, x, y;
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
if (A[i][j] > max)
{
max = A[i][j];
x = i;
y = j;
}
}
}
cout << "Максимальный элемент массива: " << "A[" << x << "][" << y << "] = " << max;
_getch();
}
Схема алгоритма:
Задание 5
Текст программы:
#include <iostream>
#include <locale>
#include <conio.h>
#include <cmath>
#include <iomanip>
using namespace std;
int main()
{
setlocale(LC_ALL, "ru");
int A[100];
cout << "Элементы массива:" << endl;
for (int x = 0; x < 100; x++)
{
A[x] = rand() % 100;
cout << "A["<< x << "] = " << A[x] << endl;
}
for (int i = 0; i < 100; i++)
{
for (int j = 0; j < 100 - 1 - i; j++)
{
if (A[j] > A[j + 1])
{
swap(A[j], A[j + 1]);
}
}
}
cout << "Элементы массива, упорядоченные по не убыванию:" << endl;
for (int i = 0; i < 100; i++)
{
cout << A[i] << endl;
}
_getch();
return 0;
}
Схема алгоритма: