Skip to content

Instantly share code, notes, and snippets.

@nathanhleung
Last active December 16, 2015 14:19
Show Gist options
  • Select an option

  • Save nathanhleung/e7bb2819bfff39c55d0f to your computer and use it in GitHub Desktop.

Select an option

Save nathanhleung/e7bb2819bfff39c55d0f to your computer and use it in GitHub Desktop.

Revisions

  1. nathanhleung revised this gist Dec 16, 2015. 1 changed file with 5 additions and 5 deletions.
    10 changes: 5 additions & 5 deletions returning-values.md
    Original file line number Diff line number Diff line change
    @@ -14,26 +14,26 @@
    4. Define a function called `squareArea` that computes the area of a square of a given `sideLength`.
    ```
    ```cpp
    double squareArea(double sideLength) {
    return pow(sideLength, 2.0);
    }
    ```

    5. Consider this function:

    ```
    ```cpp
    int mystery( int x, int y) {
    double result = (x + y ) / ( y - x);
    return result;
    }
    }
    ```
    What is the result of `mystery(2,3)` and `mystery(3,5)`.
    1. `5` and `4`.
    6. Implement a function that returns the minimum of three values.
    ```
    ```cpp
    // Prototype
    int findTheSmallest(int, int, int);
    // Implementation
    @@ -51,7 +51,7 @@
    ```

    ## Attached Code
    ```
    ```cpp
    #include <iostream>
    #include <cmath>

  2. nathanhleung revised this gist Dec 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion returning-values.md
    Original file line number Diff line number Diff line change
    @@ -6,7 +6,7 @@
    1. `cubeByVolume(8)` = `512`.
    3. Using the `pow()` function, write an alternate implementation of the `cubeByVolume` function.

    ```
    ```cpp
    double cubeByVolume(double sideLength) {
    return pow(sideLength, 3.0);
    }
  3. nathanhleung revised this gist Dec 16, 2015. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions returning-values.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    # Returning Values
    ## Questions
    1. What is the value of `cubeByVolume(9)`?
    1. 9^3 = 729.
    1. `729`.
    2. What is the value of `cubeByVolume(cubeByVolume(2))`?
    1. `cubeByVolume(8)` = 512.
    1. `cubeByVolume(8)` = `512`.
    3. Using the `pow()` function, write an alternate implementation of the `cubeByVolume` function.

    ```
    @@ -30,7 +30,7 @@
    ```

    What is the result of `mystery(2,3)` and `mystery(3,5)`.
    1. 5 and 4.
    1. `5` and `4`.
    6. Implement a function that returns the minimum of three values.

    ```
  4. nathanhleung revised this gist Dec 16, 2015. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions returning-values.md
    Original file line number Diff line number Diff line change
    @@ -5,27 +5,34 @@
    2. What is the value of `cubeByVolume(cubeByVolume(2))`?
    1. `cubeByVolume(8)` = 512.
    3. Using the `pow()` function, write an alternate implementation of the `cubeByVolume` function.

    ```
    double cubeByVolume(double sideLength) {
    return pow(sideLength, 3.0);
    }
    ```

    4. Define a function called `squareArea` that computes the area of a square of a given `sideLength`.

    ```
    double squareArea(double sideLength) {
    return pow(sideLength, 2.0);
    }
    ```

    5. Consider this function:

    ```
    int mystery( int x, int y) {
    double result = (x + y ) / ( y - x);
    return result;
    }
    ```

    What is the result of `mystery(2,3)` and `mystery(3,5)`.
    1. 5 and 4.
    6. Implement a function that returns the minimum of three values.

    ```
    // Prototype
    int findTheSmallest(int, int, int);
  5. nathanhleung revised this gist Dec 16, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions returning-values.md
    Original file line number Diff line number Diff line change
    @@ -3,7 +3,7 @@
    1. What is the value of `cubeByVolume(9)`?
    1. 9^3 = 729.
    2. What is the value of `cubeByVolume(cubeByVolume(2))`?
    a. `cubeByVolume(8)` = 512.
    1. `cubeByVolume(8)` = 512.
    3. Using the `pow()` function, write an alternate implementation of the `cubeByVolume` function.
    ```
    double cubeByVolume(double sideLength) {
    @@ -24,7 +24,7 @@ a. `cubeByVolume(8)` = 512.
    }
    ```
    What is the result of `mystery(2,3)` and `mystery(3,5)`.
    a. 5 and 4.
    1. 5 and 4.
    6. Implement a function that returns the minimum of three values.
    ```
    // Prototype
  6. nathanhleung revised this gist Dec 16, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion returning-values.md
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Returning Values
    ## Questions
    1. What is the value of `cubeByVolume(9)`?
    a. 9^3 = 729.
    1. 9^3 = 729.
    2. What is the value of `cubeByVolume(cubeByVolume(2))`?
    a. `cubeByVolume(8)` = 512.
    3. Using the `pow()` function, write an alternate implementation of the `cubeByVolume` function.
  7. nathanhleung revised this gist Dec 16, 2015. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions returning-values.md
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    # Returning Values
    ## Questions
    1. What is the value of `cubeByVolume(9)`?
    a. 9^3 = 729.
    a. 9^3 = 729.
    2. What is the value of `cubeByVolume(cubeByVolume(2))`?
    a. `cubeByVolume(8)` = 512.
    a. `cubeByVolume(8)` = 512.
    3. Using the `pow()` function, write an alternate implementation of the `cubeByVolume` function.
    ```
    double cubeByVolume(double sideLength) {
  8. nathanhleung created this gist Dec 16, 2015.
    96 changes: 96 additions & 0 deletions returning-values.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,96 @@
    # Returning Values
    ## Questions
    1. What is the value of `cubeByVolume(9)`?
    a. 9^3 = 729.
    2. What is the value of `cubeByVolume(cubeByVolume(2))`?
    a. `cubeByVolume(8)` = 512.
    3. Using the `pow()` function, write an alternate implementation of the `cubeByVolume` function.
    ```
    double cubeByVolume(double sideLength) {
    return pow(sideLength, 3.0);
    }
    ```
    4. Define a function called `squareArea` that computes the area of a square of a given `sideLength`.
    ```
    double squareArea(double sideLength) {
    return pow(sideLength, 2.0);
    }
    ```
    5. Consider this function:
    ```
    int mystery( int x, int y) {
    double result = (x + y ) / ( y - x);
    return result;
    }
    ```
    What is the result of `mystery(2,3)` and `mystery(3,5)`.
    a. 5 and 4.
    6. Implement a function that returns the minimum of three values.
    ```
    // Prototype
    int findTheSmallest(int, int, int);
    // Implementation
    int findTheSmallest(int x, int y, int z) {
    if (x < y && x < z) {
    return x;
    } else if (y < x && y < z) {
    return y;
    }
    return z;
    }
    // Function call
    findTheSmallest(1,2,3);
    // => 1
    ```

    ## Attached Code
    ```
    #include <iostream>
    #include <cmath>
    using namespace std;
    double steinbergPow( double, double);
    int main()
    {
    double result1;
    double result2;
    result1 = pow( 2.0, 4.0 );
    result2 = steinbergPow( 2.0, 4.0 );
    cout << "pow: " << result1 << endl;
    cout << "steinbergPow: " << result2 << endl;
    cout << endl;
    system("pause");
    return EXIT_SUCCESS;
    }
    /**
    Raises base to the power
    @param base is the base
    @param power is the exponent
    */
    double steinbergPow( double base, double power )
    {
    double sum = 1;
    for( int i = 0; i < power ; i++ )
    {
    sum = sum * base;
    }
    return sum;
    }
    /*
    Computes the volume of a cube.
    @param sideLength the side length of a cube
    @return the volume
    */
    double cubeByVolume( double sideLength )
    {
    double volume = sideLength * sideLength * sideLength;
    return volume;
    }
    ```