Download presentation
Presentation is loading. Please wait.
Published byNathaniel Roberts Modified over 9 years ago
1
Arithmetic Operations (L05) * Arithmetic Operations * Variables * Declaration Statement * Software Development Procedure Problem Solving Using C Dr. Ming Zhang
2
Arithmetic Operations Operation Operator Type Associativity Addition+ BinaryLeft to right Subtraction- BinaryLeft to right Multiplication* BinaryLeft to right Division/ BinaryLeft to right Modulus % BinaryLeft to right Negation- UnaryRight to Left Problem Solving Using C Dr. Ming Zhang
3
Integer Division * Integer Division Rule The fractional part of the result obtained when dividing two integers is dropped (truncated). Example 15/2 = 7 (15/2 =7.5 is not correct) * Integer Modulus Operator Modulus operator captures the remainder when two integers are divided. Example 15%2=1 (15%/2 = 7.5 is not correct) Problem Solving Using C Dr. Ming Zhang
4
Operations of Characters and Integers * Operations of Characters and Integers C++ always convert a character to an equivalent integer value whenever a character is used in and arithmetic expression. Thus, characters and integers can be freely mixed in arithmetic expressions. * Examples (ASCII) ‘A’+1 = ‘B’ ( 65 + 1 = 66; 65=‘A’, 66 =‘B’) ‘Z’ - 1= ‘Y’ ( 90-1 = 89; 90 =‘Z’, 89=‘Y’) ‘A’ + ‘B’ - 9 = 131-9 = 122=‘z’ (122 = ‘z’) Problem Solving Using C Dr. Ming Zhang
5
Exercise/Homework 1 * ‘B’-1 = ‘?’ ( 65=‘A’, 66 =‘B’, 66 =‘C’) ‘Y’ + 1= ‘?’ ( 90 =‘Z’, 89=‘Y’, 88=‘X’) ‘A’ + ‘B’ - 10 = ‘?’ (122 = ‘z’, 121=‘y’, 120=‘x’) * 19/6 = ? 121/10 = ? 19%6 = ? 121%6 = ? Problem Solving Using C Dr. Ming Zhang
6
Variables * All integer, float-point, and other values used in the program are stored and retrieved from the computer’s memory unit. *In high-level language, symbolic names are used in place of actual memory addresses. These symbolic names are called variables. * Examples of variables: nm1, nm2, total, date, class_name, First_name Problem Solving Using C Dr. Ming Zhang
7
Rules of Variables * The variable name must begin with a letter or underscore (_), and may contain only letters, underscores, or digits. It cannot contain any blanks, commas, or special symbols, such as ( ) &, $ #. ! \ ?. * A variable name cannot be a keyword. * The variable name cannot consist of more than 31 characters. * Invalid variable names ?name, #date, while, $34, data.name, data(name) Problem Solving Using C Dr. Ming Zhang
8
Naming Storage Locations num1 = 45; num2 = 12; total = num1 + num2; Variable names num1 num2 total 451257 Memory Addressed 1652 2548 3002 Problem Solving Using C Dr. Ming Zhang
9
Declaration Statements *Declaration Statement The statement for naming a variable and specifying the data type that can be stored in it are called declaration statement. * General Form of Declaration Statements data-typevariable-name; data-type: designates a valid C++ data type. Variable-name: is a user-selected variable name. * Examples int total;long int total, float firstnum; double secnum; Problem Solving Using C Dr. Ming Zhang
10
Multiple Declarations * Multiple Declaration Variables having the same data type can always be grouped together and declared using a single declaration statement - multiple declaration. * Example float grade1; float grade2; float total; float average; float grade1, grade2, total, average; Problem Solving Using C Dr. Ming Zhang
11
Equality and Relational Operators Operators Examples Meaning > x>y x is greater than y < x<y x is less than y >= x>=y x is greater than or equal to y <= x<=y x is less than or equal to y == x == y x is equal to y != x != y x is not equal y Problem Solving Using C Dr. Ming Zhang
12
Example of Equality & Operational Operators Fig1.14 * A comma-separated list. int num1, num2; * Cascaded stream extraction operations cin >> num1 >> num2; * If statement to compare two numbers input by the user. if ( num1 == num2) cout << num1 << “ is equal to “ << num2 << endl; Problem Solving Using C Dr. Ming Zhang
13
Home Work Using if statements, relational operators, and equality operators to tell us the relationships two input numbers satisfy. Problem Solving Using C Dr. Ming Zhang
14
Software Development Procedure * Step 1: Analyze the problem * Step 2: Develop a Solution * Step 3: Code the Solution into C/C++ * Step 4: Test and Correct the Program Problem Solving Using C Dr. Ming Zhang
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.