SlideShare a Scribd company logo
A First Book of C++A First Book of C++
Chapter 3 (Pt 2)Chapter 3 (Pt 2)
Assignment and Interactive InputAssignment and Interactive Input
∗ In this chapter, you will learn about:
∗ Assignment Operators
∗ Formatted Output
∗ Mathematical Library Functions
∗ Interactive Keyboard Input
∗ Symbolic Constraints
∗ Common Programming Errors
∗ Errors, Testing, and Debugging
A First Book of C++ 4th Edition 2
ObjectivesObjectives
∗ Standard preprogrammed functions that can be
included in a program
∗ Example: sqrt(number) calculates the square root of
number
∗ Table 3.5 lists more commonly used mathematical
functions provided in C++
∗ To access these functions in a program, the header file
cmath must be used
∗ Format: #include <cmath> - no semicolon
A First Book of C++ 4th Edition 3
Mathematical Library FunctionsMathematical Library Functions
Mathematical Library FunctionsMathematical Library Functions
(cont'd.)(cont'd.)
A First Book of C++ 4th Edition 4
∗ Before using a C++ mathematical function, the
programmer must know:
∗ Name of the desired mathematical function
∗ What the function does
∗ Type of data required by the function
∗ Data type of the result returned by the function
A First Book of C++ 4th Edition 5
Mathematical Library FunctionsMathematical Library Functions
(cont'd.)(cont'd.)
Mathematical Library FunctionsMathematical Library Functions
(cont'd.)(cont'd.)
A First Book of C++ 4th Edition 6
Mathematical Library FunctionsMathematical Library Functions
(cont'd.)(cont'd.)
A First Book of C++ 4th Edition 7
log base 2
e-3.2
log base 10
e ~ 2.718
Mathematical Library FunctionsMathematical Library Functions
(cont'd.)(cont'd.)
A First Book of C++ 4th Edition 8
∗ Cast: forces conversion of a value to another type
∗ Two versions: compile-time and runtime
∗ Compile-time cast: unary operator
∗ Syntax: dataType (expression)
∗ Example : int(23.45) or (int)23.45
∗ expression converted to data type of dataType
∗ Run-time cast: requested conversion checked at runtime,
applied if valid
∗ Syntax: static_cast<dataType> (expression)
∗ Example : static_cast<int>(23.45)
∗ expression converted to data type dataType
A First Book of C++ 4th Edition 9
CastsCasts
∗ If a program only executes once, data can be included
directly in the program
∗ If data changes, program must be rewritten
∗ Capability needed to enter different data
∗ cin object: used to enter data while a program is
executing
∗ Example: cin >> num1;
∗ Statement stops program execution and accepts data
from the keyboard
A First Book of C++ 4th Edition 10
Interactive Keyboard InputInteractive Keyboard Input
Interactive Keyboard Input (cont'd.)Interactive Keyboard Input (cont'd.)
A First Book of C++ 4th Edition 11
//prompt user to enter input
∗ First cout statement in Program 3.12 prints a string
∗ Tells the person at the terminal what to type
∗ A string used in this manner is called a prompt
∗ Next statement, cin, pauses computer
∗ Waits for user to type a value
∗ User signals the end of data entry by pressing Enter key
∗ Entered value stored in variable to right of extraction symbol
∗ Computer comes out of pause and goes to next cout
statement
A First Book of C++ 4th Edition 12
Interactive Keyboard Input (cont'd.)Interactive Keyboard Input (cont'd.)
∗ A well-constructed program should validate all user
input
∗ Ensures that program does not crash or produce
nonsensical output
∗ Robust programs: programs that detect and respond
effectively to unexpected user input
∗ Also known as “bulletproof” programs
∗ User-input validation: checking entered data and
providing user with a way to reenter invalid data
A First Book of C++ 4th Edition 13
User-Input ValidationUser-Input Validation
Magic numbers: literal data used in a program
Some have general meaning in context of program
Tax rate in a program to calculate taxes
Others have general meaning beyond the context of
the program
π = 3.1416; Euler’s number (e) = 2.71828
Constants can be assigned symbolic names
const float PI = 3.1416;
const double SALESTAX = 0.05;
A First Book of C++ 4th Edition 14
Symbolic ConstantsSymbolic Constants
const: qualifier specifies that the declared identifier
cannot be changed
A const identifier can be used in any C++ statement
in place of number it represents
circum = 2 * PI * radius;
amount = SALESTAX * purchase;
const identifiers commonly referred to as:
Symbolic constants
Named constants
A First Book of C++ 4th Edition 15
Symbolic Constants (cont'd.)Symbolic Constants (cont'd.)
∗ A variable or symbolic constant must be declared
before it is used
∗ C++ permits preprocessor directives/commands and
declaration statements to be placed anywhere in
program
∗ Doing so results in very poor program structure
A First Book of C++ 4th Edition 16
Placement of Statements
∗ As a matter of good programming practice, the order of
statements should be:
preprocessor directives
int main()
{
// symbolic constants
// variable declarations
// other executable statements
return 0;
}
A First Book of C++ 4th Edition 17
Placement of Statements (cont'd.)Placement of Statements (cont'd.)
Placement of Statements (cont'd.)Placement of Statements (cont'd.)
A First Book of C++ 4th Edition 18
∗ Forgetting to assign or initialize values for all variables
before they are used in an expression
∗ Using a mathematical library function without
including the preprocessor statement #include
<cmath>
∗ Using a library function without providing the correct
number of arguments of the proper data type
∗ Applying increment or decrement operator to an
expression
A First Book of C++ 4th Edition 19
Common Programming ErrorsCommon Programming Errors
∗ Forgetting to use the extraction operator, >>, to
separate variables in a cin statement
∗ Using an increment or decrement operator with
variables that appear more than once in the same
statement
∗ Being unwilling to test a program in depth
A First Book of C++ 4th Edition 20
Common Programming ErrorsCommon Programming Errors
(cont'd.)(cont'd.)
∗ Expression: sequence of operands separated by
operators
∗ Expressions are evaluated according to precedence
and associativity of its operands
∗ The assignment symbol, =, is an operator
∗ Assigns a value to variable
∗ Multiple assignments allowed in one statement
∗ Increment operator(++): adds 1 to a variable
∗ Decrement operator(--): subtracts 1 from a variable
A First Book of C++ 4th Edition 21
SummarySummary
∗ Increment and decrement operators can be used as
prefixes or postfixes
∗ C++ provides library functions for various
mathematical functions
∗ These functions operate on their arguments to calculate
a single value
∗ Arguments, separated by commas, included within
parentheses following function’s name
∗ Functions may be included within larger expressions
A First Book of C++ 4th Edition 22
Summary (cont'd.)Summary (cont'd.)
∗ cin object used for data input
∗ cin temporarily suspends statement execution until
data entered for variables in cin function
∗ Good programming practice: prior to a cin
statement, display message alerting user to type and
number of data items to be entered
∗ Message called a prompt
∗ Values can be equated to a single constant by using
the const keyword
A First Book of C++ 4th Edition 23
Summary (cont'd.)Summary (cont'd.)
∗ Program errors can be detected:
∗ Before a program is compiled
∗ While the program is being compiled
∗ While the program is running
∗ After the program has been run and the output is being
examined
∗ Desk checking
∗ Method for detecting errors before a program is compiled
∗ Program verification and testing
A First Book of C++ 4th Edition 24
Chapter Supplement: Errors, Testing,Chapter Supplement: Errors, Testing,
and Debuggingand Debugging
∗ Compile-time errors
∗ Errors detected while a program is being compiled
∗ No one but the programmer ever knows they occurred
∗ Runtime errors
∗ Errors that occur while a program is running
∗ More troubling because they occur while a user is
running the program
∗ Can be caused by program or hardware failures
A First Book of C++ 4th Edition 25
Compile-Time and Runtime ErrorsCompile-Time and Runtime Errors
∗ Syntax error
∗ Error in ordering valid language elements in a statement
or the attempt to use invalid language elements
∗ Logic error
∗ Characterized by erroneous, unexpected, or
unintentional output that’s a result of some flaw in the
program’s logic
A First Book of C++ 4th Edition 26
Syntax and Logic ErrorsSyntax and Logic Errors
∗ Program testing should be well thought out to maximize
the possibility of locating errors
∗ Bug: a program error
∗ Debugging
∗ Process of isolating and correcting the error and verifying the
correction
∗ Program tracing
∗ Process of imitating the computer by executing each
statement by hand as the computer would
∗ Echo printing
A First Book of C++ 4th Edition 27
Testing and DebuggingTesting and Debugging

