Hypotenuse and Towers of Hanoi

advertisement
09/23/13
Week 3 Assignments
Daniel Sharpe
Table of Contents
Problem 5.15 (Hypotenuse) and Approach– Page 2
Pseudocode – Page 3
Flowchart – Page 4
Code – Page 5
Problem 5.36 (Towers of Hanoi), Approach, and Recursion Reflection – Page 6
Pseudocode – Page 7
Code – Page 9
Problem:
1|Page
09/23/13
Week 3 Assignments
Daniel Sharpe
Define a function called hypotenuse that calculates the length of the hypotenuse of a right triangle
when the other two sides are given. Use this function in a program to determine the length of the
hypotenuse for each of the following triangles. The function should take two arguments of type
double and return the hypotenuse as a double. Test your program with the side values specified
below:
Triangle
Side 1
Side 2
1
3.0
4.0
2
5.0
12.0
3
8.0
15
Approach:
I know the formula to find the hypotenuse is the Pythagorean Theorem (a^2 + b^2 = c^2). My program
will then define a function that will square side 1, square side 2, add them together, then find the
square root of the sum of the two sides. It will then send this sum back to print to the screen for
Triangles 1, 2, and 3. I know that I the math.h library contains the square root function so I’ll use that
to help me calculate the square root. Since I’m using square root I’ll float all variables to avoid errors
with conversions.
Pseudocode
2|Page
09/23/13
Week 3 Assignments
Print the hypotenuse of 3 triangles using a function to calculate the hypotenuse.
Function Pythagorean Prototype (Float Side1 and Side2)
Initialization
Side 1
Side 2
Side 1 = 3, Side =4
Print Hypotenuse from Function for Triangle 1
Side 1 = 5, Side =12
Print Hypotenuse from Function for Triangle 2
Side 1 = 8 Side =15
Print Hypotenuse from Function for Triangle 3
Function Pythagorean (side1, side2)
Initialize hypotenuse and sum
Square side 1, square side 2, get sum
Find square root of the sum , get hypotenuse
Return hypotenuse
Flowchart
3|Page
Daniel Sharpe
09/23/13
Week 3 Assignments
Program Code
4|Page
Daniel Sharpe
09/23/13
#include <stdio.h>
#include <math.h>
Week 3 Assignments
Daniel Sharpe
//include math library for square root function
float pythagoreanTheorem (float side1, float side2); //Pythagorean theorem function
prototype to claculate two sides
float main (void) {
float side1, side2; //sides one and two to calculate and be assigned in printf
printf("A triangle with sides 3 and 4\n");
//Show user what's being
calculated
printf("has a hypotenuse of %.2f\n", pythagoreanTheorem(side1 = 3, side2 = 4));
//Assign side lengths, call function, print hypotenuse
printf("\n"); //space for readability in cmd prompt
printf("A triangle with sides 5 and 12\n"); //Show user what's being calculated
printf("has a hypotenuse of %.2f\n", pythagoreanTheorem(side1 = 5, side2 = 12));
//Assign side lengths, call function, print hypotenuse
printf("\n"); //space for readability in cmd prompt
printf("A triangle with sides 8 and 15\n"); //Show user what's being calculated
printf("has a hypotenuse of %.2f\n", pythagoreanTheorem(side1 = 8, side2 = 15));
//Assign side lengths, call function, print hypotenuse
printf("\n"); //space for readability in cmd prompt
}
//Our Pythagorean Theorem function that will calculate the two sides and find the
hypotenuse
float pythagoreanTheorem (float side1, float side2) { //side1 and side2 are parameter for
the function
float hypotenuse, sum;
//hypotenuse variable
sum = (side1 * side1) + (side2 * side2); //calculate the square of each side and
total
hypotenuse = sqrt(sum); //find the square root of the sum to get hypotenuse
return hypotenuse; //return hypotenuse to the Pythagorean function
}
5|Page
09/23/13
Week 3 Assignments
Daniel Sharpe
Problem:
Every budding computer scientist must grapple with certain classic problems, and the
Towers of Hanoi is one of the most famous of these. Legend has it that in a temple in
the Far East, priests are attempting to move a stack of disks from one peg to another.
The initial stack had 64 disks threaded onto one peg and arranged from bottom to top
by decreasing size. The priests are attempting to move the stack from this peg to a
second peg under the constraints that exactly one disk is moved at a time, and at no
time may a larger disk be placed above a smaller disk. A third peg is available for
temporarily holding the disks. Supposedly the world will end when the priests complete
their task, so there is little incentive for us to facilitate their efforts.
Let us assume that the priests are attempting to move the disks from peg 1 to peg 3.
We wish to develop an algorithm that will print the precise sequence of disk-to-disk peg
transfers.
If we were to approach this problem with conventional methods, we would rapidly find
ourselves hopelessly knotted up in managing the disks. Instead, if we attack the
problem with recursion in mind, it immediately becomes tractable. Moving n disks can
be viewed in terms of moving only n – 1 disks (and hence the recursion) as follows:
a. Move n – 1 disks from peg 1 to peg 2, using peg 3 as a temporary holding
area.
b. Move the last disk (the largest) from peg 1 to peg 3.
c. Move the n – 1 disks from peg 2 to peg 3, using peg 1 as a temporary holding
area.
The process ends when the last task involves moving n = 1 disk, i.e., the base case.
This is accomplished by trivially moving the disk without the need for a temporary
holding area.
Write a program to solve the Towers of Hanoi problem. Use a recursive function with
four parameters:
a.
b.
c.
d.
The number of disks to be moved
The peg on which these disks are initially threaded
The peg to which this stack of disks is to be moved
The peg to be used as a temporary holding area
Your program should print the precise instructions it will take to move the disks from the
starting peg to the destination peg. For example, to move a stack of three disks from
peg 1 to peg 3, your program should print the following series of moves:
1 → 3 (This means move one disk from peg 1 to peg 3.)
1→2
6|Page
09/23/13
Week 3 Assignments
Daniel Sharpe
3→2
1→3
2→1
2→3
1→3
Approach and Recursion
I know that Recursion focuses on the “base case” which to me, in the Tower of Hanoi, was one
disk. If there was only one disk then the program should automatically move the disk from tower 1 to
tower 3, skipping the holding tower 2. Since I knew this was my base case I wanted this to be my “if”
statement inside my Hanoi function. I knew I would need 4 parameters so I made a disk (for the disk
number, a start tower, a final tower, and temp tower). Since I knew from the book that our other two
“cases” would ne n-1 where the disk moved from peg 1 to 3 and n-1 where disk 1 moved from peg 2 to
1, I set up two calls back to the function, so that it could test these functions all the way to the base
case. For example, if I entered two disks, it skips the if statement where disk = 1 and goes to else,
however, my function doesn’t know how to solve for disk = 2 so it takes -1 from disks to get to the base
case which is disk =1. It then prints the start and final integers from the parameters and goes back to
disk two where it knows that the start is 1, but that it needs to move it to a temp holding spot in tower
2. What I’m not entirely sure about is if all this is stored in memory or if it’s destroyed every time
function goes back through the if else statements. I also know that if I enter different numbers of disks
that the order changes and I’m not sure why. This is troubleshooting that I’ll have to accomplish but I’ve
already spent 16 hours on this so I’ll have to come back to it later to make sure it gets turned in on time!
7|Page
09/23/13
Week 3 Assignments
Daniel Sharpe
Pseudocode
Set prototype variable hanoiAlgorithm (diskNumber, start, final, and temp)
Main
Set integers disk number, peg1 =1, peg2 =2, peg3 =3
Print instructions for Hanoi’s Tower
Scan in number
Call hanoiAlgorithm function
Function
If (1 Disk)
Move Disk from Tower 1 to Tower 3 (Base Case)
Else (n Disks)
Move n-1 disks from tower 1 to 3, , and 3 to 2
Print disk number and from which peg it came, and which peg it should move to
Move n-1 disks from tower 2 to 1, tower 2 to 3, and 1 to
8|Page
09/23/13
Week 3 Assignments
Daniel Sharpe
Code
#include <stdio.h>
//Towers of Hanoi Instructions using Recursive Function
void hanoiAlgorithm (int diskNumber, int start, int final, int temp); //hanoi function
prototype with four parameters (start peg, final peg, and temp peg)
int main (void) {
int diskNumber, peg1 = 1, peg2= 2, peg3 = 3; //variables for the hanoi function in
main, representing peg numbers
printf("Enter a number of n disks to see the instructions\n");
//instructions
printf("for moving each disk in the Tower of Hanoi problem.\n"); //instructions
scanf("%d", &diskNumber);
//take in number
hanoiAlgorithm(diskNumber, peg1, peg3, peg2);
main after taking in diskNumber
//call for hanoi function inside
}
void hanoiAlgorithm (int disk, int start, int final, int temp) { //hanoi function with
four parameters required including disks and 3 pegs
if ( disk == 1) { //base case rule (with one disk move it from peg 1 to peg 3
printf("For disk %d
%d --> %d\n", disk, start, final);
rule is if 1 disk, move from first peg to third peg
}
//base
else {
hanoiAlgorithm(disk - 1, start, temp, final);
//moving disks from peg 1, to peg 2, to peg 3
printf("For disk %d
%d --> %d\n", disk, start, final);
disk number, the peg it started on, and where it ended up
hanoiAlgorithm(disk - 1, temp, final, start);
//moving disks from peg 2, to peg 3, to peg 1
}
} //end function
9|Page
//print the
Download