Практическая работа №6
11.12.2024
Программа №1
cpp
#include <iostream>
#include <locale.h>
#include <cmath>
using namespace std;
int findMax(int a[], int size) {
int max = a[0];
for (int i = 0; i < size; i++) {
if (max < a[i]) {
max = a[i];
}
}
return max;
};
int main() {
int a[] = { 1, 5, 28, 2, 8, 154, 7, 8, 9 };
int size = sizeof(a) / sizeof(a[0]);
int res = findMax(a, size);
cout << res;
return 0;
}
Программа №2
cpp
#include <iostream>
#include <locale.h>
using namespace std;
bool isPrime(int num) {
if (num <= 1) return false;
for (int i = 2; i < num; i++) {
if (num % i == 0) return false;
}
return true;
}
int main() {
setlocale(LC_ALL, "");
int number;
cout << "Введите число: ";
cin >> number;
if (isPrime(number)) {
cout << "Число " << number << " является простым." << endl;
} else {
cout << "Число " << number << " не является простым." << endl;
}
return 0;
}
Программа №3
cpp
// Программа 2: Сортировка массива
#include <iostream>
#include <locale.h>
using namespace std;
void SortArray(int arr[], int size) {
for (int i = 0; i < size - 1; i++) {
for (int j = 0; j < size - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
int main() {
setlocale(LC_ALL, "");
int size;
cout << "Введите размер массива: ";
cin >> size;
int arr[size];
cout << "Введите элементы массива: ";
for (int i = 0; i < size; i++) {
cin >> arr[i];
}
SortArray(arr, size);
cout << "Отсортированный массив: ";
for (int i = 0; i < size; i++) {
cout << arr[i] << " ";
}
cout << endl;
return 0;
}
Last updated