More Related Content

What's hot

Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
Edgar Barbosa
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
MLG College of Learning, Inc
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
MLG College of Learning, Inc
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
JavaTpoint.Com
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
temkin abdlkader
 
Lesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving processLesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving process
MLG College of Learning, Inc
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
Sangharsh agarwal
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04
IIUM
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
MLG College of Learning, Inc
 
C LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONSC LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONS
BESTECH SOLUTIONS
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
MLG College of Learning, Inc
 
C++ rajan
C++ rajanC++ rajan
C++ rajan
Deep Rajan
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
Appili Vamsi Krishna
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
ahmed22dg
 
C language
C languageC language
C language
Mohamed Bedair
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
Siva Sathya
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
Bharat Kalia
 
Embedded c
Embedded cEmbedded c
Embedded c
Nandan Desai
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
AAKASH KUMAR
 

What's hot (19)

Control Flow Analysis
Control Flow AnalysisControl Flow Analysis
Control Flow Analysis
 
Lesson 5 .1 selection structure
Lesson 5 .1 selection structureLesson 5 .1 selection structure
Lesson 5 .1 selection structure
 
Lesson 5.2 logical operators
Lesson 5.2 logical operatorsLesson 5.2 logical operators
Lesson 5.2 logical operators
 
C Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpointC Programming Language Tutorial for beginners - JavaTpoint
C Programming Language Tutorial for beginners - JavaTpoint
 
