1

This homework is to be prepared in teams of two students. Ask the lecturer to announce the teams to know who you'll be working with.

Download hw_1.tar.gz and extract it. Then add your homework solutions to the directory. Rename the directory according to the rules in the syllabus before submitting it as compressed archive. Don't forget to add the correct subject to the email when submitting.

All of the tasks below need to

in order to receive (full) points.

  1. Compute 1+5*6
    The first exercise concerns basic mathematics: Write a program for computing and printing the result of 1+5*6.
    A sample run of this program:
    31
    
    Name the program file: simplemath.cpp
  2. Perimeter and area
    The program below should print the perimeter and area of a circle given its radius. However, it contains errors. Fix the program.
    int main(); {
    double radius
    cout << "Enter a radius: ";
    cin >> radius;
    double area(radius * radius * M_PI);
    double perimeter(2 * radius * M_PI);
    cout << std::setprecision(2) << std::fixed;
    cout << "The circle has an area of " << area << "\n";
    cout << "The perimeter of this circle is " << perimeter << "\n";
    return 0
    }
    Name the program file: circle.cpp
  3. Imperial units
    Ask the user for a length in meters and print the corresponding length measured in inches, in feet, in yards, and in miles. Assume that one inch is 2.54 cm, one foot is 12 inches, one yard is 3 feet, and one British mile is 1760 yards. Round all results to two digits for printing.
    A sample run of this program:
    Enter a distance in meters: 1.3
    51.18 inch
    4.27 feet
    1.42 yards
    0.00 miles
    
    Name the program file: imperial_units.cpp
  4. Mathematical functions
    Write a program that prints the sine and cosine of all values between 0 and π in steps of 0.1. The last value must be π. All values in the sine and cosine columns should be fixed to 2 digits after the decimal point. Hint: check out <iomanip>.
    A sample run of this program:
    radians |  sine | cosine
    ------------------------
        0.0 |  0.00 |   1.00
        0.1 |  0.10 |   1.00
        0.2 |  0.20 |   0.98
    [...]
        3.0 |  0.14 |  -0.99
        3.1 |  0.04 |  -1.00
       3.14 |  0.00 |  -1.00
    
    Name the program file: trigonometry.cpp
  5. Distance between two points
    Write a program that asks the user for two points in a two-dimensional plane. The program must calculate and print the euclidean distance between the two points, rounded to 4 digits.
    A sample run of this program:
    First point's x-coordinate: 3.0
    First point's y-coordinate: 7.0
    Second point's x-coordinate: -2.0
    Second point's y-coordinate: 3.0
    The euclidean distance between the two points is 6.4031.
    
    Name the program file: distance.cpp
  6. Equation solver
    Write a function quadratic(a, b, c) that solves quadratic equations. The function does not have to be able to deal with input leading to complex numbers as results.
    Name the program file: equations.cpp
  7. Sum
    Write a function sum_to(num) that takes an integer num >= 0 as sole argument. The function should return the sum of all positive integers <= num. In the function, assert that the input is ≥ 0 by using assert. Hint: if you have trouble with warnings about types, have a look at Regular cast vs. static_cast vs. dynamic_cast.
    Name the program file: addition.cpp
  8. Geometry functions
    Implement the following functions calculating the perimeters, areas, surfaces or volumes of common geometric shapes:
    surface_cylinder(r, h)
    volume_cylinder(r, h)
    surface_cone(r, h)
    volume_cone(r, h)
    Name the program file: geometry.cpp
  9. Business functions
    Implement a function interest(capital, rate, years=1, tax=0) that returns the profit (compound interest) of investing money. If a positive tax value is entered, every year's interest is to be reduced by the tax. The rate and tax parameters must be between 0 (0%) and 1 (100%). The number of years is integral.
    Using the above function, implement a second function that returns the terminal value of investing money: terminal_value(capital, rate, years=1, tax=0).
    Name the program file: business.cpp
  10. Monte Carlo Simulation (rather easy, but still worth a bonus point)
    Simulation is an ingenious method of finding approximate solutions where other methods fail. A typical example is the calculation of π. Your task is to implement a simple simulation that calculates an approximation of π. To do so, imagine a 2 x 2 square with an inscribed circle with a radius of 1. To facilitate calculations, the center of the square should be the origin of a cartesian coordinate system. Generate random pairs of (x/y) coordinates inside the square and then test if the denoted point is inside the circle (x2+y21). Since the area of the circle is pi and the area of the square is 4, the approximate number of points inside the circle divided by the number of generated points is pi divided by 4 (hitstries=π4).
    (Hint: use the function drand48() (requires #include <stdlib.h>) to generate random numbers [0.0, 1.0).)
    Name the program file: simulation.cpp