Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <Windows.h>
- #include <clocale>
- #include <ctime>
- using namespace std;
- void arrInit(int *arr, int n);
- void arrOut(int *arr, int n);
- int findMax1(int *arr, int n, int max);
- int findMax2(int *arr, int n, int max);
- int main() {
- setlocale(LC_ALL, "Russian");
- srand(time(0));
- int n;
- cout << "Введите размер массива: ";
- cin >> n;
- int *arr = new int[n];
- cout << "****************************************************************" << endl;
- arrInit(arr, n);
- arrOut(arr, n);
- cout << endl;
- cout << "****************************************************************" << endl;
- int max1, max2;
- for (int i = 0; i < n; i++) {
- if (arr[i] % 2 == 0) {
- max1 = arr[i];
- break;
- }
- }
- for (int i = 0; i < n; i++) {
- if (arr[i] % 2 != 0) {
- max2 = arr[i];
- break;
- }
- }
- max1 = findMax1(arr, n, max1);
- max2 = findMax2(arr, n, max2);
- cout << "Максимальный элемент среди четных элементов массива равен " << max1 << endl;
- cout << "Максимальный элемент среди нечетных элементов массива равен " << max2 << endl;
- system("PAUSE");
- }
- void arrInit(int *arr, int n) {
- for (int i = 0; i < n; i++) {
- arr[i] = rand() % 100;
- }
- }
- void arrOut(int *arr, int n) {
- for (int i = 0; i < n; i++) {
- cout << arr[i] << "\t";
- }
- }
- int findMax1(int *arr, int n, int max) {
- for (int i = 0; i < n; i++) {
- if (arr[i] % 2 == 0 && arr[i] > max) {
- max = arr[i];
- }
- }
- return max;
- }
- int findMax2(int *arr, int n, int max) {
- for (int i = 0; i < n; i++) {
- if (arr[i] % 2 != 0 && arr[i] > max) {
- max = arr[i];
- }
- }
- return max;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement