Created
September 23, 2011 05:28
-
-
Save b-adams/1236800 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Gets the user to pick a number | |
@sideeffect Prompts the user to enter a number | |
@returns The number the user entered | |
This function scans the user's input into a local variable, | |
and then returns the value stored in that variable to the calling code. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief The answer that the trick always comes up with | |
For not-very-mysterious algebraic reasons, this trick will always come up | |
with the same answer. Rather than have '2' randomly show up in our code as | |
a 'magic number', we give it a name here. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Pause until the user is ready to continue | |
@sideeffect Prompts the user to press enter | |
After printing a prompt, this function calls the getchar function | |
which will scan for the next letter in the stream. If the user presses | |
enter, a newline will be the next character, getchar will finish, and | |
the rest of the program will continue. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Walk the user through the arithmetic in the trick | |
@param theUsersNumber Whatever number the user picked | |
@sideeffect Displays the computations for each step of the trick. | |
@returns the result of going through the arithmetic of the trick | |
Displays values of 2^k for the various k, and | |
Walks through subtracting 2^0, multiplying by 2^2, adding 2^3, dividing by 2^1, | |
and then subtracting twice the original number. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Walk the user through the steps of the trick | |
@sideeffect Displays the instructions for each step of the trick, pausing between steps. | |
Instructions are: | |
Subtract 2^0, multiply by 2^2, add 2^3, divide by 2^1, | |
then subtract twice the original number. | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Double a number | |
@param n The number to double | |
@sideeffect None | |
@returns Twice n | |
Very simple function, should just return 2*n or n+n | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int twoToThe(int exponent) | |
{ | |
int theResult; | |
printf("-> Calling twoToThe(%d)\n",exponent); | |
if(exponent<0) theResult=1/twoToThe(-exponent); | |
else if(exponent==0) theResult=1; | |
else theResult=twice(twoToThe(exponent-1)); | |
printf("<- twoToThe(%d) returns %d\n",exponent, theResult); | |
return theResult; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
@brief Compute a power of 2 | |
@param exponent The exponent: how many times to multiply 2 by itself. | |
@sideeffect None | |
@returns 2^n | |
This works by using the fact that 2^n is twice 2^(n-1), and 2^0 is 1 | |
So, if the user gives 0 for n, this just returns n | |
Otherwise, it returns twice the value of twoToThe(n-1) | |
[We'll talk about this 'recursion' trick later in the class. It's very useful.] | |
*/ | |
int twoToThe(int exponent); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment