summaryrefslogtreecommitdiff
path: root/chapter9
diff options
context:
space:
mode:
authorFuwn <[email protected]>2024-04-07 23:18:32 -0700
committerFuwn <[email protected]>2024-04-07 23:18:32 -0700
commitc1b6ffe70bd281c6c230fd63fabcaac2aff47514 (patch)
treee8af3b1782a7cd0754590ed618fddc1bdb9b7385 /chapter9
downloaddscode-c1b6ffe70bd281c6c230fd63fabcaac2aff47514.tar.xz
dscode-c1b6ffe70bd281c6c230fd63fabcaac2aff47514.zip
feat: initial commitHEADmain
Diffstat (limited to 'chapter9')
-rw-r--r--chapter9/fractal.cxx48
-rw-r--r--chapter9/fractal.obin0 -> 3233 bytes
-rw-r--r--chapter9/maze.cxx75
-rw-r--r--chapter9/powers.cxx59
-rw-r--r--chapter9/powers.exebin0 -> 62407 bytes
-rw-r--r--chapter9/powers.obin0 -> 3858 bytes
-rw-r--r--chapter9/recursionPrograms.zipbin0 -> 4930 bytes
-rw-r--r--chapter9/useful.cxx80
-rw-r--r--chapter9/useful.h46
-rw-r--r--chapter9/vertical.cxx47
-rw-r--r--chapter9/vertical.exebin0 -> 58448 bytes
-rw-r--r--chapter9/vertical.obin0 -> 3912 bytes
12 files changed, 355 insertions, 0 deletions
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 <cassert> // Provides assert
+#include <cstdlib> // Provides EXIT_SUCCESS
+#include <iostream> // 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
--- /dev/null
+++ b/chapter9/fractal.o
Binary files 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 <cctype> // Provides toupper
+#include <cstdlib> // Provides EXIT_SUCCESS
+#include <iostream.h> // 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 <cassert> // Provides assert
+#include <cstdlib> // Provides EXIT_SUCCESS
+#include <iostream> // 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
--- /dev/null
+++ b/chapter9/powers.exe
Binary files differ
diff --git a/chapter9/powers.o b/chapter9/powers.o
new file mode 100644
index 0000000..c966897
--- /dev/null
+++ b/chapter9/powers.o
Binary files differ
diff --git a/chapter9/recursionPrograms.zip b/chapter9/recursionPrograms.zip
new file mode 100644
index 0000000..a454335
--- /dev/null
+++ b/chapter9/recursionPrograms.zip
Binary files 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 <cassert> // Provides assert
+#include <cctype> // Provides toupper
+#include <cstdlib> // Provides rand, RAND_MAX
+#include <iostream.h> // 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 <iostream> // Provides cin, cout
+#include <cstdlib> // 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
--- /dev/null
+++ b/chapter9/vertical.exe
Binary files differ
diff --git a/chapter9/vertical.o b/chapter9/vertical.o
new file mode 100644
index 0000000..e68fe41
--- /dev/null
+++ b/chapter9/vertical.o
Binary files differ