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

Similar documents
THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program

Getting started with C++ (Part 2)

BITG 1233: Introduction to C++

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

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

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

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

2 nd Week Lecture Notes

LECTURE 02 INTRODUCTION TO C++

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.

Engineering Problem Solving with C++, 3e Chapter 2 Test Bank

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

5.1. Chapter 5: The Increment and Decrement Operators. The Increment and Decrement Operators. The Increment and Decrement Operators

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

C++ Programming: From Problem Analysis to Program Design, Third Edition

The cin Object. cout << "Enter the length and the width of the rectangle? "; cin >> length >> width;

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

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

1. Match each of the following data types with literal constants of that data type. A data type can be used more than once. A.

Introduction to Programming EC-105. Lecture 2

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

Fundamentals of Programming CS-110. Lecture 2

Objects and streams and files CS427: Elements of Software Engineering

Introduction to C++ Lecture Set 2. Introduction to C++ Week 2 Dr Alex Martin 2013 Slide 1

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

Chapter 2: Introduction to C++

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program?

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

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

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

C++ basics Getting started with, and Data Types.

Name SECTION: 12:45 2:20. True or False (12 Points)

WARM UP LESSONS BARE BASICS

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

Computer Programming : C++

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

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

Chapter 2: Basic Elements of C++

Objectives. In this chapter, you will:

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

Introduction to C++ Dr M.S. Colclough, research fellows, pgtas

1 Unit 8 'for' Loops

Your First C++ Program. September 1, 2010

CPE 112 Spring 2015 Exam III (100 pts) April 8, True or False (12 Points)

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

Introduction to C++ (Extensions to C)

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Increment and the While. Class 15

Chapter 2: Overview of C++

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

CSI33 Data Structures

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

Looping. Arizona State University 1

Sample Code: OUTPUT Daily Highs & Lows

CHAPTER 3 BASIC INSTRUCTION OF C++

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

CS2141 Software Development using C/C++ C++ Basics

Why Is Repetition Needed?

Unit 7. 'while' Loops

C++ Support Classes (Data and Variables)

Engineering Problem Solving with C++, Etter/Ingber

CS302 - Data Structures using C++

PIC 10A. Review for Midterm I

causing a set of statements (the body) to be executed repeatedly. C++ provides three control structures to support iteration (or looping).

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

J. P. Cohoon and J. W. Davidson 1999 McGraw-Hill, Inc. Control Constructs. Mechanisms for deciding when and how often an action should be taken

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

Scientific Computing

Chapter 2 Basic Elements of C++

READ THIS NOW! Do not start the test until instructed to do so!

CSCI 123 Introduction to Programming Concepts in C++

do { statements } while (condition);

Input And Output of C++

CSC Web Programming. Introduction to JavaScript

EP578 Computing for Physicists

CMPS 221 Sample Final

CS242 COMPUTER PROGRAMMING

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

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

REPETITION CONTROL STRUCTURE LOGO

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

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

Chapter 2 C++ Fundamentals

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Introduction to Programming

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

CS31 Discussion 1E. Jie(Jay) Wang Week3 Oct.12

READ THIS NOW! Failure to read and follow the instructions below may result in severe penalties. Do not start the test until instructed to do so!

ME240 Computation for Mechanical Engineering. Lecture 4. C++ Data Types

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Control Structures of C++ Programming (2)

Transcription:

Object-oriented Programming for Automation & Robotics Carsten Gutwenger LS 11 Algorithm Engineering Lecture 3 Winter 2011/12 Oct 25

Visual C++: Problems and Solutions New section on web page (scroll down) Some typical problems we experienced with VC++ and solutions to fix them Will be extended if necessary Carsten Gutwenger: Object-oriented Programming 2

Loops Continued do-while-loops: Similar as while-loops, but the condition is evaluated after each iteration. The general form is: do statement; while ( condition ); statement is executed before condition is evaluated. If condition evaluates to false, the loop is terminated. statement is executed at least once! Carsten Gutwenger: Object-oriented Programming 3

Example: Sum up numbers until 0 is entered #include <iostream> using namespace std; int main() { int number, sum = 0; do { cin >> number; sum = sum + number; } while (number!= 0); cout << "sum = " << sum << endl; } return 0; Carsten Gutwenger: Object-oriented Programming 4