Intro to c++
Intro to c++Intro to c++
Intro to c++
 
Lesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving processLesson 2 beginning the problem solving process
Lesson 2 beginning the problem solving process
 
Introduction Of C++
Introduction Of C++Introduction Of C++
Introduction Of C++
 
Csc1100 lecture04 ch04
Csc1100 lecture04 ch04Csc1100 lecture04 ch04
Csc1100 lecture04 ch04
 
Lesson 3.2 data types for memory location
Lesson 3.2 data types for memory locationLesson 3.2 data types for memory location
Lesson 3.2 data types for memory location
 
C LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONSC LANGUAGE - BESTECH SOLUTIONS
C LANGUAGE - BESTECH SOLUTIONS
 
Lesson 3.1 variables and constant
Lesson 3.1 variables and constantLesson 3.1 variables and constant
Lesson 3.1 variables and constant
 
C++ rajan
C++ rajanC++ rajan
C++ rajan
 
C language for Semester Exams for Engineers
C language for Semester Exams for Engineers C language for Semester Exams for Engineers
C language for Semester Exams for Engineers
 
Review chapter 1 2-3
Review chapter 1 2-3Review chapter 1 2-3
Review chapter 1 2-3
 
C language
C languageC language
C language
 
Principle source of optimazation
Principle source of optimazationPrinciple source of optimazation
Principle source of optimazation
 
Introduction to C++
Introduction to C++ Introduction to C++
Introduction to C++
 
Embedded c
Embedded cEmbedded c
Embedded c
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 

Similar to Csc1100 lecture03 ch03-pt2-s14

Chapter2
Chapter2Chapter2
Chapter2
Anees999
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
Hattori Sidek
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
IIUM
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
IIUM
 
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
Widad Jamaluddin
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
Vikram Nandini
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
kelleyc3
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
20EUEE018DEEPAKM
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
RSathyaPriyaCSEKIOT
 
C language
C language C language
Cpp
CppCpp
C chap02
C chap02C chap02
C chap02
Khan Rahimeen
 
