Lecture 4 Tao Wang 1

Similar documents
CSc 10200! Introduction to Computing. Lecture 4-5 Edgardo Molina Fall 2013 City College of New York

Chapter 3 : Assignment and Interactive Input (pp )

C Programming

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

CHAPTER 3 Expressions, Functions, Output

Engineering Problem Solving with C++, Etter/Ingber

Lecture 3 Tao Wang 1

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

BITG 1233: Introduction to C++

Expressions, Input, Output and Data Type Conversions

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

Introduction to C++ (Extensions to C)

Chapter 3. Numeric Types, Expressions, and Output

UNIT- 3 Introduction to C++

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

Chapter 2 Basic Elements of C++

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.

Window s Visual Studio Output

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

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.

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

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

I/O Streams and Standard I/O Devices (cont d.)

3.1. Chapter 3: The cin Object in Program 3-1. Displaying a Prompt 8/23/2014. The cin Object

Chapter 3: Expressions and Interactivity

Chapter 2. Outline. Simple C++ Programs

For Teacher's Use Only Q No Total Q No Q No

Chapter 3: Expressions and Interactivity. Copyright 2012 Pearson Education, Inc. Thursday, October 9, 14

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

3.1. Chapter 3: Displaying a Prompt. Expressions and Interactivity

Visual C# Instructor s Manual Table of Contents

C++ As A "Better C" Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan.

Getting started with C++ (Part 2)

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

Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

3.1. Chapter 3: The cin Object. Expressions and Interactivity

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

Full file at

Chapter 2: Basic Elements of C++

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

Unit 3. Operators. School of Science and Technology INTRODUCTION

Come and join us at WebLyceum

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

C++ Input/Output: Streams

CS2141 Software Development using C/C++ Stream I/O

Lecture 3. Input and Output. Review from last week. Variable - place to store data in memory. identified by a name should be meaningful Has a type-

Chapter 7. Additional Control Structures

CS201 Some Important Definitions

JAYARAM COLLEGE OF ENGINEERING AND TECHNOLOGY Pagalavadi, Tiruchirappalli (An approved by AICTE and Affiliated to Anna University)

Homework #3 CS2255 Fall 2012

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

Collected By Anonymous

Lecture 18 Tao Wang 1

Programming with C++ as a Second Language

Understanding main() function Input/Output Streams

Reserved Words and Identifiers

Objectives. In this chapter, you will:

CSI33 Data Structures

CS201 Solved MCQs.

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

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

C++ Programming: From Problem Analysis to Program. Design, Fifth Edition. Chapter 1: An Overview of Computers and Programming Languages

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

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 2 - I know what I want to do NOW WHAT? Student Learning Outcomes (SLOs)

PART I. Part II Answer to all the questions 1. What is meant by a token? Name the token available in C++.

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

Chapter 3 - Notes Input/Output

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

Jayaram college of Engineering and Technology, Pagalavadi. CS2203 Object Oriented Programming Question Bank Prepared By: S.Gopalakrishnan, Lecturer/IT

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

Introduction to Computers and C++ Programming p. 1 Computer Systems p. 2 Hardware p. 2 Software p. 7 High-Level Languages p. 8 Compilers p.

Absolute C++ Walter Savitch

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Introduction to C++ Introduction to C++ 1

Differentiate Between Keywords and Identifiers

Page. No. 1/15 CS201 Introduction to Programmming Solved Subjective Questions From spring 2010 Final Term Papers By vuzs Team

Work relative to other classes

UNIT-2 Introduction to C++

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

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

LECTURE 02 INTRODUCTION TO C++

7.1 Optional Parameters

CS201 Latest Solved MCQs

Chapter 2. Lexical Elements & Operators

Fundamentals of Programming Session 7

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

Lecture 2 Tao Wang 1

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

The C++ Language. Arizona State University 1

I Internal Examination Sept Class: - BCA I Subject: - Principles of Programming Lang. (BCA 104) MM: 40 Set: A Time: 1 ½ Hrs.

Definition Matching (10 Points)

Lecture 8 Tao Wang 1

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

2.1-First.cpp #include <iostream> // contains information to support input / output using namespace std;

Introduction to C++ Systems Programming

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Introduction to Programming EC-105. Lecture 2

Transcription:

Lecture 4 Tao Wang 1

Objectives In this chapter, you will learn about: Assignment operations Formatting numbers for program output Using mathematical library functions Symbolic constants Common programming errors C++ for Engineers and Scientists, Third Edition 2

Assignment Operations Assignment Statement: Assigns the value of the expression on the right side of the = to the variable on the left side of the = Another assignment statement using the same variable will overwrite the previous value with the new value Examples: slope = 3.7; slope = 6.28; C++ for Engineers and Scientists, Third Edition 3

