Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- int main()
- {
- int i;
- int j;
- int k;
- int lengthArr;
- int buffer;
- double nearestNum;
- double setValue;
- bool isIncorrect;
- k = 0;
- lengthArr = 0;
- nearestNum = 0;
- setValue = 0;
- std::cout << "The program finds the number of the nearest member of the sequence to a given number.\n";
- std::cout << "Enter length of subsequence: ";
- do
- {
- isIncorrect = false;
- std::cin >> lengthArr;
- if (std::cin.fail())
- {
- std::cin.clear();
- while (std::cin.get() != '\n');
- isIncorrect = true;
- std::cerr << "Incorrect value. Enter the number: ";
- }
- if (!isIncorrect && std::cin.get() != '\n')
- {
- std::cin.clear();
- while (std::cin.get() != '\n');
- std::cerr << "Incorrect value. Enter the number: ";
- isIncorrect = true;
- }
- if (!isIncorrect && lengthArr < 2) {
- std::cerr << "Incorrect value (size must be > 1). Enter a valid value: ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- int* arr = new int[lengthArr];
- std::cout << "Enter elements of subsequence: ";
- for (i = 0; i < lengthArr; i++) {
- do {
- isIncorrect = false;
- std::cin >> arr[i];
- if ((std::cin.fail()))
- {
- std::cin.clear();
- while (std::cin.get() != '\n');
- isIncorrect = true;
- std::cerr << "Incorrect value. Enter the number: ";
- }
- if (!isIncorrect && std::cin.get() != '\n')
- {
- std::cin.clear();
- while (std::cin.get() != '\n');
- std::cerr << "Incorrect value. Enter the number: " << std::endl;
- isIncorrect = true;
- }
- } while (isIncorrect);
- }
- for (i = 0; i < lengthArr - 1; i++)
- {
- for (j = i + 1; j < lengthArr; j++)
- {
- if (arr[i] > arr[j])
- {
- buffer = arr[i];
- arr[i] = arr[j];
- arr[j] = buffer;
- }
- }
- }
- for (i = 0; i < lengthArr; i++)
- {
- std::cout << arr[i] << " ";
- }
- std::cout << "\nEnter real value: ";
- do
- {
- isIncorrect = false;
- std::cin >> setValue;
- if (std::cin.fail())
- {
- std::cin.clear();
- while (std::cin.get() != '\n');
- isIncorrect = true;
- std::cerr << "Incorrect value. Enter the number: ";
- }
- if (!isIncorrect && std::cin.get() != '\n')
- {
- std::cin.clear();
- while (std::cin.get() != '\n');
- std::cerr << "Incorrect value. Enter the number: ";
- isIncorrect = true;
- }
- if (!isIncorrect && (setValue < -2000000000 || setValue > 2000000000))
- {
- std::cerr << "Incorrect value. Enter a valid value: ";
- isIncorrect = true;
- }
- } while (isIncorrect);
- if (setValue < arr[0])
- {
- std::cout << "The entered value is less than the smallest term of the sequence.";
- }
- else
- {
- nearestNum = arr[0];
- i = 0;
- while (setValue > arr[i])
- {
- k = i;
- i++;
- }
- nearestNum = arr[k];
- }
- do
- {
- isIncorrect = false;
- if (nearestNum < arr[k + 1])
- std::cout << "The nearest element of the sequence numbered " << k + 1;
- else
- {
- isIncorrect = true;
- k++;
- }
- } while (isIncorrect);
- delete[] arr;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement