An introduction to C++#
Exercise 1.1. The sum of two integers#
a) Write a program that adds two integers a and b and writes out their sum.
b) Modify the program from a) so that it prompts the user for the values of the two integers.
Filename: sum_of_two.cpp
Exercise 1.2. Cooking an egg#
The following expression gives the time it takes (in seconds) for the center of the yolk to reach the
temperature
Here
Filename: egg.cpp
Exercise 1.3. Sum of first n integers#
a) Write a program that computes the sum of the integers from 1 up to (and including)
. Compare with the value of .b) Modify the program so that n is asked for in the terminal.
Filename: integers.cpp
Exercise 1.4. Generate an approximate Fahrenheit-Celsius conversion table#
The formula for converting
Filename: f2c_approx.cpp
Exercise 1.5. The sum of n integers#
Write a program that asks the user how many numbers he wants to add and asks for the value of these numbers. Then the program should print the sum of the numbers.
Filename: sum_many.cpp
1
Exercise 1.6. The sum of n integers from command line#
Write a program that prints the sum of the command line arguments.
Hint. For converting from char
to int
you can use the function atoi
.
Filename: sum_command.cpp
Exercise 1.7. Making a function in C++#
Make a function called half
that takes an integer argument. The function must print the number it received to the screen, then the program should divide that number by two to make a new number. If the new number is not zero the function then calls the function half
passing it the new number
as its argument. If the number is zero then the function exits.
Call the function half
with an argument of 100, the screen output should be
100
50
25
...
...
1
Filename: half.cpp
Exercise 1.8. Making an array#
Make an array with for
loop. Print out the elements of the array to check that the result is as wanted.
Filename: array.cpp
Exercise 1.9. Cooking more eggs#
a) Modify your program from Exercise 1.2 so that you get a function returning the time it takes for the center of the yolk to reach a temperature
when the egg had a temperature before cooking. Check that you get the same result as in Exercise 1.2 for and .b) Make an array of
values, . Then declare an arrayt
of the same length for time values. Make afor
loop and fill in thet
array.c) Use a
for
loop to print out a nicely formatted table of values and the correspondingt
values.
Filename: eggs.cpp
Exercise 1.10. Stirling’s approximation#
Stirling’s approximation is
a) Write a function taking an integer value
as argument that returns Stirling’s approximation to .b) Print a nicely formatted table with three columns:
, and Stirling’s approximation to for . Also write this table to a file named stirling.txt. Hint. To compute you can uselgamma
from<cmath>
Then .c) Make sure you have stored the
values in an arrayx
. Then declare two arraysexact
andapprox
of the same length asx
. Use afor
loop to fill these arrays with the exact value and the approximated value of for each value in the arrayx
.
Filename: stirling.cpp
Exercise 1.11. Primality checker#
Recall that a prime number is a number greater than 1 that has exactly 2 divisors. Said differently, a number greater than one is a prime if it is divisible by only itself and one. Every number
Make a function that takes a number
Hint: You will only need to check divisibility for numbers up to and including
Filename: prime.cpp
Exercise 1.12. Euler’s totient function#
Two numbers
a) Make a function that takes two numbers and returns true if they’re relatively prime and false if they’re not.
b) Euler’s totient function is defined as
Implement Euler’s totient function and print
Filename: euler.cpp
Exercise 1.13. Converting from base n to decimal#
Make a function long convert_n(long number, int n)
that converts a number from base
Hint: Remember that a number /
and %
.
Filename: convert_n.cpp
Exercise 1.14. Converting from hexadecimal to decimal#
Make a function long convert_hex(string number)
that converts a number of the type string from hexadecimal to decimal. The answer should be returned as type long.
Hint: When converting from string
to int
or long
, subtract the zero string '0'
before converting or use the function atoi
.
Filename: convert_hex.cpp
Exercise 1.15. Add two binary numbers#
a) Make a function
long add_binary(long a, long b)
that adds two binary numbers and .
Hint: Add like you do by hand: start with the last digits. You can use a while loop containing
sum += long(a%10 + b%10 + r)%2)*pow(10, i++);
r = int((a % 10 + b % 10 + r) / 2);
a /= 10;
b /= 10;
Think about what r
is. What should you do if r
is non-zero after you have gone through all the
digits?
b) Modify your program from a) so that you can add any two numbers of base
for .
Filename: add_n.cpp
Exercise 1.16. Adding fractions#
Write a program to add two fractions and display the resulting fraction. Your program will prompt the user to input the two fractions. The numerator and denominator of each fraction are input separately by space, as illustrated below.
Enter fraction 1 (numerator denominator): 1 3
Enter fraction 2 (numerator denominator): 2 5
Result: 11/15
Hint: You will need to use a struct
to define a fraction. The struct
has two members: numerator
and denominator
.
Filename: fraction.cpp
Exercise 1.17. Adding and simplifying fractions#
Modify your program from Exercise 1.16 so that the result is the fraction in it’s simplest form. You should make a function simplify
that takes in a fraction and simplifies it. Let the function be of
the type void
. An example from the terminal:
Enter fraction 1 (numerator denominator): 1 3
Enter fraction 2 (numerator denominator): 2 6
Result: 2/3
Hint: For the function to be able to change the value of the fraction, the argument of the function
should be a reference variable: simplify(Fraction& fract)
where Fraction
is the name of the struct
for fractions.
Filename: fraction2.cpp
Exercise 1.18. Make a class for rectangles#
Make a class Rectangle
that has two private variables and one member function which will return the area of the rectangle.
Filename: Rectangle.cpp
Exercise 1.19. Make a class for cooking eggs#
a) Make a class Cook_egg that has a public function taking no arguments which returns the time it takes for the egg to be cooked.
b) Add two different methods for changing the mass of the egg. The first method takes the mass of the egg in grams as argument and reassigns the mass of the egg to this mass. The other method takes
'S'
,'M'
or'L'
as arguments and changes the mass to 47g, 57g or 67g respectively.c) Add two new methods: one for changing the initial temperature of the egg and one for changing the desired final temperature of the yolk.
Filename: Cook_egg.cpp
Exercise 1.20. Make a class for quadratic functions#
Consider a quadratic function Quadratic
for representing a
, b
and c
are initial arguments. The class should have three methods: value
, table
and
roots
. The value
method should compute the value of table
method should write out a table of roots
method should compute the two roots of the quadratic function. It should accept complex roots.
Hint. For the roots
method to be able to return two values consider making a structure Two_vals
containing two values.
Filename: Quadratic.cpp
Exercise 1.21. Points in different coordinate systems#
Make a class Point
to represent a point SphericalPoint
that inherits from Point. The subclass should take the spherical representation of a point
Verify the implementation by initializing three points (e.g. the three Cartesian unit vectors) as spherical points and print the corresponding Cartesian coordinates by calling the methods for getting the value of
Filename: Point.cpp
Exercise 1.22. Numerical approximations for the derivative#
Let f(x)
be a function and f'(x)
its derivative. There are many ways to approximate the derivative, some of which are:
Make a class Diff
with a function diff1
, diff2
, and diff3
for approximating the derivative using the above formulas. The class should also have a method set_h
for changing the value of
You should let
Hint: To send a function as an argument to a class you should use pointers, i.e. let the beginning of the class be
class Diff
{
private:
double (*f)(double x);
double h;
public:
Diff(double function(double x), double _h = 0.001)
{
f = function;
h = _h;
}
Filename: Diff.cpp
Exercise 1.23. Numerical approximations for integration#
Let
where
Simpson’s rule gives the following approximation to the integral:
where
Make a class Integration
where you implement the three different rules. Include also a method for changing the number of intervals
Filename: Integration.cpp
Exercise 1.24. Newton’s method#
Make a class Function which is a subclass of Diff
from Exercise 1.22. It should take a function call
that takes
where we give a starting point approx_root
that takes a starting point and a bound
Hint: Implement a simple convergence test. Check that
Filename: Function.cpp