2. Variables, Identifiers, and Expressions. March 8, 2011

Size: px
Start display at page:

Download "2. Variables, Identifiers, and Expressions. March 8, 2011"

Transcription

1 March 8, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 34

2 Outline A Movie Structure of Simple C Codes Comments & Documentation Variables & Identifiers Built-in Datatypes Excursus: Floating Point Precision Simple Expressions: Assignments and Basic Arithmetics Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 2 of 34

3 The Golden Age of (Super)Computing Chris Johnson, head of the Scientific Computing & Imaging Institute in Salt Lake City, and member of the PITAC committee. At TUM, there s similar institutes (organised as consortia) such as the Munich Centre of Advanced Computing. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 3 of 34

4 2.1. Structure # i n c l u d e <iostream> i n t main ( ) { std : : cout << Hello yourname ; r e t u r n 0; } The computer runs through the code step-by-step (left to right, top-down) In each step, it executes the commands (a code is a cooking instruction) A step is terminated by a semicolon main identifies the jump-in point std::cout plots something on the console return 0 makes the program terminate (return to the command line prompt) Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 4 of 34

5 An Addendum: C vs. C++ C is (basically) subset of C++ Some features were deprecated or augmented (see comments) Some functions were replaced, in particular the output functions i n t a=10; double b=20; std : : cout << a i s << a << and b i s << b ; / / C++ s t y l e p r i n t f ( a i s %i and b i s %f, a, b ) ; / / C s t y l e Other functions: malloc (became new), free (became delete), and structs (need a name before the bracket opens). Furthermore, some shortcuts were removed (main always needs a return type). The C++ typically are safer (type checks) and more powerful. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 5 of 34

6 2.2. Variables Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 6 of 34

7 Declaring a Variable Memory int x; x = 10; 21: 22; 23; 24; 10 x is alias 25; 26; 27; 28: Computer runs through code step-by-step Declaration = define a name/alias for a memory location Definition = find a well-suited (free) place in memory Variable then is alias for this storage point we work with names instead of addresses Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 7 of 34

8 Identifiers W. Savitch: A C++ identifier must start with either a letter or the underscore symbol, and the remaining characters must all be letters, digits, or underscore symbols. C++ identifiers are case sensitive and have no limit to their length. (p. 7) An identifier is (unique) name of a variable Must not equal a keyword Should have a name with a precise meaning (UNIX-style and Hungarian notation today are considered to be a bad smell) Examples: usernumber, UserNumber, usernumber, usernumber, user number usno, usrn, usrn, nusr, usr1, usr1no iusernumber, intusernumber Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 8 of 34

9 Built-in Datatypes Memory int x; char y; float z; 21: y // one byte 22; z // four bytes 23; 24; 25; 26; x // two bytes? 27; 28: A variable corresponds to memory location and holds a value. What type of value? char, int, float, and double, and so forth. Compiler cares for memory layout. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 9 of 34

10 What Value Does a Variable Have By Default? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 10 of 34

11 Scope of Built-in Datatypes E. W. Dijkstra: Once a person has understood the way variables are used in programming, he has understood the quintessence of programming. (Notes on Structured Programming) name size range memory footprint bool {, } 1 Byte (?) char Byte short int 32, , Bytes int 2, 147, 483, , 147, 483, Bytes (?) long int 2, 147, 483, , 147, 483, Bytes (?) float ± ± Bytes double ± ± Bytes Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 11 of 34

12 Long and Unsigned Modifier Long Integers long i n t a ; / / size? i n t b ; / / size? C/C++ standard defines at least n bits. On 64 bit architectures, int and long int typically are the same. Some 32 bit architectures support 64 bit integers due to a tailored compiler. Others don t (such as the RZG s BlueGene/P system with the IBM compiler). Recommendation: Avoid modifiers such as long and short. Unsigned Integers unsigned i n t a ; / / size? i n t b ; / / size? That sign consumes one bit of the representation. If you are sure you don t need the sign, you can squeeze out an additional bit, however, be careful with this! Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 12 of 34

13 What is an Overflow? An excursus into binary number systems: b = b = b = b = 3 10 ALU internal (for an inc): b b b b b b Your turn: Increment b (it is an unsigned integer)! Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 13 of 34

14 What is an Overflow? C++ does not check for overflows (unlike all the interpreted languages such as Java and C# that are slower in turn). C++ does not check for underflows (unlike all the interpreted languages such as Java and C# that are slower in turn). Now, how would you define/encode your signed integer in terms of bits and what do the operations look like? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 14 of 34

15 Codes for Signed Integers 1-2: b b b b b b b b -1+1: b b b b b b b Still, the first bit is the sign, but an overflow is just running through zero. There s more negative values than positive ones. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 15 of 34

16 Syntax vs. Semantics Syntax = rules for the constructing of sentences in the languages Semantics = meaning of sentences, words, or fragments. The syntax is (more or less) prescribed by C, but the semantics is what you had in mind, i.e. the reader has to reconstruct it from your program. So, use meaningful names, and, if that is not sufficient, add documentation, documentation, and documentation. If code is not documented, it does not exist! Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 16 of 34

17 2.3. Comments # i n c l u d e <iostream> / This i s the famous main operation. Here the a p p l i c a t i o n s t a r t s. / i n t main ( ) { / / hey dude, here we go / w r i t e yourname to the console / std : : cout << Hello yourname ; / / Return to the console r e t u r n 0; / The comments are not exectuded by the machine / } C/C++ is an instruction for a computer not for a human Often, we (or others) do not understand from a code what is happening Insert comments which annotate the code They are thrown away by the compiler Syntax alternatives: C/C++/C++ (JavaDoc) Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 17 of 34

18 Facts About Comments Sad But True C code is difficult to understand it ain t literate programming (Knuth s T E X experience) Documentation (design drafts, Ph.D. theses, papers, webpages) never is up-to-date (quick-fix disease) After a few days, the author is not familiar with the code anymore Every developer has a different style of writing and a different style of thinking I ll comment when it is running that is a lie! document everything in the code, otherwise its lost or won t be read! Tools Best Practices Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 18 of 34

19 Documentation Tools Screenshot from Doxygen Sad But True document everything in the code, otherwise its lost or won t be read! Tools Idea: Extract documentation from source code Format: Webpages, PDF, T E X Content: Diagrams, documentation text, mathematical formulas, images Tools: JavaDoc, Doxygen (both for C/C++) Best Practices Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 19 of 34

20 Documentation Best Practices Sad But True document everything in the code, otherwise its lost or won t be read! Tools Best Practices Document everything in the code (first place) before you write or alter the code. If you have to work with a third-party code, document it in the first place and send it back to the authors. They ll love you! Add a description before each complicated part (operations, methods,... ). Make yourself familiar with the tools and add formulas and images to your documentation. Document what, why, rationale, alternatives, and bugs fixed. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 20 of 34

21 Screenshot Peano-Sources - ispassive ispassive Peano - Source Directory and Architecture grid AbstractVertex<> ispassive Search Help grid Directories adapter checkpoint configuration integration-tests multicore records runners statistics tests Classes AbstractCell<> AbstractLeafEvent<> AbstractNewEvent<> AbstractPersistentRefined. AbstractRefinedEvent<> AbstractRootEvent AbstractStackBasedRefined. AbstractVertex<> Block<> BlockContainer<> Event<> EventHelper GridCell GridVertex LeafEvent<> NewEvent<> PassiveLeafEvent<> PassiveNewEvent<> PassivePersistentRefinedE. PassiveRefinedEvent<> PersistentRefinedEvent<> RefinedEvent<> RootEvent<> StaticLeafEvent<> Description Source Call Graph peano::grid::abstractvertex::ispassive ( Const Public Method ) Author: Tobias Weinzierl Is Vertex Passive. Syntax / parameters Return value Description Is Vertex Passive. If a vertex is passive, the event handler is not called for this vertex. A vertex is set passive by the NewEvent::createVertex() operation if the vertex and the whole surrounding support are outside of the domain. Be careful to make any vertex outside the computational domain a passive vertex. If you want to plot boundary cells, e.g., you have to ensure that all the vertices are plotted before you plot your cells. Yet, the touch operations are not invoked for passive vertices. In the parallel mode, each vertex is set passive that does not hold the actual node as adjacent element. Thus, vertices outside the computational partition are set passive although they might be inside the computational domain. This switch is implemented within derivepersistentvertexdatafromcoarsegrid(). 1 von :07 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 21 of 34

22 2.4. Floats # i n c l u d e <iostream> i n t main ( ) { / / hey dude, here we go double a ; double b ; double c ; a = 0.145e 07; b = 23.24e09 ; c = a+b ; std : : cout << c ; } Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 22 of 34

23 A Gedankenexperiment Fixed-point notation A number in a computer has to have a finite number of bits. This means a finite number of digits. Let s assume we have four digits in the form xx.yy. a = 01.50; b = 00.50; c = a / b ; d = c / 3; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 23 of 34

24 Floating-point notation A dynamic scheme (with a header, e.g.) cannot be fast (although Maple e.g. supports this if we need arbitary number of significant/valid digits). Define number into significant digits and exponent. Make the representation unique, i.e. ab.cd 10 4 = a.bcd This is a normalisation. In a binary system, base is 2 and first digit before comma always is 0 (or 1 respectively). So, we need one bit for the sign, s bits for the significant digits, one bit for the sign of the exponent, and e bits for the exponent. Type Sign Exponent Significand Total bits Single Double This is a at least standard! And there s two additional/special values: nan and inf. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 24 of 34

25 Normalisation & round-off errors # i n c l u d e <iostream> i n t main ( ) { / / hey dude, here we go double a ; double b ; double c ; a = 0.145e 07; b = 23.24e09 ; c = a+b ; std : : cout << c ; } Use your pocket calculator and assume there s eight significant bits! Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 25 of 34

26 Normalisation & round-off errors C 1 = C 2 = N i i=1 N/2 (i + N i) i=1 Which variant is the better one? What means stability in this context? Why is the condition number important within this context? We have to study all our algorithms in detail! Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 26 of 34

27 Some remarks on performance A Flop is a floating point operation. A MFlop is a...? How many Flops does the SuperMUC provide? If one of these GPGPU guys tells you something about performance, ask him about which precision he is using! Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 27 of 34

28 2.5. Simple Expressions The Assignment Statement int x; x = 10; Memory 21: 22; 23; 24; 10 x is alias 25; 26; 27; 28: The assignment operator takes expression from the right-hand side, evaluates it, and stores the result in the variable on the lefthand side. From a mathematical point of view, choosing = as assignment operator is a poor choice. Pascal, e.g., uses the := operator, which, from the author s point of view, is a better symbol. A declaration can directly be combined with an assignemnt. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 28 of 34

29 Arithmetic Expressions (for Numbers) c=10+2; assign variable c the value 12. a=10; b=a; a=2; afterwards, a holds 2 and b holds 10, i.e. a b. c=10*2; assign variable c the value 20. b=3; a=(b+2)*10; assign variable b the value 3. a becomes 50. a=2; a=a*2; this ain t a fixpoint formula, i.e. a holds the value 4 afterwards. a=3; a++; increment operator makes a hold 4. (unary operator) a=3; a--; decrement operator. a=2; a+=4; shortcut for a=a+4. a=2; a-=4; shortcut for a=a-4. a=2; a*=4; shortcut for a=a*4. a=a/2; divide value of a by 2. a=4; a/=2; shortcut for a=a/2. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 29 of 34

30 Further Expressions For booleans bool a=true; bool a=false; bool a=0; bool a=1; bool a=2; a=!a; a=!(a a); For characters char x= a ; char x=40; Comparsions bool x=a>4; bool x=a>b; bool x=a==b; bool x=a!=b; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 30 of 34

31 Fancy Initialisation : In C++, you can initialise all variables with brackets! int a = 10; int a(10); Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 31 of 34

32 Initialisation Pitfalls I Multiple Declarations int a; int b; int a,b; Shortcut int a,b =2 ; What does this mean? Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 32 of 34

33 Initialisation Pitfalls I Multiple Declarations int a; int b; int a,b; Shortcut int a,b =2 ; What does this mean? either declare one variable per line or use C++ initialisation with brackets. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 32 of 34

34 Initialisation Pitfalls II Implicit Type Conversion double a; double a=0.3; double a=10.0 / 4.0; double a=10.0 / 4; double a=10 / 4; Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 33 of 34

35 Initialisation Pitfalls II Implicit Type Conversion double a; double a=0.3; double a=10.0 / 4.0; double a=10.0 / 4; double a=10 / 4; If you use floating point arithmetics always write.0 for natural numbers. If you are interested in the underlying techniques, study the field of type inference. If you wanna have a more dynamic feeling, use a language with a dynamic type system. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 33 of 34

36 Initialisation Pitfalls III Assignment Operators int a = 3 bool b = (a==3); bool c = (a=3); int d = 0 bool e = (d==0); bool f = (d=0); Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 34 of 34

37 Initialisation Pitfalls IV Freaky Collaborators int a = 3; int b = ++a; (that is want we want) int c = 3; int d = c++; (that is something different) int e = (d+=a)++; (also very funny) int f = ((d=4)+a)--; C/C++ offer many strange constructs. This is good luck for posers, freaks, and people giving lectures. Others should avoid them. Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 35 of 34

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

6. Pointers, Structs, and Arrays. March 14 & 15, 2011 March 14 & 15, 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

6. Pointers, Structs, and Arrays. 1. Juli 2011

6. Pointers, Structs, and Arrays. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 50 Outline Recapitulation Pointers Dynamic Memory Allocation Structs Arrays Bubble Sort Strings Einführung

More information

4. Functions. March 10, 2010

4. Functions. March 10, 2010 March 10, 2010 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 40 Outline Recapitulation Functions Part 1 What is a Procedure? Call-by-value and Call-by-reference Functions

More information

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

C++ Programming: From Problem Analysis to Program Design, Third Edition C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 2: Basic Elements of C++ Objectives (continued) Become familiar with the use of increment and decrement operators Examine

More information

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

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program Objectives Chapter 2: Basic Elements of C++ In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Chapter 2: Basic Elements of C++

Chapter 2: Basic Elements of C++ Chapter 2: Basic Elements of C++ Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates

More information

Visual C# Instructor s Manual Table of Contents

Visual C# Instructor s Manual Table of Contents Visual C# 2005 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion Topics Additional Projects Additional Resources Key Terms

More information

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

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction Chapter 2: Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 1 Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers

More information

5. Applicative Programming. 1. Juli 2011

5. Applicative Programming. 1. Juli 2011 1. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Computer architecture extended: Registers and caches Header files Global variables

More information

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

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements Review: Exam 1 9/20/06 CS150 Introduction to Computer Science 1 1 Your First C++ Program 1 //*********************************************************** 2 // File name: hello.cpp 3 // Author: Shereen Khoja

More information

Full file at

Full file at Java Programming: From Problem Analysis to Program Design, 3 rd Edition 2-1 Chapter 2 Basic Elements of Java At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class

More information

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols.

C++ Basic Elements of COMPUTER PROGRAMMING. Special symbols include: Word symbols. Objectives. Programming. Symbols. Symbols. EEE-117 COMPUTER PROGRAMMING Basic Elements of C++ Objectives General Questions Become familiar with the basic components of a C++ program functions, special symbols, and identifiers Data types Arithmetic

More information

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

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018 C++ Basics Lecture 2 COP 3014 Spring 2018 January 8, 2018 Structure of a C++ Program Sequence of statements, typically grouped into functions. function: a subprogram. a section of a program performing

More information

Chapter 2 Basic Elements of C++

Chapter 2 Basic Elements of C++ C++ Programming: From Problem Analysis to Program Design, Fifth Edition 2-1 Chapter 2 Basic Elements of C++ At a Glance Instructor s Manual Table of Contents Overview Objectives s Quick Quizzes Class Discussion

More information

Objectives. In this chapter, you will:

Objectives. In this chapter, you will: Objectives In this chapter, you will: Become familiar with functions, special symbols, and identifiers in C++ Explore simple data types Discover how a program evaluates arithmetic expressions Learn about

More information

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5 C++ Data Types Contents 1 Simple C++ Data Types 2 2 Quick Note About Representations 3 3 Numeric Types 4 3.1 Integers (whole numbers)............................................ 4 3.2 Decimal Numbers.................................................

More information

These are notes for the third lecture; if statements and loops.

These are notes for the third lecture; if statements and loops. These are notes for the third lecture; if statements and loops. 1 Yeah, this is going to be the second slide in a lot of lectures. 2 - Dominant language for desktop application development - Most modern

More information

printf( Please enter another number: ); scanf( %d, &num2);

printf( Please enter another number: ); scanf( %d, &num2); CIT 593 Intro to Computer Systems Lecture #13 (11/1/12) Now that we've looked at how an assembly language program runs on a computer, we're ready to move up a level and start working with more powerful

More information

The C++ Language. Arizona State University 1

The C++ Language. Arizona State University 1 The C++ Language CSE100 Principles of Programming with C++ (based off Chapter 2 slides by Pearson) Ryan Dougherty Arizona State University http://www.public.asu.edu/~redoughe/ Arizona State University

More information

Fundamental of Programming (C)

Fundamental of Programming (C) Borrowed from lecturer notes by Omid Jafarinezhad Fundamental of Programming (C) Lecturer: Vahid Khodabakhshi Lecture 3 Constants, Variables, Data Types, And Operations Department of Computer Engineering

More information

10. Object-oriented Programming. 7. Juli 2011

10. Object-oriented Programming. 7. Juli 2011 7. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 47 Outline Object Case Study Brain Teaser Copy Constructor & Operators Object-oriented Programming, i.e.

More information

CSc Introduction to Computing

CSc Introduction to Computing CSc 10200 Introduction to Computing Lecture 2 Edgardo Molina Fall 2011 - City College of New York Thursday, September 1, 2011 Introduction to C++ Modular program: A program consisting of interrelated segments

More information

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I

BASIC COMPUTATION. public static void main(string [] args) Fundamentals of Computer Science I BASIC COMPUTATION x public static void main(string [] args) Fundamentals of Computer Science I Outline Using Eclipse Data Types Variables Primitive and Class Data Types Expressions Declaration Assignment

More information

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

Looping and Counting. Lecture 3 Hartmut Kaiser  hkaiser/fall_2012/csc1254.html Looping and Counting Lecture 3 Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2012/csc1254.html Abstract First we ll discuss types and type safety. Then we will modify the program

More information

11. Generic Programming and Design Patterns. 8. Juli 2011

11. Generic Programming and Design Patterns. 8. Juli 2011 8. Juli 2011 Einführung in die Programmierung Introduction to C/C++, Tobias Weinzierl page 1 of 26 Outline Recapitulation Template Programming An Overview over the STL Design Patterns (skipped) Evaluation/feedback

More information

3. Simple Types, Variables, and Constants

3. Simple Types, Variables, and Constants 3. Simple Types, Variables, and Constants This section of the lectures will look at simple containers in which you can storing single values in the programming language C++. You might find it interesting

More information

8. Object-oriented Programming. June 15, 2010

8. Object-oriented Programming. June 15, 2010 June 15, 2010 Introduction to C/C++, Tobias Weinzierl page 1 of 41 Outline Recapitulation Copy Constructor & Operators Object-oriented Programming Dynamic and Static Polymorphism The Keyword Static The

More information

C++ Support Classes (Data and Variables)

C++ Support Classes (Data and Variables) C++ Support Classes (Data and Variables) School of Mathematics 2018 Today s lecture Topics: Computers and Programs; Syntax and Structure of a Program; Data and Variables; Aims: Understand the idea of programming

More information

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING KEY CONCEPTS COMP 10 EXPLORING COMPUTER SCIENCE Lecture 2 Variables, Types, and Programs Problem Definition of task to be performed (by a computer) Algorithm A particular sequence of steps that will solve

More information

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Parts of a C++ Program Parts of a C++ Program // sample C++ program

More information

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents:

Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language. Dr. Amal Khalifa. Lecture Contents: Structured Programming Using C++ Lecture 2 : Introduction to the C++ Language Dr. Amal Khalifa Lecture Contents: Introduction to C++ Origins Object-Oriented Programming, Terms Libraries and Namespaces

More information

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York CSc 10200! Introduction to Computing Lecture 2-3 Edgardo Molina Fall 2013 City College of New York 1 C++ for Engineers and Scientists Third Edition Chapter 2 Problem Solving Using C++ 2 Objectives In this

More information

Lecture 2 Tao Wang 1

Lecture 2 Tao Wang 1 Lecture 2 Tao Wang 1 Objectives In this chapter, you will learn about: Modular programs Programming style Data types Arithmetic operations Variables and declaration statements Common programming errors

More information

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

Looping and Counting. Lecture 3. Hartmut Kaiser  hkaiser/fall_2011/csc1254.html Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2011/csc1254.html 2 Abstract First we ll discuss types and type safety. Then we will modify the program we developed last time (Framing

More information

LECTURE 02 INTRODUCTION TO C++

LECTURE 02 INTRODUCTION TO C++ PowerPoint Slides adapted from *Starting Out with C++: From Control Structures through Objects, 7/E* by *Tony Gaddis* Copyright 2012 Pearson Education Inc. COMPUTER PROGRAMMING LECTURE 02 INTRODUCTION

More information

CS242 COMPUTER PROGRAMMING

CS242 COMPUTER PROGRAMMING CS242 COMPUTER PROGRAMMING I.Safa a Alawneh Variables Outline 2 Data Type C++ Built-in Data Types o o o o bool Data Type char Data Type int Data Type Floating-Point Data Types Variable Declaration Initializing

More information

3. Java - Language Constructs I

3. Java - Language Constructs I Educational Objectives 3. Java - Language Constructs I Names and Identifiers, Variables, Assignments, Constants, Datatypes, Operations, Evaluation of Expressions, Type Conversions You know the basic blocks

More information

Object-oriented Programming in C++

Object-oriented Programming in C++ Object-oriented Programming in C++ Working with C and C++ Wolfgang Eckhardt, Tobias Neckel March 23, 2015 Working with C and C++, March 23, 2015 1 Challenges of This Course Heterogeneity of the audience

More information

Scientific Computing

Scientific Computing Scientific Computing Martin Lotz School of Mathematics The University of Manchester Lecture 1, September 22, 2014 Outline Course Overview Programming Basics The C++ Programming Language Outline Course

More information

Exercise: Using Numbers

Exercise: Using Numbers Exercise: Using Numbers Problem: You are a spy going into an evil party to find the super-secret code phrase (made up of letters and spaces), which you will immediately send via text message to your team

More information

Chapter 2: Introduction to C++

Chapter 2: Introduction to C++ Chapter 2: Introduction to C++ Copyright 2010 Pearson Education, Inc. Copyright Publishing as 2010 Pearson Pearson Addison-Wesley Education, Inc. Publishing as Pearson Addison-Wesley 2.1 Parts of a C++

More information

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

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen Chapter 2: Introduction to C++ 2.1 Parts of a C++ Program Copyright 2009 Pearson Education, Inc. Copyright 2009 Publishing Pearson as Pearson Education, Addison-Wesley Inc. Publishing as Pearson Addison-Wesley

More information

UNIT- 3 Introduction to C++

UNIT- 3 Introduction to C++ UNIT- 3 Introduction to C++ C++ Character Sets: Letters A-Z, a-z Digits 0-9 Special Symbols Space + - * / ^ \ ( ) [ ] =!= . $, ; : %! &? _ # = @ White Spaces Blank spaces, horizontal tab, carriage

More information

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

Lab # 02. Basic Elements of C++ _ Part1 Lab # 02 Basic Elements of C++ _ Part1 Lab Objectives: After performing this lab, the students should be able to: Become familiar with the basic components of a C++ program, including functions, special

More information

Computational Physics Operating systems

Computational Physics Operating systems Computational Physics numerical methods with C++ (and UNIX) 2018-19 Fernando Barao Instituto Superior Tecnico, Dep. Fisica email: fernando.barao@tecnico.ulisboa.pt Computational Physics 2018-19 (Phys Dep

More information

Computer Programming : C++

Computer Programming : C++ The Islamic University of Gaza Engineering Faculty Department of Computer Engineering Fall 2017 ECOM 2003 Muath i.alnabris Computer Programming : C++ Experiment #1 Basics Contents Structure of a program

More information

Programming with C++ as a Second Language

Programming with C++ as a Second Language Programming with C++ as a Second Language Week 2 Overview of C++ CSE/ICS 45C Patricia Lee, PhD Chapter 1 C++ Basics Copyright 2016 Pearson, Inc. All rights reserved. Learning Objectives Introduction to

More information

Integer Data Types. Data Type. Data Types. int, short int, long int

Integer Data Types. Data Type. Data Types. int, short int, long int Data Types Variables are classified according to their data type. The data type determines the kind of information that may be stored in the variable. A data type is a set of values. Generally two main

More information

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics PIC 10A Pointers, Arrays, and Dynamic Memory Allocation Ernest Ryu UCLA Mathematics Pointers A variable is stored somewhere in memory. The address-of operator & returns the memory address of the variable.

More information

Chapter 2: Overview of C++

Chapter 2: Overview of C++ Chapter 2: Overview of C++ Problem Solving, Abstraction, and Design using C++ 6e by Frank L. Friedman and Elliot B. Koffman C++ Background Introduced by Bjarne Stroustrup of AT&T s Bell Laboratories in

More information

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

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 1 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 variables. Apply C++ syntax rules to declare variables, initialize

More information

Reserved Words and Identifiers

Reserved Words and Identifiers 1 Programming in C Reserved Words and Identifiers Reserved word Word that has a specific meaning in C Ex: int, return Identifier Word used to name and refer to a data element or object manipulated by the

More information

6.096 Introduction to C++ January (IAP) 2009

6.096 Introduction to C++ January (IAP) 2009 MIT OpenCourseWare http://ocw.mit.edu 6.096 Introduction to C++ January (IAP) 2009 For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms. Welcome to 6.096 Lecture

More information

Syntax and Variables

Syntax and Variables Syntax and Variables What the Compiler needs to understand your program, and managing data 1 Pre-Processing Any line that starts with # is a pre-processor directive Pre-processor consumes that entire line

More information

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003

Objects and Types. COMS W1007 Introduction to Computer Science. Christopher Conway 29 May 2003 Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 Java Programs A Java program contains at least one class definition. public class Hello { public static void

More information

A Java program contains at least one class definition.

A Java program contains at least one class definition. Java Programs Identifiers Objects and Types COMS W1007 Introduction to Computer Science Christopher Conway 29 May 2003 A Java program contains at least one class definition. public class Hello { public

More information

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

Intro to Programming & C Why Program? 1.2 Computer Systems: Hardware and Software. Why Learn to Program? Intro to Programming & C++ Unit 1 Sections 1.1-4 and 2.1-10, 2.12-13, 2.15-17 CS 1428 Spring 2019 Jill Seaman 1.1 Why Program? Computer programmable machine designed to follow instructions Program a set

More information

Introduction to Scientific Programming with C++

Introduction to Scientific Programming with C++ Introduction to Scientific Programming with C++ Session 0: Basics Martin Uhrin edited by Andrew Maxwell and Ahmed Al Refaie UCL November 6, 2016 1 / 26 Table of Contents 1 Introduction to language 2 Program

More information

COMP2611: Computer Organization. Data Representation

COMP2611: Computer Organization. Data Representation COMP2611: Computer Organization Comp2611 Fall 2015 2 1. Binary numbers and 2 s Complement Numbers 3 Bits: are the basis for binary number representation in digital computers What you will learn here: How

More information

Programming, numerics and optimization

Programming, numerics and optimization Programming, numerics and optimization Lecture A-2: Programming basics II Łukasz Jankowski ljank@ippt.pan.pl Institute of Fundamental Technological Research Room 4.32, Phone +22.8261281 ext. 428 March

More information

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

Overview. - General Data Types - Categories of Words. - Define Before Use. - The Three S s. - End of Statement - My First Program Overview - General Data Types - Categories of Words - The Three S s - Define Before Use - End of Statement - My First Program a description of data, defining a set of valid values and operations List of

More information

CSCI 1061U Programming Workshop 2. C++ Basics

CSCI 1061U Programming Workshop 2. C++ Basics CSCI 1061U Programming Workshop 2 C++ Basics 1 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output

More information

Professor Peter Cheung EEE, Imperial College

Professor Peter Cheung EEE, Imperial College 1/1 1/2 Professor Peter Cheung EEE, Imperial College In this lecture, we take an overview of the course, and briefly review the programming language. The rough guide is not very complete. You should use

More information

Fundamentals of Programming

Fundamentals of Programming Fundamentals of Programming Lecture 3 - Constants, Variables, Data Types, And Operations Lecturer : Ebrahim Jahandar Borrowed from lecturer notes by Omid Jafarinezhad Outline C Program Data types Variables

More information

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

C++ PROGRAMMING. For Industrial And Electrical Engineering Instructor: Ruba A. Salamh C++ PROGRAMMING For Industrial And Electrical Engineering Instructor: Ruba A. Salamh CHAPTER TWO: Fundamental Data Types Chapter Goals In this chapter, you will learn how to work with numbers and text,

More information

Chapter 2. Outline. Simple C++ Programs

Chapter 2. Outline. Simple C++ Programs Chapter 2 Simple C++ Programs Outline Objectives 1. Building C++ Solutions with IDEs: Dev-cpp, Xcode 2. C++ Program Structure 3. Constant and Variables 4. C++ Operators 5. Standard Input and Output 6.

More information

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

CS2141 Software Development using C/C++ C++ Basics CS2141 Software Development using C/C++ C++ Basics Integers Basic Types Can be short, long, or just plain int C++ does not define the size of them other than short

More information

DEPARTMENT OF MATHS, MJ COLLEGE

DEPARTMENT OF MATHS, MJ COLLEGE T. Y. B.Sc. Mathematics MTH- 356 (A) : Programming in C Unit 1 : Basic Concepts Syllabus : Introduction, Character set, C token, Keywords, Constants, Variables, Data types, Symbolic constants, Over flow,

More information

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur

Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Programming in C++ Prof. Partha Pratim Das Department of Computer Science and Engineering Indian Institute of Technology, Kharagpur Lecture 04 Programs with IO and Loop We will now discuss the module 2,

More information

REVIEW. The C++ Programming Language. CS 151 Review #2

REVIEW. The C++ Programming Language. CS 151 Review #2 REVIEW The C++ Programming Language Computer programming courses generally concentrate on program design that can be applied to any number of programming languages on the market. It is imperative, however,

More information

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

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable Basic C++ Overview C++ is a version of the older C programming language. This is a language that is used for a wide variety of applications and which has a mature base of compilers and libraries. C++ is

More information

Variables and Constants

Variables and Constants HOUR 3 Variables and Constants Programs need a way to store the data they use. Variables and constants offer various ways to work with numbers and other values. In this hour you learn: How to declare and

More information

Introduction to the C++ Programming Language

Introduction to the C++ Programming Language LESSON SET 2 Introduction to the C++ Programming Language OBJECTIVES FOR STUDENT Lesson 2A: 1. To learn the basic components of a C++ program 2. To gain a basic knowledge of how memory is used in programming

More information

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

Character Set. The character set of C represents alphabet, digit or any symbol used to represent information. Digits 0, 1, 2, 3, 9 Character Set The character set of C represents alphabet, digit or any symbol used to represent information. Types Uppercase Alphabets Lowercase Alphabets Character Set A, B, C, Y, Z a, b, c, y, z Digits

More information

Chapter 1 INTRODUCTION

Chapter 1 INTRODUCTION Chapter 1 INTRODUCTION A digital computer system consists of hardware and software: The hardware consists of the physical components of the system. The software is the collection of programs that a computer

More information

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab.

University of Technology. Laser & Optoelectronics Engineering Department. C++ Lab. University of Technology Laser & Optoelectronics Engineering Department C++ Lab. Second week Variables Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable.

More information

Object-oriented Programming in C++

Object-oriented Programming in C++ Object-oriented Programming in C++ Working with C and C++ Wolfgang Eckhardt, Tobias Neckel June 30, 2014 Working with C and C++, June 30, 2014 1 Challenges of This Course Heterogeneity of the audience

More information

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed

Today. o main function. o cout object. o Allocate space for data to be used in the program. o The data can be changed CS 150 Introduction to Computer Science I Data Types Today Last we covered o main function o cout object o How data that is used by a program can be declared and stored Today we will o Investigate the

More information

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved

Chapter 1. C++ Basics. Copyright 2010 Pearson Addison-Wesley. All rights reserved Chapter 1 C++ Basics Copyright 2010 Pearson Addison-Wesley. All rights reserved Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment

More information

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

Types, Values, Variables & Assignment. EECS 211 Winter 2018 Types, Values, Variables & Assignment EECS 211 Winter 2018 2 Road map Strings and string I/O Integers and integer I/O Types and objects * Type safety * Not as in object orientation we ll get to that much

More information

Motivation was to facilitate development of systems software, especially OS development.

Motivation was to facilitate development of systems software, especially OS development. A History Lesson C Basics 1 Development of language by Dennis Ritchie at Bell Labs culminated in the C language in 1972. Motivation was to facilitate development of systems software, especially OS development.

More information

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics Java Programming, Sixth Edition 2-1 Chapter 2 Using Data At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional Projects Additional

More information

This watermark does not appear in the registered version - Slide 1

This watermark does not appear in the registered version -   Slide 1 Slide 1 Chapter 1 C++ Basics Slide 2 Learning Objectives Introduction to C++ Origins, Object-Oriented Programming, Terms Variables, Expressions, and Assignment Statements Console Input/Output Program Style

More information

Computer Systems Principles. C Pointers

Computer Systems Principles. C Pointers Computer Systems Principles C Pointers 1 Learning Objectives Learn about floating point number Learn about typedef, enum, and union Learn and understand pointers 2 FLOATING POINT NUMBER 3 IEEE Floating

More information

Variables and literals

Variables and literals Demo lecture slides Although I will not usually give slides for demo lectures, the first two demo lectures involve practice with things which you should really know from G51PRG Since I covered much of

More information

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

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char Week 1 Operators, Data Types & I/O Gaddis: Chapters 1, 2, 3 CS 5301 Fall 2016 Jill Seaman Programming A program is a set of instructions that the computer follows to perform a task It must be translated

More information

Full file at

Full file at Java Programming, Fifth Edition 2-1 Chapter 2 Using Data within a Program At a Glance Instructor s Manual Table of Contents Overview Objectives Teaching Tips Quick Quizzes Class Discussion Topics Additional

More information

Values and Variables 1 / 30

Values and Variables 1 / 30 Values and Variables 1 / 30 Values 2 / 30 Computing Computing is any purposeful activity that marries the representation of some dynamic domain with the representation of some dynamic machine that provides

More information

Variables. Data Types.

Variables. Data Types. Variables. Data Types. The usefulness of the "Hello World" programs shown in the previous section is quite questionable. We had to write several lines of code, compile them, and then execute the resulting

More information

Floating Point Arithmetic

Floating Point Arithmetic Floating Point Arithmetic CS 365 Floating-Point What can be represented in N bits? Unsigned 0 to 2 N 2s Complement -2 N-1 to 2 N-1-1 But, what about? very large numbers? 9,349,398,989,787,762,244,859,087,678

More information

A First Program - Greeting.cpp

A First Program - Greeting.cpp C++ Basics A First Program - Greeting.cpp Preprocessor directives Function named main() indicates start of program // Program: Display greetings #include using namespace std; int main() { cout

More information

Getting started with C++ (Part 2)

Getting started with C++ (Part 2) Getting started with C++ (Part 2) CS427: Elements of Software Engineering Lecture 2.2 11am, 16 Jan 2012 CS427 Getting started with C++ (Part 2) 1/22 Outline 1 Recall from last week... 2 Recall: Output

More information

CHAPTER 3 BASIC INSTRUCTION OF C++

CHAPTER 3 BASIC INSTRUCTION OF C++ CHAPTER 3 BASIC INSTRUCTION OF C++ MOHD HATTA BIN HJ MOHAMED ALI Computer programming (BFC 20802) Subtopics 2 Parts of a C++ Program Classes and Objects The #include Directive Variables and Literals Identifiers

More information

Introduction to Programming using C++

Introduction to Programming using C++ Introduction to Programming using C++ Lecture One: Getting Started Carl Gwilliam gwilliam@hep.ph.liv.ac.uk http://hep.ph.liv.ac.uk/~gwilliam/cppcourse Course Prerequisites What you should already know

More information

CEN 414 Java Programming

CEN 414 Java Programming CEN 414 Java Programming Instructor: H. Esin ÜNAL SPRING 2017 Slides are modified from original slides of Y. Daniel Liang WEEK 2 ELEMENTARY PROGRAMMING 2 Computing the Area of a Circle public class ComputeArea

More information

CMPT 125: Lecture 3 Data and Expressions

CMPT 125: Lecture 3 Data and Expressions CMPT 125: Lecture 3 Data and Expressions Tamara Smyth, tamaras@cs.sfu.ca School of Computing Science, Simon Fraser University January 3, 2009 1 Character Strings A character string is an object in Java,

More information

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1 Chapter 2 Input, Processing, and Output Fall 2016, CSUS Designing a Program Chapter 2.1 1 Algorithms They are the logic on how to do something how to compute the value of Pi how to delete a file how to

More information

Numerical computing. How computers store real numbers and the problems that result

Numerical computing. How computers store real numbers and the problems that result Numerical computing How computers store real numbers and the problems that result The scientific method Theory: Mathematical equations provide a description or model Experiment Inference from data Test

More information

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2

Python for Analytics. Python Fundamentals RSI Chapters 1 and 2 Python for Analytics Python Fundamentals RSI Chapters 1 and 2 Learning Objectives Theory: You should be able to explain... General programming terms like source code, interpreter, compiler, object code,

More information

Bits, Words, and Integers

Bits, Words, and Integers Computer Science 52 Bits, Words, and Integers Spring Semester, 2017 In this document, we look at how bits are organized into meaningful data. In particular, we will see the details of how integers are

More information