This exercise is available at
https://study.find-santa.eu/exercises/cpp/exercise_1/.
For the sake of the environment, please avoid printing these instructions in
the future. Thank you!
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
compile without warnings or errors
run with the provided input data without crashing
produce correct results
have proper documentation
be implemented without using non-English function and variable names,
docstrings etc.
in order to receive (full) points.
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
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.
intmain();{double radius
cout <<"Enter a radius: ";
cin >> radius;doublearea(radius * radius * M_PI);doubleperimeter(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";return0}
Name the program file: circle.cpp
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
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:
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
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
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
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
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
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 (). Since the area of the
circle is and the area of the square is 4, the approximate number of
points inside the circle divided by the number of generated points is
divided by 4 ().
(Hint: use the function drand48() (requires
#include <stdlib.h>) to generate
random numbers [0.0, 1.0).)
Name the program file: simulation.cpp