Pascal Schärli 29.03.2019
Pascal Schärli 29.03.2019
Pascal Schärli 15.03.2019
// PRE: x is roundable to a number in
// the value range of type int
// POST: return value is the integer
// nearest to x, or the one
// further away from 0 if x lies
// right in between two integers.
int round (double x) {
if(x > 0){
return x + .5;
}
else{
return x - .5;
}
}Pascal Schärli 29.03.2019
Pascal Schärli 15.03.2019
\(F*(\beta , p, e_{min} , e_{max})\)
\(\beta = 2\)
\(p = 3\)
\(e_{min} = -4\)
\(e_{max} = 4\)
| dez | bin | |
|---|---|---|
| + | ||
| = | ||
| + | ||
| = |
(10 + 0.5) + 0.5
| dez | bin | |
|---|---|---|
| 10 | 1.01 * 2^3 | |
| + | 0.5 | 0.0001 * 2^3 |
| = | 10 | 1.01 * 2^3 |
| + | 0.5 | 0.0001 * 2^3 |
| = |
10 | 1.01 * 2^3 |
Pascal Schärli 15.03.2019
\(F*(\beta , p, e_{min} , e_{max})\)
\(\beta = 2\)
\(p = 3\)
\(e_{min} = -4\)
\(e_{max} = 4\)
| dez | bin | |
|---|---|---|
| 0.5 | 1.00 * 2^-1 | |
| + | 0.5 | 1.00 * 2^-1 |
| = | 1 | 1.00 * 2^0 |
| + | 10 | 1010 * 2^0 |
| = | 12 | 1.10 * 2^3 |
(0.5 + 0.5) + 10
| dez | bin | |
|---|---|---|
| + | ||
| = | ||
| + | ||
| = |
Pascal Schärli 15.03.2019
Â
Â
Pascal Schärli 15.03.2019
std::vector<int> a; // empty vector of ints
std::vector<float> b(5); // five floats with value 0
std::vector<double> c(5,3); // five doubles with value 3Beispiele:
Pascal Schärli 15.03.2019
#include <vector>
#include <iostream>
int main(){
std::vector<int> values(5);
for(int i = 0; i < 5; i++){
values.at(i) = i * i;
}
// [..]values -> [0, 1, 4, 9, 16] // [...]
std::cout << values.at(3) << std::endl;
values.at(0) = 5;
std::cout << values.at(0) << std::endl;
return 0;
}Output:
3
5Pascal Schärli 15.03.2019
Vektoren sind keine Primitive Datentypen. Sie haben noch weitere Funktionalitäten:
| Name | Rückgabe-Typ | Funktionalität |
|---|---|---|
sizeunsigned intGrösse (Länge) vom Vektor
emptybooltrue falls Vektor leer ist
push_backvoidElement am Ende hinzufügen
pop_backvoidLöscht letztes Element
backTLetztes Element im Vektor
Eine komplette Liste findet ihr hier unter "Member Functions"
Pascal Schärli 15.03.2019
#include <iostream>
#include <vector>
int main(){
std::vector<int> test(5);
test[1] = test.size();
test.pop_back();
test[2] = test.size();
test.push_back(2);
test[3] = test.empty();
for(unsigned int i = 0; i < test.size(); i++){
std::cout << test.at(i) << " ";
}
std::cout << std::endl;
return 0;
}0 5 4 0 2Pascal Schärli 15.03.2019
std::vector<int> test(5);
0 0 0 0 00 5 0 0 00 5 0 00 5 4 00 5 4 0 20 5 4 0 2std::vector<int> test(5);
test.at(1) = test.size();std::vector<int> test(5);
test.at(1) = test.size();
test.pop_back();std::vector<int> test(5);
test.at(1) = test.size();
test.pop_back();
test.at(2) = test.size();std::vector<int> test(5);
test.at(1) = test.size();
test.pop_back();
test.at(2) = test.size();
test.push_back(2);std::vector<int> test(5);
test.at(1) = test.size();
test.pop_back();
test.at(2) = test.size();
test.push_back(2);
test.at(3) = test.empty();Pascal Schärli 15.03.2019
std::vector<int> v(5, 0);
v.at(5) = 3;
for(unsigned int i = 0; i < v.size(); i++){
std::cout << v.at(i) << " ";
}
terminate called after throwing an instance
of 'std::out_of_range'0
0
0
0
0
0
1
2
3
4
5
Indizierung bei 0 nicht vergessen! Bei Zugriff auf Daten, welche nicht existieren gibt es einen Fehler.
Inhalt
Index
Pascal Schärli 29.03.2019
#include <iostream>
#include <vector>
#include <ios>
// POST: Converts lowercase letters a-z to upper case A-Z
void char_to_upper(char& letter) {
// TODO
}
// POST: Converts all letters to upper-case.
void to_upper(std::vector<char>& letters) {
// TODO
}
int main () {
// Say to std::cin not to ignore whitespace.
std::cin >> std::noskipws;
std::vector<char> letters;
char ch;
// Step 1: Read input.
do {
std::cin >> ch;
letters.push_back(ch);
} while (ch != '\n');
// Step 2: Convert to upper-case.
// TODO
// Step 3: Output.
// TODO
return 0;
}Skizziert ein Programm, welches einen gegebenen Text in Grossbuchstaben umwanden kann:
H3llo World!H3LLO WORLD!Pascal Schärli 29.03.2019
// POST: Converts the letter to upper case.
void char_to_upper(char& letter) {
if ('a' <= letter && letter <= 'z') {
letter -= 'a' - 'A'; // 'a' > 'A'
}
}// POST: Converts all letters to upper-case.
void to_upper(std::vector<char>& letters) {
for (unsigned int i = 0; i < letters.size(); ++i) {
char_to_upper(letters[i]);
}
}Pascal Schärli 29.03.2019
int main () {
// Say to std::cin not to ignore whitespace.
std::cin >> std::noskipws;
std::vector<char> letters;
char ch;
// Step 1: Read input.
do {
std::cin >> ch;
letters.push_back(ch);
} while (ch != '\n');
// Step 2: Convert to upper-case.
to_upper(letters);
// Step 3: Output.
for (unsigned int i = 0; i < letters.size(); ++i) {
std::cout << letters[i];
}
return 0;
}Pascal Schärli 29.03.2019
In C++ können wir existierende Variablen "Verlinken"
int a = 3;
int& b = a;
b = 2;
std::cout << a;2Pascal Schärli 29.03.2019
#include <iostream>
void foo(int a, int b){
int temp = a;
a = b;
b = temp;
}
int main(){
int a = 5;
int b = 7;
foo(a, b);
std::cout << a << " " << b;
return 0;
}5 7#include <iostream>
void foo(int& a, int& b){
int temp = a;
a = b;
b = temp;
}
int main(){
int a = 5;
int b = 7;
foo(a, b);
std::cout << a << " " << b;
return 0;
}7 5Call by Value
Call by Reference
Pascal Schärli 29.03.2019
Warum braucht es Referenzen überhaupt?
void scale(double& x, double& y, double amount){
x *= amount;
y *= amount;
}void read_i (Vector& v, unsigned int i);std::ostream o = std::cout;
o << "It works!\n";std::ostream& o = std::cout;
o << "It works!\n";It works!error
Pascal Schärli 29.03.2019
Der Rückgabetyp von Funktionen kann auch eine Referenz sein:
#include <iostream>
int square(int n){
return n*=n;
}
int main(){
int n = 3;
square(square(n));
std::cout << n;
return 0;
}3#include <iostream>
int& square(int& n){
return n*=n;
}
int main(){
int n = 3;
square(square(n));
std::cout << n;
return 0;
}81#include <iostream>
int square(int& n){
return n*=n;
}
int main(){
int n = 3;
square(square(n));
std::cout << n;
return 0;
}error: cannot bind
non-const lvalue
reference of type ‘int&’
to an rvalue of type ‘int’Pascal Schärli 29.03.2019
Ein Character ist ein Buchstabe, welchen in C++ als 8-Bit Zahl gespeichert wird. Die Buchstaben sind mit ASCII Codiert.
"\n""\r"Pascal Schärli 29.03.2019
char c1 = 'i';
std::cout << c1;ichar c2 = 110;
std::cout << c2;nchar c3 = c1 - 3;
std::cout << c3;fchar c4 = c1 - 'a' + 'A';
std::cout << c4;IPascal Schärli 29.03.2019
int main () {
unsigned int length;
std::vector<int> sequence (10);
// Step 1: Read input.
std::cin >> length;
if (length > 10) {
std::cout << "Bad input" << std::endl;
return 0;
}
// TODO: Read Vector from std::cin
// Reverse sequence;
reverse(sequence, length);
// TODO: Output Vector to std::cout
return 0;
}Skizziert ein Programm, welches einen gegebenen Vektor umdrehen kann.
0 1 2 3 4 5 6 7 8 9// POST: the targets of i and j got their
// values swapped
void swap (int& i, int& j) {
// TODO
}
// PRE: length is the real length of the
// vector sequence.
// POST: Reverses the sequence stored in
// the vector sequence.
void reverse(std::vector<int>& sequence,
unsigned int length) {
// TODO
}
9 8 7 6 5 4 3 2 1 0Pascal Schärli 29.03.2019
// POST: the targets of i and j got their values swapped
void swap (int& i, int& j) {
const int tmp = i;
i = j;
j = tmp;
}// PRE: length is the real length of the vector sequence.
// POST: Reverses the sequence stored in the vector sequence.
void reverse(std::vector<int>& sequence, unsigned int length) {
if (length > 1) {
int front = 0;
int back = length-1;
while (front < back) {
swap(sequence.at(front), sequence.at(back));
++front;
--back;
}
}
}Pascal Schärli 29.03.2019
int main () {
unsigned int length;
std::vector<int> sequence (10);
// Step 1: Read input.
std::cin >> length;
if (length > 10) {
std::cout << "Bad input" << std::endl;
return 0;
}
for (unsigned int i = 0; i < length; ++i) {
std::cin >> sequence.at(i);
}
// Step 2: Reverse sequence;
reverse(sequence, length);
// Step 3: Output
for (unsigned int i = 0; i < length; ++i) {
std::cout << sequence.at(i) << " ";
}
std::cout << std::endl;
return 0;
}Pascal Schärli 29.03.2019
#include <iostream>
int foo (int a, int b) {
a += b;
return a;
}
int main() {
int a = 0;
int b = 1;
for (int i=0; i<5; ++i) {
b = foo (a, b);
std::cout << b << " ";
}
return 0;
}1 1 1 1 1Was ist der Output von diesem Programm?
Pascal Schärli 29.03.2019
#include <iostream>
int foo (int& a, int b) {
a += b;
return a;
}
int main() {
int a = 0;
int b = 1;
for (int i=0; i<5; ++i) {
b = foo (a, b);
std::cout << b << " ";
}
return 0;
}1 2 4 8 16Was ist der Output von diesem Programm?
Pascal Schärli 29.03.2019
#include <iostream>
int foo (int a, int& b) {
a += b;
return a;
}
int main() {
int a = 0;
int b = 1;
for (int i=0; i<5; ++i) {
b = foo (a, b);
std::cout << b << " ";
}
return 0;
}1 1 1 1 1Was ist der Output von diesem Programm?
Pascal Schärli 15.03.2019
Mit dem Keyword const kann man bestimmen, dass gewisse Variabeln Konstant sind, sich also nicht veränderen können.
Â
Pascal Schärli 15.03.2019
#include <iostream>
int foo(const int x){
x = 5;
return x;
}
int main(){
std::cout << foo(3) << std::endl;
return 0;
}error: assignment of read-only parameter ‘x’Pascal Schärli 15.03.2019
#include <iostream>
int& foo(int x){
x = 5;
return x;
}
int main(){
std::cout << foo(3) << std::endl;
return 0;
}Segmentation fault (core dumped)Pascal Schärli 29.03.2019
Pascal Schärli 29.03.2019
T foo (S i)
{
return ++i;
}1. T = int, S = int
2. T = int, S = const int
3. T = int, S = int&
4. T = int&, S = int
5. T = const int&, S = int&Pascal Schärli 29.03.2019
Schreibt folgendes Programm:
Tipps:
void read_vector(std::vector<int> & values) {/*...*/}
int occurrences(const std::vector<int> & values, int toFind) {/*...*/}Pascal Schärli 29.03.2019
Schreibt folgendes Programm:
22 35 4 16 42 3 -13Tipps:
Jetzt könnt ihr eure einlese-Funktion von der Aufgabe 2 wiederverwenden
Pascal Schärli 29.03.2019
Schreibt folgendes Programm:
Tipps:
Pascal Schärli 29.03.2019
Pascal Schärli 29.03.2019