Document Details

DesirableBoron

Uploaded by DesirableBoron

Notre Dame of Marbel University

2018

Tags

C++ programming program design computer science

Full Transcript

Chapter 2 Basic Elements of C++ C++ Programming: Program Design Including Data Structures, Eighth Edition © 2018 Cenga...

Chapter 2 Basic Elements of C++ C++ Programming: Program Design Including Data Structures, Eighth Edition © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain 1 product or service or otherwise on a password-protected website for classroom Objectives (1 of 3) In this chapter, you will: Become familiar with the basic components of a C++ program, including functions, special symbols, and identifiers Explore simple data types Discover how to use arithmetic operators Examine how a program evaluates arithmetic expressions Become familiar with the string data type Learn what an assignment statement is and what it does 2 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 2 or otherwise on a password-protected website for classroom Objectives (2 of 3) Learn about variable declaration Discover how to input data into memory using input statements Become familiar with the use of increment and decrement operators Examine ways to output results using output statements Learn how to use preprocessor directives and why they are necessary 3 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 3 or otherwise on a password-protected website for classroom Objectives (3 of 3) Learn how to debug syntax errors Explore how to properly structure a program, including using comments to document a program Become familiar with compound statements Learn how to write a C++ program 4 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 4 or otherwise on a password-protected website for classroom Introduction Computer program A sequence of statements whose objective is to accomplish a task Programming The process of planning and creating a program Real-world analogy: a recipe for cooking 5 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 5 or otherwise on a password-protected website for classroom A Quick Look at a C++ Program (1 of 5) 6 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 6 or otherwise on a password-protected website for classroom A Quick Look at a C++ Program (2 of 5) Sample Run: 7 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 7 or otherwise on a password-protected website for classroom A Quick Look at a C++ Program (3 of 5) FIGURE 2-1 Various parts of a C++ program 8 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 8 or otherwise on a password-protected website for classroom A Quick Look at a C++ Program (4 of 5) FIGURE 2-1 Various parts of a C++ program (cont’d.) 9 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 9 or otherwise on a password-protected website for classroom A Quick Look at a C++ Program (5 of 5) Variable: a memory location whose contents can be changed FIGURE 2-3 Memory allocation FIGURE 2-4 Memory spaces after the statement length = 6.0; executes 10 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 10 or otherwise on a password-protected website for classroom The Basics of a C++ Program Subprogram (or function): collection of statements When executed, accomplishes something May be predefined or standard Syntax rules: rules that specify which statements (instructions) are legal or valid Semantic rules: determine the meaning of the instructions Programming language: a set of rules, symbols, and special words 11 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 11 or otherwise on a password-protected website for classroom Comments Comments are for the reader, not the compiler Two types Single line: begins with // / 12 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 12 or otherwise on a password-protected website for classroom Special Symbols A token is the smallest individual unit of a program written in any language C++ tokens include special symbols, word symbols, and identifiers Special symbols in C++ include: + - * /. ; ? , = 13 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 13 or otherwise on a password-protected website for classroom Reserved Words (Keywords) Reserved word symbols (or keywords): Cannot be redefined within a program Cannot be used for anything other than their intended use Examples include: int float double char const void return 14 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 14 or otherwise on a password-protected website for classroom Identifiers (1 of 2) An identifier is the name of something that appears in a program Consists of letters, digits, and the underscore character (_) Must begin with a letter or underscore C++ is case sensitive NUMBER is not the same as number Two predefined identifiers are cout and cin Unlike reserved words, predefined identifiers may be redefined, but it is not a good idea 15 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 15 or otherwise on a password-protected website for classroom Identifiers (2 of 2) Legal identifiers in C++ first conversion payRate TABLE 2-1 Examples of Illegal Identifiers Illegal Identifier Reason A Correct Identifier There can be no space between employee Salary employeeSalary employee and Salary. The exclamation mark cannot be Hello! Hello used in an identifier. The symbol + cannot be used in an one+two onePlusTwo identifier. An identifier cannot begin with a 2nd second digit. 16 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 16 or otherwise on a password-protected website for classroom Whitespaces Every C++ program contains whitespaces Include blanks, tabs, and newline characters Whitespaces separate special symbols, reserved words, and identifiers Proper utilization of whitespaces is important Can be used to make the program more readable 17 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 17 or otherwise on a password-protected website for classroom Data Types A data type is set of values together with a set of allowed operations C++ data types fall into three categories: Simple data type Structured data type Pointers 18 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 18 or otherwise on a password-protected website for classroom Simple Data Types (1 of 2) Three categories of simple data Integral: integers (numbers without a decimal) - Can be further categorized: char, short, int, long, bool, unsigned char, unsigned short, unsigned int, unsigned long Floating-point: decimal numbers Enumeration: a user-defined data type 19 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 19 or otherwise on a password-protected website for classroom Simple Data Types (2 of 2) TABLE 2-2 Values and Memory Allocation for Simple Data Types Data Type Values Storage (in bytes) int –147483648 (= –231 ) to 2147483647 (= 231 – 1) 4 bool true and false 1 char –128 (= –27 ) to 127 (= 27 – 1) 1 –9223372036854775808 (–263 ) to long long 64 9223372036854775807(263 – 1) Different compilers may allow different ranges of values 20 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 20 or otherwise on a password-protected website for classroom int Data Type Examples -6728 0 78 +763 Positive integers do not require a + sign A comma cannot be used within an integer Commas are only used for separating items in a list 21 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 21 or otherwise on a password-protected website for classroom bool Data Type bool type Two values: true and false Purpose: to manipulate logical (Boolean) expressions true and false Logical values bool, true, and false Reserved words 22 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 22 or otherwise on a password-protected website for classroom char Data Type (1 of 2) Data type char is the smallest integral data type It is used for single characters: letters, digits, and special symbols Each character is enclosed in single quotes 'A', 'a', '0', '*', '+', '$', '&' A blank space is a character Written ' ', with a space left between the single quotes 23 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 23 or otherwise on a password-protected website for classroom char Data Type (2 of 2) Different character data sets exist ASCII: American Standard Code for Information Interchange Each of 128 values in ASCII code set represents a different character Characters have a predefined ordering based on the ASCII numeric value Collating sequence: ordering of characters based on the character set code 24 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 24 or otherwise on a password-protected website for classroom Floating-Point Data Types (1 of 3) C++ uses scientific notation to represent real numbers (floating-point notation) TABLE 2-3 Examples of Decimal Numbers in Scientific and C11 Floating-Point Notations Decimal Number Scientific Notation C++ Floating-Point Notation 75.924 7.5924 * 101 7.592400E1 0.18 1.8 * 10-1 1.800000E-1 0.0000453 4.53 * 10-5 4.530000E-5 –1.482 -1.482 * 100 -1.482000E0 7800.0 7.8 * 103 7.800000E3 25 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 25 or otherwise on a password-protected website for classroom Floating-Point Data Types (2 of 3) float: represents any real number Range: -3.4 * 1038 to 3.4 * 1038 (four bytes) double: represents any real number Range: –1.7 * 10308 to 1.7 * 10308 (eight bytes) Minimum and maximum values of data types are system dependent 26 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 26 or otherwise on a password-protected website for classroom Floating-Point Data Types (3 of 3) Maximum number of significant digits (decimal places) for float values: 6 or 7 Maximum number of significant digits for double: 15 Precision: maximum number of significant digits float values are called single precision double values are called double precision 27 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 27 or otherwise on a password-protected website for classroom Data Types, Variables, and Assignment Statements To declare a variable, must specify its data type Syntax rule to declare a variable is: dataType identifier; Examples include: int counter; double interestRate; char grade; Assignment statement has the form: variable = expression Example: interestRate = 0.05; 28 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 28 or otherwise on a password-protected website for classroom Arithmetic Operators, Operator Precedence, and Expressions (1 of 2) C++ arithmetic operators include: + addition - subtraction (or negation) * multiplication / division % mod (modulus or remainder) +, -, *, and / can be used with integral and floating-point data types Modulus (%) can only be used with integral data types 29 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 29 or otherwise on a password-protected website for classroom Arithmetic Operators, Operator Precedence, and Expressions (2 of 2) When you use / with integral data types, the integral result is truncated (no rounding) Arithmetic expressions contain values and arithmetic operators Operands are the numbers appearing in the expressions Operators can be unary (one operand) or binary (two operands) 30 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 30 or otherwise on a password-protected website for classroom Order of Precedence All operations inside () are evaluated first *, /, and % are at the same level of precedence and are evaluated next + and - have the same level of precedence and are evaluated last When operators are on the same level Operations are performed from left to right (associativity) 3 * 7 - 6 + 2 * 5 / 4 + 6 means (((3 * 7) – 6) + ((2 * 5) / 4 )) + 6 31 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 31 or otherwise on a password-protected website for classroom Expressions Integral expression: all operands are integers Yields an integral result Example: 2 + 3 * 5 Floating-point (decimal) expression: all operands are floating-point Yields a floating-point result Example: 12.8 * 17.5 - 34.50 32 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 32 or otherwise on a password-protected website for classroom Mixed Expressions (1 of 2) Mixed expression Has operands of different data types Contains integers and floating-point Examples of mixed expressions 2 + 3.5 6 / 4 + 3.9 5.4 * 2 – 13.6 + 18 / 2 33 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 33 or otherwise on a password-protected website for classroom Mixed Expressions (2 of 2) Evaluation rules If operator has same types of operands - The operator is evaluated according to the type of the operands If operator has both types of operands - Integer is changed to floating-point - Operator is evaluated - Result is floating-point Entire expression is evaluated according to precedence rules 34 © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 34 or otherwise on a password-protected website for classroom Example: Write a C++ program that prompts the user to input the elapsed time for an event in seconds. The program then outputs the elapsed time in hours, minutes, and seconds. ( For example, if the elapsed time is 9,630 seconds, then the output is 2:40:30) Algorithm: -Input the time in seconds, -convert the given time in seconds to hours:minutes:seconds using the modulus operator -display the result. © 2018 Cengage Learning. All Rights Reserved. May not be copied, scanned, or duplicated, in whole or in part, except for use as permitted in a license distributed with a certain product or service 35 or otherwise on a password-protected website for classroom Source Code #include using namespace std; main() { int sec,HH,MM,SS,R; //HH as hour, MM for minutes, and SS for seconds , then R for remainder. cout>sec; HH = sec/3600; R = sec%3600; MM = R/60; SS= R%60; cout

Use Quizgecko on...
Browser
Browser