Commit 08856433 authored by Christian Godenschwager's avatar Christian Godenschwager
Browse files

Added course files

parent 11f4a0e1
.ipynb_checkpoints
\ No newline at end of file
%% Cell type:code id: tags:
``` C++14
template <typename T>
class Counter
{
public:
Counter();
void increment();
T getValue() const;
private:
T v_;
};
```
%% Cell type:code id: tags:
``` C++14
template< typename T >
Counter<T>::Counter() : v_(0) {}
```
%% Cell type:code id: tags:
``` C++14
template< typename T >
void Counter<T>::increment()
{
++v_;
}
```
%% Cell type:code id: tags:
``` C++14
template< typename T >
T Counter<T>::getValue() const
{
return v_;
}
```
%% Cell type:code id: tags:
``` C++14
Counter<short> ctr;
```
%% Cell type:code id: tags:
``` C++14
ctr.increment();
```
%% Cell type:code id: tags:
``` C++14
ctr.getValue()
```
%% Output
1
%% Cell type:code id: tags:
``` C++14
```
%% Cell type:markdown id: tags:
# Homework A
%% Cell type:markdown id: tags:
Write a function that parses the values from a matrix given as a string to a `std::vector`. A sample 3x4 matrix may be given like this:
```
2.0 3.2 5.6 7.8
1.0 4.3 6.9 4.4
6.8 2.0 10.8 9.1
```
Use `double` as a dataype for your vector. Store the values to the vector using [row-major order](https://en.wikipedia.org/wiki/Row-_and_column-major_order). Your function should also report the number of rows `m` and the number of columns `n` to the caller. Implement the following signature:
```c++
void readMatrix( const std::string & matrix, std::vector<double> & values, unsigned int & m, unsigned int & n )
```
Also write the opposite function that converts the matrix back to a string:
```c++
std::string matrixToString( const std::vector<double> & values, const unsigned int m, const unsigned int n )
```
%% Cell type:code id: tags:
``` C++14
// readMatrix
```
%% Cell type:code id: tags:
``` C++14
// matrixToString
```
%% Cell type:code id: tags:
``` C++14
std::string matrix("2.0 3.2 5.6 7.8\n1.0 4.3 6.9 4.4\n6.8 2.0 10.8 9.1");
```
%% Cell type:code id: tags:
``` C++14
unsigned int n, m;
std::vector<double> values;
```
%% Cell type:code id: tags:
``` C++14
#include <iostream>
readMatrix( matrix, values, m, n );
values
```
%% Cell type:code id: tags:
``` C++14
m // should be 3
```
%% Cell type:code id: tags:
``` C++14
n // should be 4
```
%% Cell type:code id: tags:
``` C++14
matrixToString( values, m , n )
```
%% Cell type:markdown id: tags:
Also test some corner cases:
%% Cell type:code id: tags:
``` C++14
std::string m1("2.0"); // 1x1
```
%% Cell type:code id: tags:
``` C++14
std::string m2(""); // empty matrix (0x0)
```