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

Similar documents
Exercise 9 Pointers, Iterators and Recursion

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

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

Function Templates. Consider the following function:

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

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

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

Lecture on pointers, references, and arrays and vectors

CS242 COMPUTER PROGRAMMING

THE STANDARD TEMPLATE LIBRARY (STL) Week 6 BITE 1513 Computer Game Programming

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

6.096 Introduction to C++ January (IAP) 2009

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

Scientific Computing

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

Arrays. Week 4. Assylbek Jumagaliyev

CS302 - Data Structures using C++

PHY4321 Summary Notes

Informatik I (D-ITET) Übungsstunde 12,

Physics 234: Computational Physics

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Exercise 1.1 Hello world

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

CSCI-1200 Data Structures Fall 2017 Test 1 Solutions

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

Summary of basic C++-commands

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.

Functions. Functions in C++ Calling a function? What you should know? Function return types. Parameter Type-Checking. Defining a function

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

CSE030 Fall 2012 Final Exam Friday, December 14, PM

Introduction to C++ 2. A Simple C++ Program. A C++ program consists of: a set of data & function definitions, and the main function (or driver)

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

FORM 1 (Please put your name and section number (001/10am or 002/2pm) on the scantron!!!!) CS 161 Exam II: True (A)/False(B) (2 pts each):

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Introduction to C ++

Lab # 02. Basic Elements of C++ _ Part1

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Outline. Introduction. Arrays declarations and initialization. Const variables. Character arrays. Static arrays. Examples.

Cours de C++ Introduction

Building on the foundation. Now that we know a little about cout cin math operators boolean operators making decisions using if statements

Type Aliases. Examples: using newtype = existingtype; // C++11 typedef existingtype newtype; // equivalent, still works

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Working with Batches of Data

Increment and the While. Class 15

What will happen if we try to compile, link and run this program? Do you have any comments to the code?

Introduction to Programming

Exercise 7 References, Arrays, Vectors

Elements of C in C++ data types if else statement for loops. random numbers rolling a die

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2013 CISC2200 Yanjun Li 1. Fall 2013 CISC2200 Yanjun Li 2

GCC : From 2.95 to 3.2

(6) The specification of a name with its type in a program. (7) Some memory that holds a value of a given type.

DELHI PUBLIC SCHOOL TAPI

Creating a C++ Program

Review. Outline. Array Pointer Object-Oriented Programming. Fall 2017 CISC2200 Yanjun Li 1. Fall 2017 CISC2200 Yanjun Li 2

Chapter 2: Introduction to C++

A First Program - Greeting.cpp

Programación de Computadores. Cesar Julio Bustacara M. Departamento de Ingeniería de Sistemas Facultad de Ingeniería Pontificia Universidad Javeriana

COMP322 - Introduction to C++ Lecture 01 - Introduction

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

14. Pointers, Algorithms, Iterators and Containers II

CS 7B - Spring Final Exam

Chapter 2. C++ Syntax and Semantics, and the Program Development Process. Dale/Weems 1

Multiple Choice (Questions 1 14) 28 Points Select all correct answers (multiple correct answers are possible)

Computer Programming : C++

15. Pointers, Algorithms, Iterators and Containers II

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering

int n = 10; int sum = 10; while (n > 1) { sum = sum + n; n--; } cout << "The sum of the integers 1 to 10 is " << sum << endl;

Chapter 2: Overview of C++

LECTURE 02 INTRODUCTION TO C++

Computational Physics

Introduction to Programming using C++

Input And Output of C++

Chapter 2 C++ Fundamentals

W3101: Programming Languages C++ Ramana Isukapalli

Introduction to Programming

CHAPTER 3 BASIC INSTRUCTION OF C++

CMPS 221 Sample Final

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

Fast Introduction to Object Oriented Programming and C++

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

Outline. Introduction. Pointer variables. Pointer operators. Calling functions by reference. Using const with pointers. Examples.

Exceptions, Case Study-Exception handling in C++.

