Übungsstunde: Informatik 1 D-MAVT

Similar documents
File I/O Christian Schumacher, Info1 D-MAVT 2013

Connection Guide Link ECU

Informatik I (D-ITET) Übungsstunde 9, Hossein Shafagh

Exercise 3 Logical Operators, Branching, Loops. Informatik I für Mathematiker und Physiker (HS 2015) Yeara Kozlov

13. Arrays II. Strings as Arrays. Texts. Strings: pimped char-arrays. char text[] a =... can be initialized via string literals. char text[] = "bool"

Search Engines Chapter 2 Architecture Felix Naumann

1. Übungsblatt. Vorlesung Embedded System Security SS 2017 Trusted Computing Konzepte. Beispiellösung

12. Arrays II. Strings as Arrays. Texts. Strings: pimped char-arrays. char text[] = { b, o, o, l } char text[] = "bool" text.

Übungsblatt 2. Aufgabe 1 (Klassifikationen von Betriebssystemen)

Modern and Lucid C++ for Professional Programmers. Week 15 Exam Preparation. Department I - C Plus Plus

10/25/ Recursion. Objectives. Harald Gall, Prof. Dr. Institut für Informatik Universität Zürich.

4. Multicast Multicast Principles. Why is Multicast Important for. Multimedia? Definition of Multicast

USB. USB Sticks in Design und Qualität

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

PHY4321 Summary Notes

Exercise 1.1 Hello world

CS302 - Data Structures using C++

Input and Output. Data Processing Course, I. Hrivnacova, IPN Orsay

Working with Strings. Lecture 2. Hartmut Kaiser. hkaiser/spring_2015/csc1254.html

Writing a Good Program. 7. Stream I/O

TI-No. 4002TI05.doc PAGE NO. : 1/1. Settings after Installation of the Firmware Version 74

Informatik II. Andreas Bärtschi, Andreea Ciuprina, Felix Friedrich, Patrick Gruntz, Hermann Lehner, Max Rossmannek, Chris Wendler FS 2018

1 enum class -- scoped and strongly typed enums

AC500. Application Note. Scalable PLC for Individual Automation. AC500-S safety PLC - Overview of changes in Automation Builder 2.1.x and 2.0.

Übersicht über die grundlegenden C++-Elemente

1. Introduction. What is Computer Science? Informatics Science of Computers. Computer Science Informatics

G52CPP C++ Programming Lecture 17