Assignment Operations (continued) Right side of an assignment statement may contain any expression that can be evaluated to a value Examples: newtotal = 18.3 + total; taxes =.06*amount; average = sum / items; Only one variable can be on the left side of an assignment statement C++ for Engineers and Scientists, Third Edition 4

Assignment Operations (continued) C++ for Engineers and Scientists, Third Edition 5

Assignment Operations (continued) Assignment operator: The = sign C++ statement: Any expression terminated by a semicolon Multiple assignments in the same expression are possible Example: a = b = c = 25; C++ for Engineers and Scientists, Third Edition 6

Assignment Operations (continued) Coercion: Forcing a data value to another data type Value of the expression on the right side of an assignment statement will be coerced (converted) to the data type of the variable on the left side during evaluation Variable on the left side may also be used on the right side of an assignment statement C++ for Engineers and Scientists, Third Edition 7

Assignment Operations (continued) C++ for Engineers and Scientists, Third Edition 8

Assignment Operations (continued) Accumulation statement: Has the effect of accumulating, or totaling Syntax: variable = variable + newvalue; C++ for Engineers and Scientists, Third Edition 9

Assignment Operations (continued) Additional assignment operators provide short cuts: +=, -=, *=, /=, %= Example: sum = sum + 10; is equivalent to: sum += 10; price *= rate +1; is equivalent to: price = price * (rate + 1); C++ for Engineers and Scientists, Third Edition 10

Assignment Operations (continued) C++ for Engineers and Scientists, Third Edition 11

Assignment Operations (continued) Counting statement: Adds a fixed value to the variable s current value Syntax: variable = variable + fixednumber; Example: i = i + 1; count = count + 1; C++ for Engineers and Scientists, Third Edition 12

Assignment Operations (continued) Increment operator ++: Unary operator for the special case when a variable is increased by 1 Prefix increment operator appears before the variable Example: ++i Postfix increment operator appears after the variable Example: i++ C++ for Engineers and Scientists, Third Edition 13

Assignment Operations (continued) Example: k = ++n; //prefix increment is equivalent to: n = n + 1; //increment n first k = n; //assign n s value to k Example: k = n++; //postfix increment is equivalent to k = n; n = n + 1; //assign n s value to k //and then increment n C++ for Engineers and Scientists, Third Edition 14

Assignment Operations (continued) Decrement operator --: Unary operator for the special case when a variable is decreased by 1 Prefix decrement operator appears before the variable Example: --i; Postfix decrement operator appears after the variable Example: i--; C++ for Engineers and Scientists, Third Edition 15

Formatting Numbers for Program Output Proper output formatting contributes to ease of use and user satisfaction cout with stream manipulators can control output formatting C++ for Engineers and Scientists, Third Edition 16

Formatting Numbers for Program Output (continued) Table 3.1 Commonly Used Stream Manipulators C++ for Engineers and Scientists, Third Edition 17

Formatting Numbers for Program Output (continued) Table 3.1 Commonly Used Stream Manipulators (continued) C++ for Engineers and Scientists, Third Edition 18

Formatting Numbers for Program Output (continued) C++ for Engineers and Scientists, Third Edition 19

Formatting Numbers for Program Output (continued) The field width manipulator must be included for each value in the data stream sent to cout Other manipulators remain in effect until they are changed iomanip header file must be included to use manipulators requiring arguments C++ for Engineers and Scientists, Third Edition 20

Formatting Numbers for Program Output (continued) Formatting floating-point numbers requires three field-width manipulators to: Set the total width of the display Force a decimal place Set the number of significant digits after the decimal point Example: cout << " " << setw(10) << fixed << setprecision(3) << 25.67 << " "; produces this output: 25.670 C++ for Engineers and Scientists, Third Edition 21

Formatting Numbers for Program Output (continued) setprecision: Sets number of digits after decimal point if a decimal point has been explicitly forced; otherwise, it sets the total number of displayed digits If the field width is too small, cout ignores the setw manipulator setting and allocates enough space for printing If setprecision setting is too small, the fractional part of the value is rounded to the specified number of decimal places C++ for Engineers and Scientists, Third Edition 22

Formatting Numbers for Program Output (continued) If setprecision value is too large, the fractional value is displayed with its current size C++ for Engineers and Scientists, Third Edition 23

Formatting Numbers for Program Output (continued) Table 3.2 Effect of Format Manipulators C++ for Engineers and Scientists, Third Edition 24

