Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create a new C++ source file named oracle.cpp that implements the class declared in oracle.h , so that oracle.cpp and the provided files compile into
Create a new C++ source file named oracle.cpp that implements the class declared in oracle.h, so that oracle.cpp and the provided files compile into a program that runs with no failed tests. You cannot add any more functions that that aren't in the oracle.h file. Submit the source file "oracle.cpp".
(oracle.h):
#ifndef ORACLE_H #define ORACLE_H #includeclass Oracle { public: // Creates a new oracle, ready to be told all numbers // (in any order) from 1 to 1000000, except for one or two. // // Must run in O(1) time. Oracle(); // Tells the oracle a number between 1 and 1000000 not yet told. // // Must run in O(1) time. void tell(int i); // If every number between 1 and 1000000 except one have // been told, and no number has been told more than once, // sets x equal to the one number not yet told. // // Otherwise has undefined behavior. // // Must run in O(1) time. void missing_one(int &x); // If every number between 1 and 1000000 except two have // been told, and no number has been told more than once, // sets x and y equal to the two numbers not yet told (where x --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------(main.cpp):#include #include#include #include #include "oracle.h" using namespace std; inline void _test(const char* expression, const char* file, int line) { cerr V; // Test missing_one. Oracle o1; for (int i = 1; i = 1; --i) if (i != m) o5.tell(i); x = 0; o5.missing_one(x); test(x == m); Oracle o6; m = (rand() % 1000000) + 1; V.clear(); for (int i = 1; i m2) swap(m1, m2); for (int i = 1; i m2) swap(m1, m2); for (int i = 1; i 1 Introduction In a distant land lies a curious town filled with strange inhabitants. They speak only in complementary language: everyone says everything except what they mean. When it comes to specifying numbers, like the number of dollars in their bank account, they don't say the number, but instead list every possible number (say, those between 1 and 1000000) except the actual value. For instance tell(1) It's not 1) tell (2); It's not 2") tell(4) "It's not 4") tell(5); It's not 5") Ok" Ok" Ok" Ok" tell(999999); (It's not 999999") tell (1000000); It's not 1000000) missing.one (x) "What number was missing?") Ok" Ok" x = 3; ("3 was missing") In this homework, you'll implement an "oracle" that can be told a sequence of numbers containing every number from 1 to 1000000, except for one or two, and returns the missing numbIf this seems impossible, just consider the following facts: Theorem! 1.1. Let x1,22, ,Fm-1, y be a peTnutation of the numbers from 1 to n. Then y = n(n+1) Theorem 1.2. Let r1,22,...,Tn-2. yi, y2 be a permutation of the numbers from 1 to n. Let syi+ Moreover, 2y2-2sy1+52 and n-2 , and t = i=1 ormu wl 2a 1 Introduction In a distant land lies a curious town filled with strange inhabitants. They speak only in complementary language: everyone says everything except what they mean. When it comes to specifying numbers, like the number of dollars in their bank account, they don't say the number, but instead list every possible number (say, those between 1 and 1000000) except the actual value. For instance tell(1) It's not 1) tell (2); It's not 2") tell(4) "It's not 4") tell(5); It's not 5") Ok" Ok" Ok" Ok" tell(999999); (It's not 999999") tell (1000000); It's not 1000000) missing.one (x) "What number was missing?") Ok" Ok" x = 3; ("3 was missing") In this homework, you'll implement an "oracle" that can be told a sequence of numbers containing every number from 1 to 1000000, except for one or two, and returns the missing numbIf this seems impossible, just consider the following facts: Theorem! 1.1. Let x1,22, ,Fm-1, y be a peTnutation of the numbers from 1 to n. Then y = n(n+1) Theorem 1.2. Let r1,22,...,Tn-2. yi, y2 be a permutation of the numbers from 1 to n. Let syi+ Moreover, 2y2-2sy1+52 and n-2 , and t = i=1 ormu wl 2a
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