Input/output. Remember std::ostream? std::istream std::ostream. std::ostream cin std::istream. namespace std { class ostream { /*...

Aufgabe 2. Join-Methoden Differential Snapshots. Ulf Leser Wissensmanagement in der Bioinformatik

Week 3: File I/O and Formatting 3.7 Formatting Output

Proxy Pattern (et Relata) (seen from someone still living in the '70, the '80 and, partly, in the '90)

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

CANOpen DS402 at KEBA

Exercise 7 References, Arrays, Vectors

CMPS 221 Sample Final

Usability Engineering

C++ does not, as a part of the language, define how data are sent out and read into the program

11. Reference Types. Swap! Reference Types: Definition. Reference Types

Installing and Configuring Windows 10 MOC

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 1/9/ Review. Here s a simple C++ program:

Neuigkeiten aus dem Oracle-Cloud-Portfolio - Fokus auf Infrastructure-as-a-Service

Looping and Counting. Lecture 3 Hartmut Kaiser hkaiser/fall_2012/csc1254.html

Ulrich Stärk

Looping and Counting. Lecture 3. Hartmut Kaiser hkaiser/fall_2011/csc1254.html

Informatik I (D-ITET) Übungsstunde 6, Hossein Shafagh

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

ProCANopen Questions & Answers Version Application Note AN-ION

Strings and Stream I/O

Lecture 5 Files and Streams

Integration of Ongoing Integers into PostgreSQL

G52CPP C++ Programming Lecture 14. Dr Jason Atkin

AvO-Übung 6 Rechnerübung zu Ice (2)

PIC10B/1 Winter 2014 Exam I Study Guide

Dwg viewer free download vista. Dwg viewer free download vista.zip

File Operations. Lecture 16 COP 3014 Spring April 18, 2018

<Intellectual output O6 >

Read-only Transportable Tablespaces 11g <> 12c Bodo von Neuhaus

SD2IEC evo². Revision E3b

DVI KVM switches DVIMUX 7.5

Vorlesung Datenstrukturen und Algorithmen Letzte Vorlesung Felix Friedrich, Map/Reduce Sorting Networks Prüfung

Call a spanning tree T of G end-faithful if the natural map : (T )! (G)! 7!! 0! is 1 1 and onto. Examples: neither need be the case.

Designing Database Solutions for Microsoft SQL Server 2012 MOC 20465

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

estos ECSTA for Teles Voice Application Server

Computer Science. 1. Introduction. What is Computer Science? Computer Science vs. Computers

Assignment 2 Solution

Installation, Storage and Compute with Windows Server Online-Training Examen 740. Ausbildungsinhalte. ITKservice

WebTransactions V7.1. Supplement

How to apply with the Beuth online application system

Lecture 3 The character, string data Types Files

Anleitung zur Schnellinstallation TEW-684UB 1.01

8. Negation 8-1. Deductive Databases and Logic Programming. (Winter 2007/2008) Chapter 8: Negation. Motivation, Differences to Logical Negation

AUTOMATISIERUNG DER INFRASTRUKTUR

Plan Based Thread Scheduling on HPC Nodes

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

Continuous Delivery. für Java Anwendungen. Axel Fontaine Software Development Expert

Introduction to Computational Linguistics

Consistency, clarity, simplification and continuous maintenance. Release 5

PIC Council Charter. Jürgen Wagner, Friedhelm Krebs. SAP AG Version 1.1 April 26 th, 2004

This chapter introduces the notion of namespace. We also describe how to manage input and output with C++ commands via the terminal or files.

Given the C++ declaration statement below, which of the following represents the value of exforsys? e) None of the above. 1K

CS242 COMPUTER PROGRAMMING

The activities described in these upgrade instructions may only be performed by engineers or maintenance/technical staff.

Rib Design to Increase Stiffness of Housings

SECTION A (15 MARKS) Answer ALL Questions. Each Question carries ONE Mark. 1 (a) Choose the correct answer: (10 Marks)

Introduction. Lecture 5 Files and Streams FILE * FILE *

Streams. Rupesh Nasre.

Einführung in die Kognitive Ergonomie

Developing Microsoft Azure Solutions MOC 20532

Exercise 9 Pointers, Iterators and Recursion

FXD A new exchange format for fault symptom descriptions

Emerging Technologies Workshops

Assignment operator string class c++ Assignment operator string class c++.zip

4 Strings and Streams. Testing.

Datenblatt: True Flat Touch Screens

5. Garbage Collection

11. Recursion. n (n 1)!, otherwise. Mathematical Recursion. Recursion in Java: Infinite Recursion. 1, if n 1. n! =

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

Deploying & Managing Windows 10 Using Enterprise Services

Arrow Citrix Monthly. Update. D i e t m a r K o p f B u s i n e s s D e v e l o p m e n t M a n a g e r -

Transcription:

Übungsstunde: Informatik 1 D-MAVT Daniel Bogado Duffner Übungsslides unter: n.ethz.ch/~bodaniel Bei Fragen: bodaniel@student.ethz.ch Daniel Bogado Duffner 18.04.2018 1

Ablauf Sorting Multidimensionale Arrays und Vektoren Darstellung einer Matrix in einem Array Strings Lindenmayer Systems and Turtle graphics Pointer / Zeiger Übung 7 Vorstellung und Tipps Daniel Bogado Duffner 18.04.2018 2

Arrays - Kurzübersicht Arrays sind 0 indexiert (Zählung fängt bei 0 an) Arrays können immer nur Ausdrücke von einem Typ (int, bool, ) speichern int test1[]; geht nicht. Grösse muss bei Compilezeit festliegen! 3

Algorithm Bubble Sort

Algorithm Bubble Sort int a[] = {6, 1, 3, 2, 1}; for (int i = 4; i >= 0; --i) for (int j = 0; j < i; ++j) if (a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } 5

Algorithm Bubble Sort int a[] = {6, 1, 3, 2, 1}; for (int i = 4; i >= 0; --i) for (int j = 0; j < i; ++j) if (a[j] > a[j+1]) { int temp = a[j]; a[j] = a[j+1]; a[j+1] = temp; } Note: Do not consider previous maxima. 1 3 2 1 6 6

Nachtrag letzte Woche Vektoren können nicht zu Beginn unterschiedliche Zahlen zugewiesen bekommen int a[]={1,4,5}; // Als Array geht das std::vector<int> numbers (n, 0); // Als Vektor geht das nicht Daniel Bogado Duffner 18.04.2018 7

Mehrdimensionale Arrays/Vektoren Mehrdimensionale Arrays: Mehrdimensionale Vektoren: int array[4][3] for (int i = 0; i < 4; i++) for (int j = 0; j < 3; j++) std::cin >> array[i][j]; int m = 4; int n = 3; std::vector< std::vector<int> > vector (m, std::vector<int>(n)); for (unsigned int i = 0; i < m; i++) for (unsigned int j = 0; j < n; j++) std::cin >> vector[i][j]; Daniel Bogado Duffner 18.04.2018 8

Representation of a Matrix in an Array

Matrix in an Array Matrix (m x n) i i, j, k start from 0. j Index: k = i n + j k =? Array (length mn) 10

Strings Müssen mit #include <string> hinzugefügt werden Funktionieren wie Vektoren (.push_back(),.at() ) Besitzen einen += Operator, mit dem Text angefügt werden kann Können ohne Schleife ausgegeben werden Daniel Bogado Duffner 18.04.2018 11

Strings 1 #include <iostream> 2 #include <string> 3 4 int main () { 5 6 std::string text; 7 std::cin >> text; // reads in a text of arbitrary length, for example "Hello" 8 9 text += " world!"; // appends text to the string, in this case changing it to 10 // "Hello world!" 11 12 std::string text2 = text; // initialization also works with a string variable on 13 // the right hand side, in this case text2 = text 14 15 std::cout << text2 << "\n"; // outputs whole text stored in text2, here 16 // "Hello world!" 17 return 0; 18 } Daniel Bogado Duffner 18.04.2018 12

Strings 1 #include <iostream> 2 #include <string> 3 4 int main () { 5 6 std::string str ("The quick brown fox jumps over the lazy dog."); 7 8 std::cout << str.find("fox") << "\n"; // outputs 16 9 std::cout << str.find("fox", 30) << "\n"; // outputs std::string::npos 10 // (substring not found) 11 str.replace(10, 5, "red"); 12 std::cout << str << "\n"; //outputs "The quick red fox jumps over the lazy dog. 13 14 str.erase(10,4); 15 std::cout << str << "\n"; // outputs "The quick fox jumps over the lazy dog." 16 17 return 0; 18 } Daniel Bogado Duffner 18.04.2018 13

We have the following functions: Strings find (const string& string, int pos = 0), // gibt die Stelle in der das gesuchte Wort beginnt an replace (int pos, int count, const string& string) // ersetzt Wort im Bereich [pos, pos+count) erase (int pos, int count) // löscht den Bereich [pos, pos+count) Daniel Bogado Duffner 18.04.2018 14

Lindenmayer Systems

Lindenmayer Systems Characterized by three parameters: 1. Alphabet - the allowed symbols 2. Production P - how to replace each symbol 3. Initial word s - the word to start with Example: 1. 1.

Lindenmayer Systems Characterized by three parameters: 1. Alphabet - the allowed symbols 2. Production P - how to replace each symbol 3. Initial word s - the word to start with lexamplele: 1. 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Lindenmayer Systems How does it look after 3 rounds? s: w : 1 : w 2 w 3 : F F+F+ F+F++F+F++ F+F++F+F+++F+F++F+F+++ 1.

Draw Lindenmayer Systems

Two Step Procedure Goal: Draw n-th step of Lindenmayer system Done in 2 steps: 1. Obtain n-th step 2. Draw it

Step 1 Obtain n-th Word Write and use the following two functions std::string production (const char c) In: symbol e.g. F Out: its production e.g. F+F+

Step 1 Obtain n-th Word Write and use the following two functions std::string production (const char c) In: symbol e.g. F Out: its production e.g. F+F+ std::string next_word (std::string& word) In: w (Word of step n) e.g. FF Out: w (Word n of step n+1) e.g. F+F+F+F+ Applies production n+1 to each character in w and concatenates the results. n

Step 2 Draw It Idea: view alphabet as turtle commands Example: Alphabet: := { F, +, - } F: turtle::forward() +: turtle::left(90) - : turtle::right(90)

Turtle Commands (aus: Skriptaufgabe 5) 42

Moving the Turtle C++ Commands Step (drawn): Step (not drawn):turtle::jump(); Rotation left: turtle::left(my_angle); Rotation right: turtle::right(my_angle); Save position: turtle::save(); Load position: turtle::restore(); Color cycling: turtle::colorcycle(); Requires: #include "turtle.cpp" 43

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 44

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 45

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 46

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 47

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 48

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 49

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 50

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 51

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 52

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 53

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 54

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 55

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 56

Moving the Turtle turtle::save(); turtle::left(45); turtle::jump(); turtle::save(); turtle::right(45); turtle::forward(2); turtle::restore(); turtle::restore(); turtle::right(45); 57

Pointer / Zeiger Ähnlich wie Referenzen, aber können auch im Nachhinein verändert werden int i = 5; int j = 7; int& k = i; // k is initialized to reference i k = j; // the value of i is changed to 7 Damit das möglich ist, müssen wir die Speicheradresse unserer Variable kennen Pointer müssen immer einen Typ haben, obwohl sie auf eine Adresse zeigen Pointer müssen die Länge des Ziels kennen (wie viel Bytes) Daniel Bogado Duffner 18.04.2018 58

Introduction to Pointers

Pointer Program int a = 5; int* x = &a; char c = 'd'; char* z = &c; 60

Pointer Program int a = 5; int* x = &a; char c = 'd'; char* z = &c; 61

Pointer Program int a = 5; int* x = &a; char c = 'd'; char* z = &c; 62

Pointer Program int a = 5; int* x = &a; char c = 'd'; char* z = &c; x Visualization 63

Pointer Program int a = 5; int* x = &a; char c = 'd'; char* z = &c; x 64

Pointer Program int a = 5; int* x = &a; char c = 'd'; char* z = &c; x 65

Pointer Program int a = 5; int* x = &a; char c = 'd'; char* z = &c; x z 66

Shifting Pointers

Pointer Program ++x; ++z; x z 68

Pointer Program ++x; ++z; ++x z 69

Pointer Program ++x; ++z; ++x ++z 70

Pointer Program ++x; ++z; Warning: We don t know the value at ++x ++z 71

Pointer / Zeiger Wie unterscheiden zwischen Adressänderung und Wertänderung? Zwei neue Operatoren: Referenz / Adress Operator & Dereferenzierung oder Wert Operator * Daniel Bogado Duffner 18.04.2018 72

Pointer / Zeiger Referenz / Adress Operator & Dereferenzierung oder Wert Operator * int a = 5; int b = 7; int* x = 0; x = &a; std::cout << a << "\n"; // outputs 5 std::cout << *x << "\n"; // outputs 5 too // always initialize empty pointers to zero! // the address of a is written to x; x points to a std::cout << x << "\n"; // outputs 0x28fef8 (address of a) std::cout << &a << "\n"; // outputs 0x28fef8 (address of a) too x = &b; // x now points to b *x = 1; // changes value of b to 1 Daniel Bogado Duffner 18.04.2018 73

Pointer und/auf Arrays Pointer und Arrays sind sich ähnlich bzw können ineinander konvertiert werden int arr[] = {7,1,0,2,5}; int* point = arr; std::cout << *point << "\n"; // outputs 7 std::cout << *(point + 3) << "\n"; // outputs 2 // arr gets converted to the address of the // first array element a[0] (arr == &arr[0]) int arr[] = {9,2,4,5,1,2,6}; for (int i = 0; i < 7; ++i) std::cout << arr[i] << "\n"; for (int* i = arr; i < arr + 7; ++i) std::cout << *i << "\n"; Daniel Bogado Duffner 18.04.2018 74

Pointers on Arrays

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers -6 1 3-8 1 5-3 4 1 7 2 7 76

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers -6 1 0 8 7 2-1 a 4 1 7 2 7 77

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers ptr -6 1 0 8 7 2-1 a 4 1 7 2 7 78

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers ptr -6 1 0 8 7 2-1 a 4 1 7 2 7 79

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers ptr -6 1 0 8 7 2-1 a 4 1 7 8 7 my_int 80

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers ptr -6 1 0 8 7 2-1 a 4 1 7 8 7 my_int 81

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers ptr -6 1 0 8 7 18-1 a 4 1 7 8 7 my_int 82

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers ptr past -6 1 0 8 7 18-1 a 4 1 7 8 7 my_int 83

Pointer Program Output: true int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers Because ptr is "to the left" of past. ptr past -6 1 0 8 7 18-1 a 4 1 7 8 7 my_int 84

Pointer Program "To the left" means: smaller index of element Output: true int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; pointed to // array-to-pointer conv ++ptr; in array // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers Because ptr is "to the left" of past. ptr past -6 1 0 8 7 18-1 a 4 1 7 8 7 my_int 85

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; pointed to // array-to-pointer conv ++ptr; in array // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers Here: Index 3 < Index 5 "To the left" means: smaller index of element Output: true Because ptr is "to the left" of past. ptr past -6 1 0 8 7 18-1 a 4 1 7 8 7 my_int 86

Pointer Program int a[5] = {0, 8, 7, 2, -1}; int* ptr = a; // array-to-pointer conv ++ptr; // shift to the right int my_int = *ptr; // read target ptr += 2; // shift by 2 elements *ptr = 18; // overwrite target int* past = a+5; std::cout << (ptr < past) << "\n"; // compare pointers ptr past -6 1 0 8 7 18-1 a 4 1 7 8 7 my_int 87

Übung 7 - Tipps und Tricks Aufgabe 1: Ziel: Matrix und Vector Operationen (Kreuzprodukt, ) schreiben Tipp: Schreibt für das einlesen und ausgeben der Arrays eine Funktion + Schreibt für jede Operation eine Funktion + Schreibt für jeden Aufruf einer der Aufgaben eine Funktion Aufgabe 2: Ziel: Decoden eines Zeitungsartikels in Binär System Tipp: Stumpf Übersetzung von 8 bit Binärzahl zu Char, Filestream Aufgabe 3 (optional): Ziel: Eine Schöne Zeichnung aus Turtle durch Lindenmayer System Tipp: probiert ein wenig rum (Demo siehe slides), nicht zu viel Zeit darauf verwenden Daniel Bogado Duffner 18.04.2018 88

Streams and Files <fstream> Streaming data from/to files (read/write) Almost identical to console I/O Except file streams have to be instantiated and opened by the user program // Instantiate ofstream object "fout" ofstream fout; fout.open("myfile.dat"); fout << var; // writing to file cout << var; // writing to screen 89 M. Gross, ETH Zürich, 2017

Step-by-Step Streaming to Files 1. Include header #include <fstream> 2. Create fstream instance ofstream fout; 3. Open file fout.open("mydat"); 4. Transfer data from/to file fout << "Hello!\n"; 5. Close file fout.close(); 90 M. Gross, ETH Zürich, 2017

Example: Simple Write and Read #include <iostream> #include <fstream> using namespace std; int main() { ofstream fout; fout.open("mydat"); if(!fout.is_open()) { cerr << "IO error\n"; return 1; } int a = 12345; #include <iostream> #include <fstream> using namespace std; int main() { ifstream fin; fin.open("mydat"); if(!fin.is_open()) { cerr << "IO error\n"; return 1; } int a; fout << a; fout.close(); // Write to file fin >> a; fin.close(); // Read from file } return 0; } return 0; 91 M. Gross, ETH Zürich, 2017