Professor Terje Haukaas University of British Columbia, Vancouver C++ Programming

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 15 - C++ As A "Better C"

Chapter 2: Basic Elements of C++

a data type is Types

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Final Exam Solutions PIC 10B, Spring 2016

typedef Labeling<unsigned char,short> LabelingBS; typedef Labeling<unsigned char,short>::regioninfo RegionInfoBS;

Chapter 10 - Notes Applications of Arrays

True or False (15 Points)

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

1 Unit 8 'for' Loops

CMSC 341 Lecture 6 Templates, Stacks & Queues. Based on slides by Shawn Lupoli & Katherine Gibson at UMBC

Introduction to C++ Professor Hugh C. Lauer CS-2303, System Programming Concepts

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

1 #include <iostream > 3 using std::cout; 4 using std::cin; 5 using std::endl; 7 int main(){ 8 int x=21; 9 int y=22; 10 int z=5; 12 cout << (x/y%z+4);

Transcription:

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

Self-Assessment III 1) Characters: FILO - 2P for the completely correct answer - 0P otherwise 2) Pointers

Self-Assessment III 3) Referenzen

Self-Assessment III 3) Iteration

Self-Assessment III 5) Felder und Arrays I

Self-Assessment III 5) Felder und Arrays I

Self-Assessment III 6) Felder und Zeiger II

Self-Assessment II