C chap02
C chap02C chap02
C chap02
Kamran
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
LidetAdmassu
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
Widad Jamaluddin
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
sajjad ali khan
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
UNIVERSITY OF ENGINEERING AND TECHNOLOGY TAXILA
 
c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444
PurvaShyama
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
GDGKuwaitGoogleDevel
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
cpjcollege
 

Similar to Csc1100 lecture03 ch03-pt2-s14 (20)

Chapter2
Chapter2Chapter2
Chapter2
 
Ch2 introduction to c
Ch2 introduction to cCh2 introduction to c
Ch2 introduction to c
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
 
Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1Csc1100 lecture01 ch01-pt1
Csc1100 lecture01 ch01-pt1
 
Chap 3 c++
Chap 3 c++Chap 3 c++
Chap 3 c++
 
C Programming Unit-1
C Programming Unit-1C Programming Unit-1
C Programming Unit-1
 
C++ Overview
C++ OverviewC++ Overview
C++ Overview
 
Introduction%20C.pptx
Introduction%20C.pptxIntroduction%20C.pptx
Introduction%20C.pptx
 
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
UNIT-1 notes(Data Types – Variables – Operations – Expressions and Statements...
 
C language
C language C language
C language
 
Cpp
CppCpp
Cpp
 
C chap02
C chap02C chap02
C chap02
 
C chap02
C chap02C chap02
C chap02
 
Fundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programmingFundamental of programming Fundamental of programming
Fundamental of programming Fundamental of programming
 
Chap 2 c++
Chap 2 c++Chap 2 c++
Chap 2 c++
 
C++ programming language basic to advance level
C++ programming language basic to advance levelC++ programming language basic to advance level
C++ programming language basic to advance level
 
C++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWAREC++ AND CATEGORIES OF SOFTWARE
C++ AND CATEGORIES OF SOFTWARE
 
c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444c programming L-1.pdf43333333544444444444444444444
c programming L-1.pdf43333333544444444444444444444
 
#Code2 create c++ for beginners
#Code2 create  c++ for beginners #Code2 create  c++ for beginners
#Code2 create c++ for beginners
 
OOPS using C++
OOPS using C++OOPS using C++
OOPS using C++
 

More from IIUM

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
IIUM
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
IIUM
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
IIUM
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
IIUM
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
IIUM
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
IIUM
 
Group p1
Group p1Group p1
Group p1
IIUM
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
IIUM
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
IIUM
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
IIUM
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
IIUM
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
IIUM
 
Heaps
HeapsHeaps
Heaps
IIUM
 
Report format
Report formatReport format
Report format
IIUM
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
IIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
IIUM
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
IIUM
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
IIUM
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
IIUM
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
IIUM
 

More from IIUM (20)

How to use_000webhost
How to use_000webhostHow to use_000webhost
How to use_000webhost
 
Chapter 2
Chapter 2Chapter 2
Chapter 2
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Kreydle internship-multimedia
Kreydle internship-multimediaKreydle internship-multimedia
Kreydle internship-multimedia
 
03phpbldgblock
03phpbldgblock03phpbldgblock
03phpbldgblock
 
Chap2 practice key
Chap2 practice keyChap2 practice key
Chap2 practice key
 
Group p1
Group p1Group p1
Group p1
 
Tutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seoTutorial import n auto pilot blogspot friendly seo
Tutorial import n auto pilot blogspot friendly seo
 
Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009Visual sceneperception encycloperception-sage-oliva2009
Visual sceneperception encycloperception-sage-oliva2009
 
03 the htm_lforms
03 the htm_lforms03 the htm_lforms
03 the htm_lforms
 
Exercise on algo analysis answer
Exercise on algo analysis   answerExercise on algo analysis   answer
Exercise on algo analysis answer
 
Redo midterm
Redo midtermRedo midterm
Redo midterm
 
Heaps
HeapsHeaps
Heaps
 
Report format
Report formatReport format
Report format
 
Edpuzzle guidelines
Edpuzzle guidelinesEdpuzzle guidelines
Edpuzzle guidelines
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Final Exam Paper
Final Exam PaperFinal Exam Paper
Final Exam Paper
 
Group assignment 1 s21516
Group assignment 1 s21516Group assignment 1 s21516
Group assignment 1 s21516
 
Avl tree-rotations
Avl tree-rotationsAvl tree-rotations
Avl tree-rotations
 
Week12 graph
Week12   graph Week12   graph
Week12 graph
 

Recently uploaded

ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
dot55audits
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
iammrhaywood
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
Nguyen Thanh Tu Collection
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
GeorgeMilliken2
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
Celine George
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
haiqairshad
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
WaniBasim
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
Himanshu Rai
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
HajraNaeem15
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
Priyankaranawat4
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
MysoreMuleSoftMeetup
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Denish Jangid
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
Colégio Santa Teresinha
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
eBook.com.bd (প্রয়োজনীয় বাংলা বই)
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
heathfieldcps1
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
PsychoTech Services
 

Recently uploaded (20)

ZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptxZK on Polkadot zero knowledge proofs - sub0.pptx
ZK on Polkadot zero knowledge proofs - sub0.pptx
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptxNEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
NEWSPAPERS - QUESTION 1 - REVISION POWERPOINT.pptx
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
BÀI TẬP BỔ TRỢ TIẾNG ANH 8 CẢ NĂM - GLOBAL SUCCESS - NĂM HỌC 2023-2024 (CÓ FI...
 
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
What is Digital Literacy? A guest blog from Andy McLaughlin, University of Ab...
 
How to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 InventoryHow to Setup Warehouse & Location in Odoo 17 Inventory
How to Setup Warehouse & Location in Odoo 17 Inventory
 
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skillsspot a liar (Haiqa 146).pptx Technical writhing and presentation skills
spot a liar (Haiqa 146).pptx Technical writhing and presentation skills
 
Liberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdfLiberal Approach to the Study of Indian Politics.pdf
Liberal Approach to the Study of Indian Politics.pdf
 
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem studentsRHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
RHEOLOGY Physical pharmaceutics-II notes for B.pharm 4th sem students
 
How to deliver Powerpoint Presentations.pptx
How to deliver Powerpoint  Presentations.pptxHow to deliver Powerpoint  Presentations.pptx
How to deliver Powerpoint Presentations.pptx
 
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdfANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
ANATOMY AND BIOMECHANICS OF HIP JOINT.pdf
 
Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47Mule event processing models | MuleSoft Mysore Meetup #47
Mule event processing models | MuleSoft Mysore Meetup #47
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
Chapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptxChapter wise All Notes of First year Basic Civil Engineering.pptx
Chapter wise All Notes of First year Basic Civil Engineering.pptx
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
MARY JANE WILSON, A “BOA MÃE” .
MARY JANE WILSON, A “BOA MÃE”           .MARY JANE WILSON, A “BOA MÃE”           .
MARY JANE WILSON, A “BOA MÃE” .
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdfবাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
বাংলাদেশ অর্থনৈতিক সমীক্ষা (Economic Review) ২০২৪ UJS App.pdf
 
The basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptxThe basics of sentences session 6pptx.pptx
The basics of sentences session 6pptx.pptx
 
Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...Gender and Mental Health - Counselling and Family Therapy Applications and In...
Gender and Mental Health - Counselling and Family Therapy Applications and In...
 

Csc1100 lecture03 ch03-pt2-s14

  • 1. A First Book of C++A First Book of C++ Chapter 3 (Pt 2)Chapter 3 (Pt 2) Assignment and Interactive InputAssignment and Interactive Input
  • 2. ∗ In this chapter, you will learn about: ∗ Assignment Operators ∗ Formatted Output ∗ Mathematical Library Functions ∗ Interactive Keyboard Input ∗ Symbolic Constraints ∗ Common Programming Errors ∗ Errors, Testing, and Debugging A First Book of C++ 4th Edition 2 ObjectivesObjectives
  • 3. ∗ Standard preprogrammed functions that can be included in a program ∗ Example: sqrt(number) calculates the square root of number ∗ Table 3.5 lists more commonly used mathematical functions provided in C++ ∗ To access these functions in a program, the header file cmath must be used ∗ Format: #include <cmath> - no semicolon A First Book of C++ 4th Edition 3 Mathematical Library FunctionsMathematical Library Functions
  • 4. Mathematical Library FunctionsMathematical Library Functions (cont'd.)(cont'd.) A First Book of C++ 4th Edition 4
  • 5. ∗ Before using a C++ mathematical function, the programmer must know: ∗ Name of the desired mathematical function ∗ What the function does ∗ Type of data required by the function ∗ Data type of the result returned by the function A First Book of C++ 4th Edition 5 Mathematical Library FunctionsMathematical Library Functions (cont'd.)(cont'd.)
  • 6. Mathematical Library FunctionsMathematical Library Functions (cont'd.)(cont'd.) A First Book of C++ 4th Edition 6
  • 7. Mathematical Library FunctionsMathematical Library Functions (cont'd.)(cont'd.) A First Book of C++ 4th Edition 7 log base 2 e-3.2 log base 10 e ~ 2.718
  • 8. Mathematical Library FunctionsMathematical Library Functions (cont'd.)(cont'd.) A First Book of C++ 4th Edition 8
  • 9. ∗ Cast: forces conversion of a value to another type ∗ Two versions: compile-time and runtime ∗ Compile-time cast: unary operator ∗ Syntax: dataType (expression) ∗ Example : int(23.45) or (int)23.45 ∗ expression converted to data type of dataType ∗ Run-time cast: requested conversion checked at runtime, applied if valid ∗ Syntax: static_cast<dataType> (expression) ∗ Example : static_cast<int>(23.45) ∗ expression converted to data type dataType A First Book of C++ 4th Edition 9 CastsCasts
  • 10. ∗ If a program only executes once, data can be included directly in the program ∗ If data changes, program must be rewritten ∗ Capability needed to enter different data ∗ cin object: used to enter data while a program is executing ∗ Example: cin >> num1; ∗ Statement stops program execution and accepts data from the keyboard A First Book of C++ 4th Edition 10 Interactive Keyboard InputInteractive Keyboard Input
  • 11. Interactive Keyboard Input (cont'd.)Interactive Keyboard Input (cont'd.) A First Book of C++ 4th Edition 11 //prompt user to enter input
  • 12. ∗ First cout statement in Program 3.12 prints a string ∗ Tells the person at the terminal what to type ∗ A string used in this manner is called a prompt ∗ Next statement, cin, pauses computer ∗ Waits for user to type a value ∗ User signals the end of data entry by pressing Enter key ∗ Entered value stored in variable to right of extraction symbol ∗ Computer comes out of pause and goes to next cout statement A First Book of C++ 4th Edition 12 Interactive Keyboard Input (cont'd.)Interactive Keyboard Input (cont'd.)
  • 13. ∗ A well-constructed program should validate all user input ∗ Ensures that program does not crash or produce nonsensical output ∗ Robust programs: programs that detect and respond effectively to unexpected user input ∗ Also known as “bulletproof” programs ∗ User-input validation: checking entered data and providing user with a way to reenter invalid data A First Book of C++ 4th Edition 13 User-Input ValidationUser-Input Validation
  • 14. Magic numbers: literal data used in a program Some have general meaning in context of program Tax rate in a program to calculate taxes Others have general meaning beyond the context of the program π = 3.1416; Euler’s number (e) = 2.71828 Constants can be assigned symbolic names const float PI = 3.1416; const double SALESTAX = 0.05; A First Book of C++ 4th Edition 14 Symbolic ConstantsSymbolic Constants
  • 15. const: qualifier specifies that the declared identifier cannot be changed A const identifier can be used in any C++ statement in place of number it represents circum = 2 * PI * radius; amount = SALESTAX * purchase; const identifiers commonly referred to as: Symbolic constants Named constants A First Book of C++ 4th Edition 15 Symbolic Constants (cont'd.)Symbolic Constants (cont'd.)
  • 16. ∗ A variable or symbolic constant must be declared before it is used ∗ C++ permits preprocessor directives/commands and declaration statements to be placed anywhere in program ∗ Doing so results in very poor program structure A First Book of C++ 4th Edition 16 Placement of Statements
  • 17. ∗ As a matter of good programming practice, the order of statements should be: preprocessor directives int main() { // symbolic constants // variable declarations // other executable statements return 0; } A First Book of C++ 4th Edition 17 Placement of Statements (cont'd.)Placement of Statements (cont'd.)
  • 18. Placement of Statements (cont'd.)Placement of Statements (cont'd.) A First Book of C++ 4th Edition 18
  • 19. ∗ Forgetting to assign or initialize values for all variables before they are used in an expression ∗ Using a mathematical library function without including the preprocessor statement #include <cmath> ∗ Using a library function without providing the correct number of arguments of the proper data type ∗ Applying increment or decrement operator to an expression A First Book of C++ 4th Edition 19 Common Programming ErrorsCommon Programming Errors
  • 20. ∗ Forgetting to use the extraction operator, >>, to separate variables in a cin statement ∗ Using an increment or decrement operator with variables that appear more than once in the same statement ∗ Being unwilling to test a program in depth A First Book of C++ 4th Edition 20 Common Programming ErrorsCommon Programming Errors (cont'd.)(cont'd.)
  • 21. ∗ Expression: sequence of operands separated by operators ∗ Expressions are evaluated according to precedence and associativity of its operands ∗ The assignment symbol, =, is an operator ∗ Assigns a value to variable ∗ Multiple assignments allowed in one statement ∗ Increment operator(++): adds 1 to a variable ∗ Decrement operator(--): subtracts 1 from a variable A First Book of C++ 4th Edition 21 SummarySummary
  • 22. ∗ Increment and decrement operators can be used as prefixes or postfixes ∗ C++ provides library functions for various mathematical functions ∗ These functions operate on their arguments to calculate a single value ∗ Arguments, separated by commas, included within parentheses following function’s name ∗ Functions may be included within larger expressions A First Book of C++ 4th Edition 22 Summary (cont'd.)Summary (cont'd.)
  • 23. ∗ cin object used for data input ∗ cin temporarily suspends statement execution until data entered for variables in cin function ∗ Good programming practice: prior to a cin statement, display message alerting user to type and number of data items to be entered ∗ Message called a prompt ∗ Values can be equated to a single constant by using the const keyword A First Book of C++ 4th Edition 23 Summary (cont'd.)Summary (cont'd.)
  • 24. ∗ Program errors can be detected: ∗ Before a program is compiled ∗ While the program is being compiled ∗ While the program is running ∗ After the program has been run and the output is being examined ∗ Desk checking ∗ Method for detecting errors before a program is compiled ∗ Program verification and testing A First Book of C++ 4th Edition 24 Chapter Supplement: Errors, Testing,Chapter Supplement: Errors, Testing, and Debuggingand Debugging
  • 25. ∗ Compile-time errors ∗ Errors detected while a program is being compiled ∗ No one but the programmer ever knows they occurred ∗ Runtime errors ∗ Errors that occur while a program is running ∗ More troubling because they occur while a user is running the program ∗ Can be caused by program or hardware failures A First Book of C++ 4th Edition 25 Compile-Time and Runtime ErrorsCompile-Time and Runtime Errors
  • 26. ∗ Syntax error ∗ Error in ordering valid language elements in a statement or the attempt to use invalid language elements ∗ Logic error ∗ Characterized by erroneous, unexpected, or unintentional output that’s a result of some flaw in the program’s logic A First Book of C++ 4th Edition 26 Syntax and Logic ErrorsSyntax and Logic Errors
  • 27. ∗ Program testing should be well thought out to maximize the possibility of locating errors ∗ Bug: a program error ∗ Debugging ∗ Process of isolating and correcting the error and verifying the correction ∗ Program tracing ∗ Process of imitating the computer by executing each statement by hand as the computer would ∗ Echo printing A First Book of C++ 4th Edition 27 Testing and DebuggingTesting and Debugging