Formatting Numbers for Program Output (continued) Table 3.2 Effect of Format Manipulators (continued) C++ for Engineers and Scientists, Third Edition 25

Formatting Numbers for Program Output (continued) setiosflags manipulator: Allows additional formatting: Right or left justification Fixed display with 6 decimal places Scientific notation with exponential display Display of a leading + sign Parameterized manipulator: One which requires arguments, or parameters C++ for Engineers and Scientists, Third Edition 26

Formatting Numbers for Program Output (continued) Table 3.3 Format Flags for Use with setiosflags() C++ for Engineers and Scientists, Third Edition 27

Formatting Numbers for Program Output (continued) C++ for Engineers and Scientists, Third Edition 28

Formatting Numbers for Program Output (continued) To designate an octal integer constant, use a leading zero To designate a hexadecimal integer constant, use a leading 0x Manipulators affect only output; the value stored internally does not change C++ for Engineers and Scientists, Third Edition 29

Formatting Numbers for Program Output (continued) C++ for Engineers and Scientists, Third Edition 30

Formatting Numbers for Program Output (continued) Manipulators can also be set using the ostream class methods Separate the cout object name from the method name with a period Example: cout.precision(2) C++ for Engineers and Scientists, Third Edition 31

Formatting Numbers for Program Output (continued) Table 3.4 ostream Class Functions C++ for Engineers and Scientists, Third Edition 32

Using Mathematical Library Functions C++ has preprogrammed mathematical functions that can be included in a program You must include the cmath header file: #include <cmath> Math functions require one or more arguments as input, but will return only one value All functions are overloaded, and can be used with integer and real arguments C++ for Engineers and Scientists, Third Edition 33

Using Mathematical Library Functions (continued) Table 3.5 Common C++ Functions C++ for Engineers and Scientists, Third Edition 34

Using Mathematical Library Functions (continued) To use a math function, give its name and pass the input arguments within parentheses Expressions that can be evaluated to a value can be passed as arguments Figure 3.10 Using and passing data to a function C++ for Engineers and Scientists, Third Edition 35

Using Mathematical Library Functions (continued) Determine the time it takes a ball to hit the ground after it has been dropped from an 800-foot tower C++ for Engineers and Scientists, Third Edition 36

Using Mathematical Library Functions (continued) Function calls can be nested Example: sqrt(sin(abs(theta))) Cast operator: A unary operator that forces the data to the desired data type Compile-time cast Syntax: datatype (expression) Example: int(a+b) C++ for Engineers and Scientists, Third Edition 37

Using Mathematical Library Functions (continued) Run-time cast: The requested conversion is checked at run time and applied if valid Syntax: staticcast<data-type> (expression) Example: staticcast<int>(a*b) C++ for Engineers and Scientists, Third Edition 38

Symbolic Constants Symbolic constant: Constant value that is declared with an identifier using the const keyword A constant s value may not be changed Example: const int MAXNUM = 100; Good programming places statements in appropriate order C++ for Engineers and Scientists, Third Edition 39

Symbolic Constants (continued) Proper placement of statements: preprocessor directives int main() { symbolic constants main function declarations } other executable statements return value C++ for Engineers and Scientists, Third Edition 40

A Closer Look: Programming Errors Program errors may be detected in four ways: Before a program is compiled (desk checking) While it is being compiled (compile-time errors) While it is being run (run-time errors) While examining the output after completion Errors may be: Typos in the source code Logic errors C++ for Engineers and Scientists, Third Edition 41

A Closer Look: Programming Errors (continued) Logic errors: Often difficult to detect and difficult to find the source Program tracing: Stepping through the program by hand or with a trace tool Debugger: Program that allows the interruption of a running program to determine values of its variables at any point C++ for Engineers and Scientists, Third Edition 42

Common Programming Errors Failure to declare or initialize variables before use Failure to include the preprocessor statement when using a C++ preprogrammed library Passing the incorrect number or type of arguments to a function Applying increment or decrement operator to an expression instead of an individual variable C++ for Engineers and Scientists, Third Edition 43

Common Programming Errors (continued) Failure to separate all variables passed to cin with the extraction symbol >> Failure to test thoroughly Compiler-dependent evaluation when increment or decrement operators are used with variables that appear more than once in the same expression C++ for Engineers and Scientists, Third Edition 44

Case Study The number of bacteria, B, in a culture that's subject to refrigeration can be appromimated by this formula: B = 300000*e^(-0.032*t) e is Euler's number 2.71828 t is the time in hours the culture has been refrigerated Use the above formula write a program that determine the number of bacteria (in 4 precisions) in the culture after N hours, which N is from input C++ for Engineers and Scientists, Third Edition 45