Problem 8.1. Vector and matrix multiplication int main() { char ltype; std::cout << "enter left operand type:\n ; std::cin >> ltype; char rtype; std::cout << "enter right operand type:\n ; std::cin >> rtype; if (ltype == v && rtype == v ) { vector_vector(); else if (ltype == m && rtype == v ) { matrix_vector(); else if (ltype == m && rtype == m ) { matrix_matrix(); else { return 0; std::cout << "unsupported operation\n";

Problem 8.1. Vector and matrix multiplication Einlesen void matrix_vector() { int matrix[3][3]; int vector[3]; int res[3]; input_matrix(matrix); input_vector(vector); matrix_vector_product(res, matrix, vector); output_vector(res);

Problem 8.1. Vector and matrix multiplication Einlesen // POST: matrix filled with values from std input void input_matrix(int (&matrix)[3][3]) { for (int row = 0; row!= 3; ++row) { for (int col = 0; col!= 3; ++col) std::cin >> matrix[row][col];

Problem 8.1. Vector and matrix multiplication // POST: result contains the cross product of left and right void cross_product(int (&res)[3], const int (&left)[3], const int (&right)[3]) { res[0] = left[1] * right[2] - left[2] * right[1]; res[1] = left[2] * right[0] - left[0] * right[2]; res[2] = left[0] * right[1] - left[1] * right[0]; // POST: result contains the matrix-vector product of left and right void matrix_vector_product(int (&res)[3], const int (&left)[3][3], const int (&right)[3]) { // row of left array for (int row = 0; row!= 3; ++row) { int sum = 0; // column of right array for (int col = 0; col!= 3; ++col) { // column of left array = row of right array sum += left[row][col] * right[col]; res[row] = sum;

Problem 8.1. Vector and matrix multiplication // POST: result contains the matrix product of left and right void matrix_product(int (&res)[3][3], const int (&left)[3][3], const int (&right)[3][3]) { // row of left array for (int row = 0; row!= 3; ++row) { // column of right array for (int col = 0; col!= 3; ++col) { // column of left array = row of right array int sum = 0; for (int i = 0; i!= 3; ++i){ sum += left[row][i] * right[i][col]; res[row][col] = sum;

Problem 8.2. Decode binary NZZ front page #include <iostream> #include <fstream> int main() { std::string filename; std::cin >> filename; std::ifstream in(filename.c_str()); std::string value; while(in >> value) { unsigned char c; bool success = decode_to_char(c, value); if (!success) { std::cout << "input file has incorrect format\n"; break; return 0; std::cout << c;

Problem 8.2. Decode binary NZZ front page // PRE: Input string value containing exactly 8 characters. Allowed characters are 0 and 1. // POST: Returns true if the string value could be converted to a character, otherwise false. // If return value is true then res contains the character corresponding to the input string // value, otherwise res is undefined. bool decode_to_char(unsigned char& res, const std::string& value) { if (value.length()!= 8) { return false; res = 0; for(int i = 0; i < 8; ++i) { res *= 2; if (value[i] == 1 ) { res += 1; else if (value[i]!= 0 ) { return false; return true;

Problem 8.3 Lindenmayer systems

1 Pointers #include <iostream> using namespace std; int main () { int var1; char var2[10]; cout << "Address of var1 variable: "; cout << &var1 << endl; // Address of var1 variable: 0xbfebd5c0 cout << "Address of var2 variable: "; cout << &var2 << endl; // Address of var2 variable: 0xbfebd5b6 return 0;

1 Pointers int *ip; // pointer to an integer double *dp; // pointer to a double float *fp; // pointer to a float char *ch // pointer to character

1 Pointers int main () { int var = 20; // actual variable declaration. int *ip; // pointer variable ip = &var; // store address of var in pointer variable cout << "Value of var variable: "; cout << var << endl; // Value of var variable: 20 // print the address stored in ip pointer variable cout << "Address stored in ip variable: "; cout << ip << endl; // Address stored in ip variable: 0xbfc601ac // access the value at the address available in pointer cout << "Value of *ip variable: "; cout << *ip << endl; // Value of *ip variable: 20

1 Pointers

1 Pointers Common mistakes when working with pointers int c, *pc; pc=c; // Wrong! pc is address whereas, c is not an address. *pc=&c; // Wrong! *pc is the value pointed by address whereas, c is an address. pc=&c; // Correct! pc is an address and, pc is also an address. */ *pc=c; // Correct! *pc is the value pointed by address and, c is also a value. */

1 Pointers Pointer Program

2 Iterators for Vectors Vectors have a similar concept as pointers, called iterators. Similar to array and pointers.begin() and.end() on a vector to get the iterators the & operator unnecessary for vectors #include <vector> // (for reference: the corresponding pointer example) // int array[] = {8,3,1,4,6,9; // for (int* p = array; p!= array + 6; ++p) // std::cout << *p; std::vector<int> vec = {8,3,1,4,6,9; // C++11 syntax! for (std::vector<int>::iterator it = vec.begin(); it < vec.end(); ++it) std::cout << *it;

3 Typedef gives a new name to an already existing type More readable code! Easier to maintain! std::vector<int> vec = {8,3,1,4,6,9; for (std::vector<int>::iterator it = vec.begin(); it < vec.end(); ++it) std::cout << *it; typedef std::vector<int> intvec; typedef std::vector<int>::iterator intvecit; intvec vec = {7,3,5,2,7,9; for (intvecit it = vec.begin(); it < vec.end(); ++it) std::cout << *it;

4 const Pointers int x = 7; int y = 5; const int* i = &x; ++(*i); // not compiling, value stored in x cannot be changed via pointer access i = &y; // works int* const j = &x; ++(*j); // works j = &y; // does not compile, address stored in j is const const int* const k = &x; // one can also do both ++(*k); // not compiling, value stored in x cannot be changed via pointer access k = &y; // does not compile, address stored in k is const

4 const Pointers It is not possible to have a non-const-pointer pointing to a const-target: const int a = 5; int* a_ptr = &a; // Error: must be const int* a_ptr = a;

4 const Pointers

Übungsblatt 9 Problem 9.1. Understanding Pointers Problem 9.2. Sorting integer array pointers: void input(int* const begin, int* const end) several functions: input, output, swap, min, max, sort Problem 9.3. Lexicographic comparison e.g., a = bicycle, b = bike: a < b, because a[2] < b[2] e.g., web < website Iterators lexicographic_compare largeststring (std::vector<std::string> strings(n);)