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: execute_result
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)
```
%% 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,
unsigned int m, const unsigned int n )
```
%% Cell type:code id: tags:
``` C++14
#include <string>
#include <vector>
#include <sstream>
void readMatrix( const std::string & matrix,
std::vector<double> & values,
unsigned int & m, unsigned int & n )
{
values.clear();
std::istringstream iss(matrix);
std::string line;
m = 0;
n = 0;
while( std::getline( iss, line ) )
{
++m;
std::istringstream issLine( line );
double v;
n = 0;
while( issLine >> v )
{
++n;
values.push_back( v );
}
}
}
```
%% Cell type:code id: tags:
``` C++14
std::vector<int> vector;
for( int = 0; i < 100; ++i )
{
vector.assign( 100, i );
// ...
}
```
%% Cell type:code id: tags:
``` C++14
std::string matrixToString( const std::vector<double> & values,
const unsigned int m, const unsigned int n )
{
// we should check whether m * n == values.size() but no error handling yet...
std::ostringstream oss;
size_t idx = 0;
for( unsigned int i = 0; i < m; ++i )
{
if( i > 0 )
oss << "\n";
for( unsigned int j = 0; j < m; ++j )
{
if( j > 0 )
oss << " ";
oss << values[idx];
++idx;
}
}
return oss.str();
}
```
%% 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
```
%%%% Output: execute_result
{ 2, 3.2, 5.6, 7.8, 1, 4.3, 6.9, 4.4, 6.8, 2, 10.8, 9.1 }
%% Cell type:code id: tags:
``` C++14
m // should be 3
```
%%%% Output: execute_result
3
%% Cell type:code id: tags:
``` C++14
n // should be 4
```
%%%% Output: execute_result
4
%% Cell type:code id: tags:
``` C++14
matrixToString( values, m , n )
```
%%%% Output: execute_result
"2 3.2 5.6
7.8 1 4.3
6.9 4.4 6.8"
%% 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)
```
%% Cell type:code id: tags:
``` C++14
```
%% Cell type:code id: tags:
``` C++14
int i = 23;
```
%% Cell type:code id: tags:
``` C++14
i = 42;
```
%% Cell type:code id: tags:
``` C++14
const int ci = 42;
```
%% Cell type:code id: tags:
``` C++14
ci = 32;
```
%%%% Output: error
Interpreter Error:
%% Cell type:code id: tags:
``` C++14
const double dbl = 2.5;
```
%% Cell type:code id: tags:
``` C++14
int i2 = 34;
```
%% Cell type:code id: tags:
``` C++14
int &ir = i2;
```
%% Cell type:code id: tags:
``` C++14
const int &irc = i2;
```
%% Cell type:code id: tags:
``` C++14
#include <iostream>
```
%% Cell type:code id: tags:
``` C++14
std::cout << i2;
```
%% Cell type:code id: tags:
``` C++14
std::cout << irc;
```
%% Cell type:code id: tags:
``` C++14
i2 = 62;
```
%% Cell type:code id: tags:
``` C++14
irc = 32;
```
%%%% Output: error
Interpreter Error:
%% Cell type:code id: tags:
``` C++14
const int ic2 = 42;
```
%% Cell type:code id: tags:
``` C++14
const int &irc2 = i2;
```
%% Cell type:code id: tags:
``` C++14
int &irc2 = ic2;
```
%%%% Output: error
Interpreter Error:
%% Cell type:code id: tags:
``` C++14
int i = 23;
```
%% Cell type:code id: tags:
``` C++14
int *pi = &i;
```
%% Cell type:code id: tags:
``` C++14
int *const pci = &i;
```
%% Cell type:code id: tags:
``` C++14
#include <iostream>
std::cout << i;
```
%% Cell type:code id: tags:
``` C++14
*pi
```
%%%% Output: execute_result
23
%% Cell type:code id: tags:
``` C++14
*pci
```
%%%% Output: execute_result
23
%% Cell type:code id: tags:
``` C++14
*pi = 42;
```
%% Cell type:code id: tags:
``` C++14
*pi
```
%%%% Output: execute_result
42
%% Cell type:code id: tags:
``` C++14
*pci
```
%%%% Output: execute_result
42
%% Cell type:code id: tags:
``` C++14
*pci = 63;
```
%% Cell type:code id: tags:
``` C++14
*pi
```
%%%% Output: execute_result
63
%% Cell type:code id: tags:
``` C++14
pci = nullptr;
```
%%%% Output: error
Interpreter Error:
%% Cell type:code id: tags:
``` C++14
const int *pci2 = &i;
```
%% Cell type:code id: tags:
``` C++14
*pci2 = 52;
```
%%%% Output: error
Interpreter Error:
%% Cell type:code id: tags:
``` C++14
pci2 = nullptr;
```
%% Cell type:code id: tags:
``` C++14
const int * const pci3 = &i;
```
%% Cell type:code id: tags:
``` C++14
pci3 = nullptr;
```
%%%% Output: error
Interpreter Error:
%% Cell type:code id: tags:
``` C++14
*pci3 = 67;
```
%%%% Output: error
Interpreter Error:
%% Cell type:code id: tags:
``` C++14
```
%% Cell type:markdown id: tags:
# Exercise `const`
%% Cell type:markdown id: tags:
Which of the following initializations are legal? Explain why.
%% Cell type:markdown id: tags:
- `int i2 = -1, &r = 0;`
- `int *const p2 = &i2;`
- `const int i = -1, &r = 0;`
- `const int *const p3 = &i2;`
- `const int *p1 = &i2;`
- `const int &const r2;`
- `const int i2 = i, &r = i;`
%% Cell type:markdown id: tags:
Explain the following definitions. Identify any that are illegal.
%% Cell type:markdown id: tags:
- `int i, *const cp;`
- `int *p1, *const p2;`
- `const int ic, &r = ic;`
- `const int *const p3;`
- `const int *p;`
%% Cell type:markdown id: tags:
Using the variables in the previous exercise, which of the following assignments are legal? Explain why.
- `i = ic;`
- `p1 = p3;`