From c1b6ffe70bd281c6c230fd63fabcaac2aff47514 Mon Sep 17 00:00:00 2001 From: Fuwn Date: Sun, 7 Apr 2024 23:18:32 -0700 Subject: feat: initial commit --- chapter9/fractal.cxx | 48 +++++++++++++++++++++++++ chapter9/fractal.o | Bin 0 -> 3233 bytes chapter9/maze.cxx | 75 ++++++++++++++++++++++++++++++++++++++ chapter9/powers.cxx | 59 ++++++++++++++++++++++++++++++ chapter9/powers.exe | Bin 0 -> 62407 bytes chapter9/powers.o | Bin 0 -> 3858 bytes chapter9/recursionPrograms.zip | Bin 0 -> 4930 bytes chapter9/useful.cxx | 80 +++++++++++++++++++++++++++++++++++++++++ chapter9/useful.h | 46 ++++++++++++++++++++++++ chapter9/vertical.cxx | 47 ++++++++++++++++++++++++ chapter9/vertical.exe | Bin 0 -> 58448 bytes chapter9/vertical.o | Bin 0 -> 3912 bytes 12 files changed, 355 insertions(+) create mode 100644 chapter9/fractal.cxx create mode 100644 chapter9/fractal.o create mode 100644 chapter9/maze.cxx create mode 100644 chapter9/powers.cxx create mode 100644 chapter9/powers.exe create mode 100644 chapter9/powers.o create mode 100644 chapter9/recursionPrograms.zip create mode 100644 chapter9/useful.cxx create mode 100644 chapter9/useful.h create mode 100644 chapter9/vertical.cxx create mode 100644 chapter9/vertical.exe create mode 100644 chapter9/vertical.o (limited to 'chapter9') diff --git a/chapter9/fractal.cxx b/chapter9/fractal.cxx new file mode 100644 index 0000000..be61a84 --- /dev/null +++ b/chapter9/fractal.cxx @@ -0,0 +1,48 @@ +// File: fractal.cxx +// A demonstration program for the random_fractal function from Chapter 9. + +#include // Provides assert +#include // Provides EXIT_SUCCESS +#include // Provides cin, cout +#include "useful.h" // From Appendix H. Provides random_real, display +using namespace std; + +// PROTOTYPE for the function used in this demonstration program. +void random_fractal +(double left_height, double right_height, double width, double epsilon); +// Precondition: width and epsilon are both positive. +// Postcondition: The function has generated a random fractal from a line +// segment. The parameters left_height and right_height are the heights of the +// two endpoints of the line segment, and the parameter width is the segment's +// width. The generation of the random fractal stops when the width of the +// line segments reaches epsilon or less. +// Method of displaying the output: The height of the right endpoint of +// each line segment in the random fractal is displayed by calling the +// function display(...). + +int main() +{ + cout << "Turn your head sideways to see a nice landscape:" << endl; + random_fractal(0, 0, 16, 1); + return EXIT_SUCCESS; +} + +void random_fractal +(double left_height, double right_height, double width, double epsilon) +// Library facilities used: cassert, useful.h (From Appendix I). +{ + double mid_height; // Height of the midpoint of the line segment + + assert(width > 0); + assert(epsilon > 0); + + if (width <= epsilon) + display(right_height); + else + { + mid_height = (left_height + right_height) / 2; + mid_height += random_real(-width, width); + random_fractal(left_height, mid_height, width/2, epsilon); + random_fractal(mid_height, right_height, width/2, epsilon); + } +} diff --git a/chapter9/fractal.o b/chapter9/fractal.o new file mode 100644 index 0000000..2882d05 Binary files /dev/null and b/chapter9/fractal.o differ diff --git a/chapter9/maze.cxx b/chapter9/maze.cxx new file mode 100644 index 0000000..df4ff46 --- /dev/null +++ b/chapter9/maze.cxx @@ -0,0 +1,75 @@ +// FILE: maze.cxx +// An interactive program to lead the user into a maze and back out again, +// while searching for a magic tapestry. + +#include // Provides toupper +#include // Provides EXIT_SUCCESS +#include // Provides cin, cout +#include "useful.h" // From Appendix I; Provides eatline, inquire +using namespace std; + +// PROTOTYPES of functions used in this program. +bool dead_end(); +// Postcondition:The return value is true if the direction directly in front +// is a deadend (i.e., a direction that cannot contain the tapestry). + +bool traverse_maze(); +// Precondition: The user of the program is facing an unblocked spot in the +// maze which has not previously been visited by the user. +// Postcondition: The function has asked a series of questions and provided +// various directions to the user. The questions and directions have led the +// user through the maze and back to the exact same position that the user +// started at. The return value of the function is a true/false value +// indicating whether or not the user found a magic tapestry in the maze. + + +int main() +{ + if (traverse_maze()) + cout << "You found it!" << endl; + else + cout << "It wasn't found!" << endl; + return EXIT_SUCCESS; +} + +bool dead_end() +// Library facilities used: useful.h (From Appendix I) +{ + return inquire("Are you facing a wall?") + || + inquire("Is your name written in front of you?"); +} + +bool traverse_maze() +// Library facilities used: iostream.h +{ + int direction; // Counts 1, 2, 3 for the three directions to explore + bool found; // Will be set to true if we find the tapestry + + cout << "Step forward & write your name on the ground." << endl; + found = inquire("Have you found the tapestry?"); + + if (found) + { // Pick up the tapestry and step back from whence you came. + cout << "Pick up the tapestry and take a step backward." << endl; + } + else + { // Explore the three directions (not counting the one that you just + // came from). Start with the direction on your left, and then + // turn through each of the other possible directions one at a time. + + cout << "Please turn left 90 degrees." << endl; + for (direction = 1; direction <= 3; direction++) + { + if ( !found && !dead_end( ) ) + found = traverse_maze( ); + cout << "Please turn right 90 degrees." << endl; + } + + // You're now facing the direction from whence you came, so step + // forward and turn around. This will put you in the exact + // spot that you were at when the function call began. + cout << "Please step forward, then turn 180 degrees." << endl; + } + return found; +} diff --git a/chapter9/powers.cxx b/chapter9/powers.cxx new file mode 100644 index 0000000..3120f3e --- /dev/null +++ b/chapter9/powers.cxx @@ -0,0 +1,59 @@ +// FILE: powers.cxx +// A demonstration program for two recursive functions that compute powers. +#include // Provides assert +#include // Provides EXIT_SUCCESS +#include // Provides cin, cout +using namespace std; + +double power(double x, int n); +double pow(double x, int n); + +int main( ) +{ + double x; + int n; + + cout << "Please type a value for x (double) and n (int) : "; + cin >> x >> n; + cout << "The value of x to the n is: " << power(x, n) << endl; + + cout << "Please type a value for x (double) and n (int) : "; + cin >> x >> n; + cout << "The value of x to the n is: " << power(x, n) << endl; + + return EXIT_SUCCESS; +} + +double power(double x, int n) +{ + double product; // The product of x with itself n times + int count; + + if (x == 0) + assert(n > 0); + + if (n >= 0) + { + product = 1; + for (count = 1; count <= n; count++) + product = product * x; + return product; + } + else + return 1/power(x, -n); +} + +double pow(double x, int n) +{ + if (x == 0) + { + assert(n > 0); + return 0; + } + else if (n == 0) + return 1; + else if (n > 0) + return x * pow(x, n-1); + else // x is nonzero, and n is negative + return 1/pow(x, -n); +} diff --git a/chapter9/powers.exe b/chapter9/powers.exe new file mode 100644 index 0000000..1799a42 Binary files /dev/null and b/chapter9/powers.exe differ diff --git a/chapter9/powers.o b/chapter9/powers.o new file mode 100644 index 0000000..c966897 Binary files /dev/null and b/chapter9/powers.o differ diff --git a/chapter9/recursionPrograms.zip b/chapter9/recursionPrograms.zip new file mode 100644 index 0000000..a454335 Binary files /dev/null and b/chapter9/recursionPrograms.zip differ diff --git a/chapter9/useful.cxx b/chapter9/useful.cxx new file mode 100644 index 0000000..694016b --- /dev/null +++ b/chapter9/useful.cxx @@ -0,0 +1,80 @@ +// FILE: useful.cxx +// IMPLEMENTS: A toolkit of functions (See useful.h for documentation.) + +#include // Provides assert +#include // Provides toupper +#include // Provides rand, RAND_MAX +#include // Provides cout, cin, get +#include "useful.h" + +void display(double x) +// Library facilities used: iostream.h, stdlib.h +{ + const char STAR = '*'; + const char BLANK = ' '; + const char VERTICAL_BAR = '|'; + const int LIMIT = 39; + int i; + + if (x < -LIMIT) + x = -LIMIT; + else if (x > LIMIT) + x = LIMIT; + + for (i = -LIMIT; i < 0; i++) + { + if (i >= x) + cout << STAR; + else + cout << BLANK; + } + cout << VERTICAL_BAR; + for (i = 1; i <= LIMIT; i++) + { + if (i <= x) + cout << STAR; + else + cout << BLANK; + } + cout << endl; +} + +double random_fraction( ) +// Library facilities used: stdlib.h +{ + return rand( ) / double(RAND_MAX); +} + +double random_real(double low, double high) +// Library facilities used: assert.h +{ + assert(low <= high); + return low + random_fraction( ) * (high - low); +} + +void eat_line( ) +// Library facilities used: iostream.h +// +{ + char next; + + do + cin.get(next); + while (next != '\n'); +} + +bool inquire(const char query[ ]) +// Library facilities used: ctype.h, iostream.h +{ + char answer; + do + { + cout << query << " [Yes or No]" << endl; + cin >> answer; + answer = toupper(answer); + eat_line( ); + } + while ((answer != 'Y') && (answer != 'N')); + return (answer == 'Y'); +} + diff --git a/chapter9/useful.h b/chapter9/useful.h new file mode 100644 index 0000000..25dd03e --- /dev/null +++ b/chapter9/useful.h @@ -0,0 +1,46 @@ +// FILE: useful.h +// PROVIDES: A toolkit of useful functions for random numbers and displays. +// Note that these are in the global namespace. +// +// FUNCTIONS in the toolkit: +// double random_fraction( ) +// Postcondition: The return value is a random real number in the closed +// interval [0..1] (including the endpoints). +// +// double random_real(double low, double high) +// Precondition: low <= high. +// Postcondition: The return value is a random real number in the closed +// interval [low..high] (including the endpoints). +// +// void display(double x) +// Postcondition: The function has written one line of output to the +// standard ouput, with a vertical bar in the middle. If x is positive, +// then approximately x stars are printed to the right of the vertical +// bar. If x is negative, then approximately -x stars are printed to the +// left of the vertical bar. If the absolute value of x is more than +// 39, then only 39 stars are printed. +// Examples: +// display(8) prints: |******** +// display(-4) prints: ****| +// +// void eat_line( ) +// Postcondition: Up to next newline has been read and discarded from cin. +// +// bool inquire(const char query[ ]) +// Precondition: query is a null-terminated string of characters. +// Postcondition: query has been printed, and a one-line response read +// from the user. The function returns true if the user's response begins +// with 'Y' or 'y', and returns false if the user's response begins with +// 'N' or 'n'. (If the response begins with some other letter, then the +// query is repeated.) + +#ifndef USEFUL_H // Prevent duplicate definition +#define USEFUL_H + + double random_fraction( ); + double random_real(double low, double high); + void display(double x); + void eat_line( ); + bool inquire(const char query[ ]); + +#endif diff --git a/chapter9/vertical.cxx b/chapter9/vertical.cxx new file mode 100644 index 0000000..fc84e14 --- /dev/null +++ b/chapter9/vertical.cxx @@ -0,0 +1,47 @@ +// FILE: vertical.cxx +// two recursive functions that write the digits of a number vertically. +#include // Provides cin, cout +#include // Provides EXIT_SUCCESS +using namespace std; + +void write_vertical(unsigned int number); +void super_write_vertical(int number); + +int main( ) { + int i; + + cout << "Please type a non-negative number: "; + cin >> i; + cout << "The digits of that number are:" << endl; + write_vertical(i); + + cout << "Please type a negative number: "; + cin >> i; + cout << "The digits of that number are:" << endl; + super_write_vertical(i); + + cout << "Let's get vertical!" << endl; + return EXIT_SUCCESS; +} +void write_vertical(unsigned int number) { + if (number < 10) + cout << number << endl; // Write the one digit + else { + write_vertical(number/10); // Write all but the last digit + cout << number % 10 << endl; // Write the last digit + } +} +void super_write_vertical(int number) { + if (number < 0) { + cout << '-' << endl; // print a negative sign + super_write_vertical(abs(number)); // abs computes absolute value + // This is Spot #1 referred to in the text. + } + else if (number < 10) + cout << number << endl; // Write the one digit + else { + super_write_vertical(number/10); // Write all but the last digit + // This is Spot #2 referred to in the text. + cout << number % 10 << endl; // Write the last digit + } +} diff --git a/chapter9/vertical.exe b/chapter9/vertical.exe new file mode 100644 index 0000000..baab1ee Binary files /dev/null and b/chapter9/vertical.exe differ diff --git a/chapter9/vertical.o b/chapter9/vertical.o new file mode 100644 index 0000000..e68fe41 Binary files /dev/null and b/chapter9/vertical.o differ -- cgit v1.2.3