// File: is.cxx // First examples of functions that return boolean values #include #include #include using namespace std; const int S = 300; //-------------------------------------------------------- // Function prototypes. In all these functions, ss is // the size of an already open graphics screen // Allow the user to select one of our functions: int selection( ); // Returns true if n > 9000: bool is_big(int n); // Returns true if a <= b and b <= c: bool is_in_order(int a, int b, int c); // True if the graphics screen has at least one red pixel: bool is_some_red(int ss); // True if the graphics screen has all red pixels: bool is_all_red(int ss); // True if the graphics screen has all red pixels: bool is_majority_red(int ss); // Open a window with a certain probability of red pixels: void open_window(int ss, double red_probability); //-------------------------------------------------------- //-------------------------------------------------------- int main( ) { int n, a, b, c; // User input to test function double percentage; // User input to test function switch (selection( )) { case 1: // Test is_big cout << "Enter an integer: "; cin >> n; if (is_big(n)) cout << "That's bigger than 9000!" << endl; else cout << "That's small." << endl; break; case 2: // Test is_in_order cout << "Enter three integers: "; cin >> a >> b >> c; if (is_in_order(a, b, c)) cout << "Nicely ordered!" << endl; else cout << "Those are out of order." << endl; closegraph( ); break; case 3: // Test is_some_red cout << "Enter the percentage of red for a screen: "; cin >> percentage; open_window(S, percentage); if (is_some_red(S)) cout << "There is some red!" << endl; else cout << "Not a speck of red!" << endl; delay(5000); closegraph( ); break; case 4: // Test is_all_red cout << "Enter the percentage of red for a screen: "; cin >> percentage; open_window(S, percentage); if (is_all_red(S)) cout << "That's all red!" << endl; else cout << "Not all is red!" << endl; delay(5000); closegraph( ); break; case 5: // Test is_majority_red cout << "Enter the percentage of red for a screen: "; cin >> percentage; open_window(S, percentage); if (is_majority_red(S)) cout << "That's mostly red!" << endl; else cout << "That's not mostly red!" << endl; delay(5000); closegraph( ); break; } return EXIT_SUCCESS; } //-------------------------------------------------------- //-------------------------------------------------------- bool is_big(int n) { return (n > 9000); } //-------------------------------------------------------- //-------------------------------------------------------- bool is_in_order(int a, int b, int c) { return (a <= b) && (b <= c); } //-------------------------------------------------------- //-------------------------------------------------------- bool is_some_red(int ss) { int px, py; for (px = 0; px < ss; ++px) { for (py = 0; py < ss; ++py) { if (getpixel(px, py) == RED) { return true; } } } return false; } //-------------------------------------------------------- //-------------------------------------------------------- bool is_all_red(int ss) { return true; } //-------------------------------------------------------- //-------------------------------------------------------- bool is_majority_red(int ss) { return true; } //-------------------------------------------------------- //-------------------------------------------------------- void open_window(int ss, double red_probability) { initwindow(ss, ss); int px, py; for (px = 0; px < ss; ++px) { for (py = 0; py < ss; ++py) { if (double(rand())/RAND_MAX < red_probability) { putpixel(px, py, RED); } } } } //-------------------------------------------------------- //-------------------------------------------------------- int selection( ) { int answer; cout << "Please select one of these options: " << endl; cout << " 1: is_big(int n)" << endl; cout << " 2: is_in_order(int a, int b, int c)" << endl; cout << " 3: is_some_red(int ss)" << endl; cout << " 4: is_all_red(int ss)" << endl; cout << " 5: is_majority_red(int ss)" << endl; cout << "Your selection: "; cin >> answer; return answer; } //--------------------------------------------------------