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

Similar documents
Informatik I (D-ITET) Übungsstunde 5, Hossein Shafagh

Informatik I (D-ITET) Übungsstunde 12,

16. Structs and Classes I

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

17. Classes. Overloading Functions. Operator Overloading. Function Overloading. operatorop

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

15. Recursion 2. Motivation: Calculator. Naive Attempt (without Parentheses) Analyzing the Problem (15 7 3) = Input * 3 = Result 15

& '() *+, ## & % -. & - -#%'# -%' / 1 ' / 2 '## & / % / 3 / 2 5-0

Jan 27, C++ STL Streams. Daniel Maleike

Streams. Ali Malik

17. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

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

19. Classes. Encapsulation: public / private. Member Functions: Declaration. Member Functions: Call. class rational { int n; int d; // INV: d!

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

17. Classes. Encapsulation, Classes, Member Functions, Constructors

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

CS242 COMPUTER PROGRAMMING

PIC10B/1 Winter 2014 Exam I Study Guide

Overview of Lecture. 1 Overloading I/O Operators. 2 Overloading << and >> for Fractions. 3 Formatted vs Unformatted Input

INFORMATIK 1 am D-ITET

Operator Overloading in C++ Systems Programming

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 2. Yusuf Pisan

Arizona s First University. ECE 373. Operation Overloading. The Revolution Operation Will Be Televised (and, these slides will be online later today)

CSC 330 Object Oriented Programming. Operator Overloading Friend Functions & Forms

Jim Lambers ENERGY 211 / CME 211 Autumn Quarter Programming Project 4

Introduction to Programming

CS 215 Fundamentals of Programming II Spring 2011 Project 2

Ch. 12: Operator Overloading

C++ Basics. Data Processing Course, I. Hrivnacova, IPN Orsay

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

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

G52CPP C++ Programming Lecture 18

Chapter 18 - C++ Operator Overloading

Exercise 7 References, Arrays, Vectors

Abstract Data Types (ADTs) 1. Legal Values. Client Code for Rational ADT. ADT Design. CS 247: Software Engineering Principles

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Abstraction and Encapsulation. Benefits of Abstraction & Encapsulation. Concrete Types. Using Typedefs to streamline classes.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

CS 247: Software Engineering Principles. ADT Design

Note 11/13/2014. They are like those i s, j s, and temp s that appear and disappear when the function starts and finishes...

UEE1303(1070) S12: Object-Oriented Programming Operator Overloading and Function Overloading

CSI33 Data Structures

CHAPTER 1.2 INTRODUCTION TO C++ PROGRAMMING. Dr. Shady Yehia Elmashad

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

Final Exam Solutions PIC 10B, Spring 2016

Final Exam. Name: Student ID: Section: Signature:

Cours de C++ Introduction

Topics. bool and string types input/output library functions comments memory allocation templates classes

8. Floating-point Numbers II

Programming. C++ Basics

Revising the wording of stream input operations

Types, Values, Variables & Assignment. EECS 211 Winter 2018

Programmazione. Prof. Marco Bertini

Basics of C++ // my first program in C++ Hello World! #include <iostream> using namespace std; int main () { cout << "Hello World!

Overloaded Operators, Functions, and Students

4 Strings and Streams. Testing.

DEPARTMENT OF MATHS, MJ COLLEGE

6. Control Statements II

C++ Modern and Lucid C++ for Professional Programmers

COP Programming Assignment #7

CS302 - Data Structures using C++

Computational Physics Operating systems

C++ Tutorial AM 225. Dan Fortunato

COMP6771 Advanced C++ Programming

Assignment #1 Advanced Programming in C /2018. NPRG051 Advanced programming in C++ - Assignment #1

Computer Science II Lecture 2 Strings, Vectors and Recursion

CEC Intermediate Representation

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

3. Logical Values. Our Goal. Boolean Values in Mathematics. The Type bool in C++

C++ Basics. Brian A. Malloy. References Data Expressions Control Structures Functions. Slide 1 of 24. Go Back. Full Screen. Quit.

Objectives. In this chapter, you will:

2.1. Unit 2. Integer Operations (Arithmetic, Overflow, Bitwise Logic, Shifting)

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

Name Section: M/W or T/TH. True or False (14 Points)

10/23/02 21:20:33 IO_Examples

Copying Data. Contents. Steven J. Zeil. November 13, Destructors 2

18. Dynamic Data Structures I. Dynamic Memory, Addresses and Pointers, Const-Pointer Arrays, Array-based Vectors

True or False (12 Points)

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

UEE1302 (1102) F10: Introduction to Computers and Programming

CHAPTER 4 FUNCTIONS. Dr. Shady Yehia Elmashad

QUIZ. 1. Explain the meaning of the angle brackets in the declaration of v below:

Summary of basic C++-commands

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9

Streams. Rupesh Nasre.

Operator Overloading

CSS 342 Data Structures, Algorithms, and Discrete Mathematics I. Lecture 1. Yusuf Pisan

Lecture 5 Files and Streams

3 Assignment 1 [Assignment ID:cpp_basics]

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

21. Dynamic Datatypes and Memory Management

G52CPP C++ Programming Lecture 18. Dr Jason Atkin

LECTURE 02 INTRODUCTION TO C++

Inductive Data Types

C++ Modern and Lucid C++ for Professional Programmers

Class string and String Stream Processing Pearson Education, Inc. All rights reserved.

Fast Introduction to Object Oriented Programming and C++

ESC101N: Fundamentals of Computing End-sem st semester

Introduction to C++ Systems Programming

Console input 26/09/2018. Background. Background

Transcription:

Informatik I (D-ITET) Übungsstunde 11, 4.12.2017 Hossein Shafagh shafagh@inf.ethz.ch

Problem 10.1. Recursive Function Analysis Problem 10.1. Recursive Function Analysis (a) bool f (const int n) { if (n == 0) return false; return!f(n-1); (b) void g (const int n) { if (n == 0) { std::cout << "* ; return; g(n-1); g(n-1);

Problem 10.1. Recursive Function Analysis Problem 10.1. Recursive Function Analysis // PRE: n>=0 // POST: return value is false if n is even and true if n is odd bool f (const int n) { if (n == 0) return false; return!f(n-1); The function f immediately terminates for n == 0. With each recursive call n is decremented, i.e., f is called with parameter < n, eventually reaching n = 0. Calls f (n) = n+1

Problem 10.1. Recursive Function Analysis Problem 10.1. Recursive Function Analysis // PRE: n >= 0 // POST: 2ˆn stars have been written to standard output void g (const int n) { if (n == 0) { std::cout << "* ; return; g(n-1); g(n-1); The function g immediately terminates for n == 0. With each recursive call n is decremented, i.e., g is called with parameter < n, eventually reaching n = 0. This is true for both recursive calls of g. Calls g (n) = 2! = 2 n+1 1!!!!

Problem 10.2. Trains i. <*-*><**> à invalid ii. <*--------------------------*> à valid iii. ****- à valid iv. <*-------*>*---- à invalid // train formations BNF: // train = open compositions. // open = loco cars. // loco ="* "* loco. // cars ="- "-"cars. // compositions = composition { composition. // composition = "<" open loco ">". à *-

Problem 10.2. Trains // trains.cpp // parse and validate train formations #include <iostream> #include <istream> #include <sstream> #include <string> int main() { std::cout << "please enter a train formation:\n"; // reading a string and wrapping it into stringstream prevents whitespace from occuring in a formation, // as can happen if reading directly from std::cin. std::string train_formation; std::cin >> train_formation; std::stringstream train_in(train_formation); if (train(train_in)) else return 0; std::cout << "valid\n"; std::cout << "invalid\n ;

Problem 10.2. Trains // PRE: Valid stream is. // POST: Leading whitespace characters are extracted from is, and the // first non-whitespace character is returned (0 if there is none). char lookahead (std::istream& is) { is >> std::ws; if (is.eof()) else return 0; // skip whitespaces // end of stream return is.peek(); // next character in is // PRE: Valid stream is. // POST: Reads char from stream and returns true if that char matches // the expected char, otherwise false is returned. bool readchar(std::istream& is, char expected) { char actual; is >> actual; return actual == expected;

Problem 10.2. Trains // train formations BNF: // train = open compositions. // open = loco cars. // loco ="* "* loco. // cars ="- "-"cars. // compositions = composition { composition. // composition = "<" open loco ">". bool train(std::istream& is) { // train = open compositions. bool valid; if (lookahead(is) == < ) valid = compositions(is); else valid = open(is); // no more characters in string valid = valid && lookahead(is) == 0; return valid;

Problem 10.2. Trains bool open(std::istream& is) { // open = loco cars. return loco(is) && cars(is); bool loco(std::istream& is) { // loco = "*" "*" loco bool valid = readchar(is, * ); if (lookahead(is) == * ) return valid; valid = valid && loco(is); bool cars(std::istream& is) { // cars = "-" "-" cars bool valid = readchar(is, - ); if (lookahead(is) == - ) valid = valid && cars(is); return valid;

Problem 10.2. Trains bool compositions(std::istream& is) { // compositions = composition { composition. bool valid = composition(is); while(valid && lookahead(is) == < ) { valid = valid && composition(is); return valid; bool composition(std::istream& is) { // composition = "<" open loco ">". return readchar(is, < ) && open(is) && loco(is) && readchar(is, > );

Problem 10.3. Money int main() { // the 13 denominations of CHF unsigned int chf[] = {100000, 20000, 10000, 5000, 2000, 1000, 500, 200, 100, 50, 20, 10, 5; // input std::cout << "in how many ways can I own x CHF-centimes for x =?\n"; unsigned int x; std::cin >> x; // computation and output std::cout << partitions (x, chf, chf+13) << "\n"; return 0;

Problem 10.3. Money // PRE: [begin, end) is a valid nonempty range that describes // a sequence of denominations d_1 > d_2 >... > d_n > 0 // POST: return value is the number of ways to partition amount sing denominations from d_1,..., d_n unsigned int partitions (const unsigned int amount, const unsigned int* begin, const unsigned int* end) { if (amount == 0) return 1; unsigned int ways = 0; // ways = ways_1 +... + ways_n, where ways_i is the number // of ways to partition amount using d_i as the largest denomination for (const unsigned int* d = begin; d!= end; ++d) // ways_i = number of partitions of the form (d_i, X), with // (X) being a partition of amount-d_i using d_i,...,d_n if (amount >= *d) ways += partitions (amount-*d, d, end); return ways;

1 Structs 2.1 Data members struct rational { int n; int d; // INV: d!= 0 ; int main () { rational r; // With C++11 initializer list: r = {1, 2 r.n = 1; r.d = 2; return 0;

1 Structs 2.1 Data members struct strange { ; int main () { int n; bool b; int a[3]; strange x; // With C++11 initializer list: x = {1, true, {1,2,3; x.n = 1; x.b = true; x.a[0] = 1; x.a[1] = 2; x.a[2] = 3; strange y = x; return 0;

1 Structs Exercise 2) Struct for Point and Lines

2 Function Overloading int f (int a) {... int f (int a, int b) {... int f (float a) {... int f (int b) { // error double f (int a) { // error

2 Function Overloading Impact of Overloading double calculation (double value) { return value / 2.0; int calculation (int value) { return value / 2; int main () { int number; std::cin >> number; double result = calculation(number); std::cout << result; return 0;

3 Operator Overloading operators +=, -=, *=, /= struct rational { int n; int d; // INV: d!= 0 ; rational& operator+= (rational& a, const rational b) { a.n = a.n * b.d + a.d * b.n; a.d *= b.d; return a; rational operator+ (rational a, const rational b) { return a += b;

3 Operator Overloading ++r and r++ // pre-comrement rational& operator++ (rational& r) { rational s = {1,1; return r += s; // post-comrement rational operator++ (rational& r, int i) { rational s = {1,1; rational r_0 = r; r += s; return r_0;

3 Operator Overloading << std::ostream& operator<< (std::ostream& o, rational r) { o << r.n << "/" << r.d; return o;

4 Tribool

5 Const references int a = 5; int& b = a; const int& c = a; c++; // runtime error, a cannot be changed through const reference c b++; // a is now 6, a can still be changed through other non-const references a++; // a is now 7, a can still change by itself Guarantee to not touch the variable void print_result (const double& result) { Initialize with r-value const int& a = 5; // compiler creates an int variable, puts 5 into it, creates a const reference pointing to it A const reference as an argument enables a function to do one of the two: const call by reference and call by value.

Übungsblatt 11 Structs & Operators Problem 11.1 Finite Rings Computations with modulo 7 Addition/Subtraction Problem 11.2a Complex numbers Data type for complex numbers Arithmetic operations Problem 11.2b Calculator for complex numbers your own data type Complex as a drop-in replacement for double.