DM560, Introduction to Programming in C++

Sheet 13

Task 1

Drill for Chapter 24 on page 922.

  1. Print the size of a char, a short, an int, a long, a float, a double, an int*, and a double* (use sizeof, not <limits>).
  2. Print out the size as reported by sizeof of Matrix<int> a(10), Matrix<int> b(100), Matrix<double> c(10), Matrix<int,2> d(10,10), Matrix<int,3> e(10,10,10).
  3. Print out the number of elements of each of the Matrixes from 2.
  4. Write a program that takes ints from cin and outputs the sqrt() of each int, or “no square root” if sqrt(x) is illegal for some x (i.e., check your sqrt() return values).
  5. Read ten floating-point values from input and put them into a Matrix<double>. Matrix has no push_back() so be careful to handle an attempt to enter a wrong number of doubles. Print out the Matrix.
  6. Compute a multiplication table for [0,n)*[0,m) and represent it as a 2D Matrix. Take n and m from cin and print out the table nicely (assume that m is small enough that the results fit on a line).
  7. Read ten complex<double>s from cin (yes, cin supports >> for complex) and put them into a Matrix. Calculate and output the sum of the ten complex numbers.
  8. Read six ints into a Matrix<int,2> m(2,3) and print them out.

Task 2

How random is your default_random_engine? Write a program that takes two integers n and d as inputs and calls randint(n) d times, recording the result. Output the number of draws for each of [0:n) and “eyeball” how similar the counts are. Try with low values for n and with low values for d to see if drawing only a few random numbers causes obvious biases.

Task 3

Write a random number generator to use in the final project. It must generate the arrivals of users at the elevator system. It has to return a triple consisting of the interarrival time since the previous user arrived, the floor of arrival and the destination floor. The interarrival time has to be exponentially distributed while the floors are draw from a uniform integer distribution between two numbers. Draw the histogram of the interarrival times generated.

Task 4

Do exercises 4 and 5 of Chapter 24 on page 923. You find the code here.

Get the Gaussian elimination program to work; that is, complete it, get it to compile, and test it with a simple example.

Try the Gaussian elimination program with A=={ {0 1} {1 0} } and b=={ 5 6 } and watch it fail. Then, try elim_with_partial_pivot().