Files
IT-Step/elements.cpp
2025-09-11 20:49:24 +03:00

65 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
setlocale(0, "");
srand(time(0));
const int SiZE = 15;
int arr[SiZE];
for (int i = 0; i < SiZE; i++) {
arr[i] = rand() % 21 - 10;
}
cout << "Масив: ";
for (int i = 0; i < SiZE; i++) {
cout << arr[i] << " ";
}
cout << endl;
int count = 0;
int third = 0;
bool found = false;
for (int i = 0; i < SiZE; i++) {
if (arr[i] > 0) {
count++;
if (count == 3) {
third = arr[i];
found = true;
break;
}
}
}
int last = 0;
bool found2 = false;
for (int i = SiZE - 1; i >= 0; i--) {
if (arr[i] < 0) {
last = arr[i];
found2 = true;
break;
}
}
if (found) {
cout << "Третiй додатний елемент: " << third << endl;
}
else {
cout << "Третiй додатний елемент не знайдено" << endl;
}
if (found2) {
cout << "Останнiй вiд'ємний елемент: " << last << endl;
}
else {
cout << "Останнiй вiд'ємний елемент не знайдено" << endl;
}
return 0;
}