Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please write in C++ Given a list of random numbers and a number to search for, determine if the number is in the list. You
Please write in C++
Given a list of random numbers and a number to search for, determine if the number is in the list. You may not use a for-loop to solve this problem.
Starter Code:
#include#include #include #include static int getRandomNumber(); template std::ostream& operator<<(std::ostream& out, std::vector list) { out << "["; for (const auto& val : list) { out << val << ", "; } out << "]"; return out; } int main() { std::vector list; std::generate_n(std::back_inserter(list), 20, []() { return getRandomNumber(); }); std::cout << list << " "; auto numberToFind = getRandomNumber(); std::cout << "looking for " << numberToFind << " "; // TODO: print out 'true' if the number is in the list and 'false' otherwise // NOTE: NO for-loops, use an algorithm } static int getRandomNumber() { static std::mt19937 rng{ std::random_device{}() }; static std::uniform_int_distribution rand{ 0, 100 }; return rand(rng); }
Sample Output:
[29, 18, 47, 61, 50, 27, 27, 6, 14, 31, 29, 20, 55, 54, 15, 89, 64, 38, 30, 63, 83, 72, 16, 62, 98, 57, 14, 73, 91, 1, 45, 40, 89, 85, 0, 12, 78, 14, 62, 69, ] looking for 85 true ... [46, 80, 34, 21, 97, 23, 3, 35, 75, 95, 81, 32, 43, 76, 79, 100, 17, 91, 88, 95, 99, 40, 59, 71, 97, 3, 68, 20, 100, 31, 92, 8, 77, 27, 18, 88, 0, 63, 57, 92, ] looking for 86 false
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started