A typical pattern for while-loops We often use while-loops of the following form: int counter = 0; while (counter < n) { // // do something // counter = counter + 1; } Initialization Loop-condition Iteration There is a special syntax for writing such kinds of while-loops! Carsten Gutwenger: Object-oriented Programming 5

for-loops The general form is: Semantics: for ( init-statement; condition; iter-statement ) body-statement; 1.First init-statement is executed (only once!) 2.Then condition is evaluated: false terminate loop 3.Then body-statement is executed. 4.Then iter-statement is executed. 5.Go to step 2. Carsten Gutwenger: Object-oriented Programming 6

Example: Print first n square numbers int n = 10; /* for example */ for (int i = 1; i <= n; i = i + 1) cout << i * i << endl; This is equivalent to: int n = 10; /* for example */ int i = 1; while (i <= n) { cout << i * i << endl; i = i + 1; } Carsten Gutwenger: Object-oriented Programming 7

Transforming for-loops into while-loops for(init-stat; cond; iter-stat) { body-statement-1; body-statement-n; } { } init-stat; while( cond ) { body-statement-1; body-statement-n; iter-stat; } We will understand the block around the while-statement next time when discussing scope and lifetime of variables! Carsten Gutwenger: Object-oriented Programming 8

The break-statement Used to terminate a loop at some place in the body of the loop. Syntax: break; Semantics: Terminates the nearest enclosing while-, do-while-, or for-loop statement. Example: for (int i = 1; i <= 100; i = i + 1) { } int x; cin >> x; if (x == 0) break; cout << 100 / x << endl; Carsten Gutwenger: Object-oriented Programming 9

The continue-statement Used to terminate an iteration of a loop. Syntax: Semantics: Terminates the current iteration of the nearest enclosing while-, do-while-, or for-loop statement. Example: continue; for (int i = 1; i <= 100; i = i + 1) { } int x; cin >> x; if (x == 0) continue; cout << 100 / x << endl; Carsten Gutwenger: Object-oriented Programming 10

Strings Strings (texts) can be stored in variables of type std::string. std::string is part of the C++ standard library, not the C++ language itself. We have to include this additional functionality first: #include <string> String literals have to be enclosed in quotation marks " " std::string name; name = "Carsten"; Carsten Gutwenger: Object-oriented Programming 11

Using Strings We can use std::string almost like a built-in type: Initialization: std::string name = "Carsten" Comparing for equality with == Assignment using = Concatenation using + Example: string name = "Gutwenger"; string firstname = "Carsten"; name = firstname + " " + name; cout << "The name is " << name << endl; if(name == "Carsten Gutwenger") cout << "That's me!" << endl; Carsten Gutwenger: Object-oriented Programming 12

Warning Don t compare string literals! if ("abc" == "abc") cout << "Undefined if we get here!" << endl; Carsten Gutwenger: Object-oriented Programming 13

Special Characters Within string literals, you can use the following special characters: \n newline character \t tab stop character \" quotation mark \\ backslash character Example: "This \"string\"\ngoes over two lines" Carsten Gutwenger: Object-oriented Programming 14

Output formatting C++ supports various manipulators for nicely formatting output. You need to include this special functionality: #include <iomanip> Manipulators are inserted into the output stream (with the output operator <<) just as you print data. The following manipulators are useful for integers: setw(n): sets the number n of characters to be used as field width for the next insert operation. left: output is left-aligned in the output field. right: output is right-aligned in the output field. Carsten Gutwenger: Object-oriented Programming 15

Example: Formatting Output #include <iostream> #include <iomanip> using namespace std; int main() { int a = 7, b = 12345; cout << left; cout << setw(10) << a << "***" << endl; cout << setw(10) << b << "***" << endl; cout << right; cout << setw(10) << a << "***" << endl; cout << setw(10) << b << "***" << endl; Output: 7 *** 12345 *** 7*** 12345*** 10 characters } return 0; Carsten Gutwenger: Object-oriented Programming 16

The bool Data Type Variables of type bool store one of two possible values: true and false Example: bool found = true; bool isgreater = ( a > b ); // a and b are variables Operators for Boolean expressions: logical AND: && logical OR: logical NOT:! Example:!found ( a > b && a < 2*b ) Carsten Gutwenger: Object-oriented Programming 17

Printing bool values By default, bool values are printed as 0 or 1 (corresponding to false or true). You can change this behavior with manipulators! boolalpha: print false or true noboolalpha: print 0 or 1 Example: bool b = true; cout << b << endl; cout << boolalpha << b << endl; cout << noboolalpha << b << endl; Output: 1 true 1 Carsten Gutwenger: Object-oriented Programming 18

Preparations for next week Floating-point numbers (float, double) Increment and Decrement (e.g. ++, += operators) Scope and lifetime of variables C++-Vectors (std::vector) Carsten Gutwenger: Object-oriented Programming 19