Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <vector>
- #include <format>
- using namespace std;
- int main()
- {
- SetConsoleOutputCP(1251);
- SetConsoleCP(1251);
- srand(time(0));
- vector<double> numbers{};
- double temp_number{}, S{}, difference{}, max_difference{};
- int N{}, i{}, index{};
- while (true) {
- cout << "Введіть бажану кількість елементів у масиві (від 2 до 10): ";
- cin >> N;
- if (cin.fail() || cin.peek() != '\n' || N < 2 || N > 10) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- cout << "Згенерований масив: ";
- for (i = 0; i < N; i++) {
- temp_number = (rand() % 199 + 1 - 100) + (rand() % 100 + 1) / 100.0;
- numbers.push_back(temp_number);
- cout << format("{:8.2f}", temp_number) << " ";
- }
- cout << endl;
- while (true) {
- cout << "Введіть число S: ";
- cin >> S;
- if (cin.fail()) {
- cin.clear();
- cin.ignore(32767, '\n');
- cout << "Число було введено неправильно. Спробуйте ще раз!" << endl;
- continue;
- }
- else {
- break;
- }
- }
- max_difference = fabs(numbers[0] - S);
- for (int i = 1; i < numbers.size(); i++) {
- difference = fabs(numbers[i] - S);
- if (difference > max_difference) {
- max_difference = difference;
- index = i;
- }
- }
- cout << "Найбільш віддалений елемент: " << numbers[index] << endl;
- cout << "Його індекс: " << index << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement