Chapter 02 (Part II) Introduction to C++ Programming.

Slides:



Advertisements
Similar presentations
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
Advertisements

Lecture 2 Introduction to C Programming
Introduction to C Programming
CS 6301 Lecture 2: First Program1. CS Topics of this lecture Introduce first program  Explore inputs and outputs of a program Arithmetic using.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 1 – Introduction to Computers and C++ Programming Outline 1.6 Machine Languages, Assembly Languages,
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline History of C and C++ C++ Standard Library Object Technology Basics.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
 2003 Prentice Hall, Inc. All rights reserved. 1 Machine Languages, Assembly Languages, and High-level Languages Three types of computer languages 1.Machine.
Introduction to C Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
Introduction to C++ Programming
COMPUTER SCIENCE I C++ INTRODUCTION
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
 2008 Pearson Education, Inc. All rights reserved. 1 CISC 1600 – Computer Science I Fall 2010 Introduction to C++ Programming Chapters 1 and 2 (Deitel.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
C++ How to Program, 8/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
C++ How to Program, Late Objects Version, 7/e © by Pearson Education, Inc. All Rights Reserved.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
Announcements Starting next week class 6-8 on Thursday Homework 1 on the web  Due January 29 – next class meeting  Homework policy No late assignments.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 3 – Inventory Application: Introducing Variables,
計算機程式語言 Lecture 02-1 國立台灣大學生物機電系 林達德 2 2 Introduction to C++ Programming.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
STRUCTURED PROGRAMMING C++ Operators. Content 2  C++ operators  Assignment operators  Arithmetic operators  Increment and decrement operators  Decision.
LECTEURE # 5 : STRUCTURED PROGRAMMING VARIABLES, INPUT, MEMORY المتغيرات, المدخلات, الذاكرة By Mr. Ali Edan.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
 2008 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
 2006 Pearson Education, Inc. All rights reserved Introduction to C++ Programming.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Chapter 2 of C++ How to Program, 10/e © by Pearson Education, Inc. All Rights Reserved.
© by Pearson Education, Inc. All Rights Reserved.
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
ARITHMETIC IN C Operators.
Chapter 1.2 Introduction to C++ Programming
Chapter 1: Introduction to computers and C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Introduction to C++ Programming
Chapter 2 Introduction to C++ Programming
1.2 What is a Computer? Computer Computer programs Hardware Software
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
1.13 The Key Software Trend: Object Technology
Chapter 2 - Introduction to C Programming
Variables T.Najah Al_Subaie Kingdom of Saudi Arabia
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Programs written in C and C++ can run on many different computers
Capitolo 1 – Introduction C++ Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Presentation transcript:

Chapter 02 (Part II) Introduction to C++ Programming

Goal Example: –How to obtain information from the user. –How to perform arithmetic calculations Introduction to computer memory.

Obtaining Two Integers and Summing Them Up

Variables We can store data in variables. –Variable must be declared with name and data type before used. For example: int integer1; int integer2; –Several variables of same type can be written in one declaration. For example, int interger1, integer2, sum; Data typeVariable name Place a space after each comma (,) to make programs more readable.

Principles of naming a Variable The declaration of variables (in a block) cannot be duplicate. Use identifiers of 31 characters or fewer to ensure portability. Choosing meaningful identifiers. Avoid identifiers that begin with underscores. Always place a blank line between a declaration and adjacent executable statements.

Capturing Input Input stream object –std::cin Usually connected to keyboard Stream extraction operator >> –Waits for user to input value until the user presses Enter. –Stores value in variable to right of the operator »Converts value to variable data type Example –std::cin >> number1; »Reads an integer typed at the keyboard »Stores the integer in variable number1

Adding integers Assignment operator = –Assigns value on right to variable on left (correct) x = 3; (error)3 = x; –Binary operator (two operands) lvalue = rvalue; –Example: sum = integer1 + integer2; –Adding the values of integer1 and integer2 –Storing result in sum Remember the direction of assigning a value! int x = 3; int y = x + 2;

Outputting the Result Stream manipulator std::endl –Outputs a newline. –Flushes output buffer. Concatenating stream insertion operations –Also called chaining or cascading. Stream insertion operator knows how to output each type of data. –Example … sum = number1 + number2; std::cout << "Sum is “; std::cout << sum; std::cout << std::endl; std::cout << "Sum is " << number1 + number2 << std::endl; Outputs "Sum is “ Then, outputs sum of number1 and number2 Then, outputs newline and flushes output buffer

Obtaining Two Integers and Summing Them Up

2.5 Memory Concept Variable names –Correspond to actual locations (addresses) in computer's memory Every variable has name, type, size and value number1 45

2.5 Memory Concept A compiler maintain a table that maps variable names to real addresses, and their types (memory size) Memory cell number1 2 int number2 7 int 45 72

2.5 Memory Concept Variable names –When new value placed into variable, overwrites old value. Writing to memory is destructive. Reading variables from memory is nondestructive. –Example: sum = number1 + number2; –Value of sum is overwritten. –Values of number1 and number2 remain intact.

2.6 Arithmetic Arithmetic operators –% Modulus operator returns remainder –7 % 5 evaluates to 2 –Attempting to use the modulus operator ( % ) with non-integer operands is a compilation error.

2.6 Arithmetic (Cont.) Straight-line form –All constants, variables and operators appear in a straight line Grouping subexpressions –Parentheses are used in C++ expressions to group subexpressions –Example a * ( b + c ) –Multiple a times the quantity b + c

2.6 Arithmetic Rules of operator precedence –Operators in parentheses evaluated first. –Multiplication, division, modulus applied next Operators applied from left to right –Addition, subtraction applied last Operators applied from left to right

Exercise