Csc1100 lecture03 ch03-pt1-s14
Transcript of Csc1100 lecture03 ch03-pt1-s14
![Page 1: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/1.jpg)
A First Book of C++A First Book of C++
Chapter 3 (Pt 1)Chapter 3 (Pt 1)Assignment and Interactive InputAssignment and Interactive Input
![Page 2: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/2.jpg)
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
![Page 3: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/3.jpg)
Basic assignment operator Format: variable = expression; Computes value of expression on right of = sign,
assigns it to variable on left side of = sign If not initialized in a declaration statement,
a variable should be assigned a value before used in any computation
Variables can only store one value at a time Subsequent assignment statements will overwrite
previously assigned values
A First Book of C++ 4th Edition 3
Assignment OperatorsAssignment Operators
![Page 4: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/4.jpg)
Operand to right of = sign can be: A constant A variable A valid C++ expression
Operand to left of = sign must be a variable If operand on right side is an expression:
All variables in expression must have a value to get a valid result from the assignment
A First Book of C++ 4th Edition 4
Assignment Operators Assignment Operators (cont'd.)(cont'd.)
![Page 5: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/5.jpg)
Expression: combination of constants and variables that can be evaluated Examples:
sum = 3 + 7; diff = 15 –6; product = .05 * 14.6; tally = count + 1; newtotal = 18.3 + total; average = sum / items; slope = (y2 – y1) / (x2 – x1);
A First Book of C++ 4th Edition 5
Assignment Operators Assignment Operators (cont'd.)(cont'd.)
variable
constant
![Page 6: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/6.jpg)
Assignment Operators Assignment Operators (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 6
constantexpression
![Page 7: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/7.jpg)
Value on right side of a C++ expression is converted to data type of variable on the left side
Example:If temp is an integer variable (int temp), the
assignmenttemp = 25.89;
causes integer value 25 to be stored in integer variable temp
A First Book of C++ 4th Edition 7
CoercionCoercion
![Page 8: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/8.jpg)
A First Book of C++ 4th Edition 8
CoercionCoercion
Program
Output
CompilationWarning
![Page 9: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/9.jpg)
sum = sum + 10; is a valid C++ expression The value of sum + 10 is stored in variable sum
Not a valid algebra equation (in formal Mathematics)
A First Book of C++ 4th Edition 9
Assignment VariationsAssignment Variations
![Page 10: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/10.jpg)
Assignment Variations Assignment Variations (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 10
// Initialization value for use in accumulation
// Accumulation
![Page 11: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/11.jpg)
A First Book of C++ 4th Edition 11
Assignment Variations Assignment Variations (cont'd.)(cont'd.)
![Page 12: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/12.jpg)
Assignment expressions such as: sum = sum + 25;
can be written by using the following shortcut operators:+= -= *= /= %=
Example:sum = sum + 10;
can be written as:sum += 10; (this means sum = sum + 10)
A First Book of C++ 4th Edition 12
Assignment Variations Assignment Variations (cont'd.)(cont'd.)
![Page 13: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/13.jpg)
The following statements add the numbers 96, 70, 85, and 60 in calculator fashion:
A First Book of C++ 4th Edition 13
AccumulatingAccumulating
![Page 14: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/14.jpg)
Accumulating (cont'd.)Accumulating (cont'd.)
A First Book of C++ 4th Edition 14
![Page 15: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/15.jpg)
Has the form:variable = variable + fixedNumber;Each time statement is executed, value of
variable is increased by a fixed amountIncrement operators (++), (--)
Unary operator for special case when variable is increased or decreased by 1
Using the increment operator, the expression:variable = variable + 1; can be replaced byeither: ++variable; or variable++;
A First Book of C++ 4th Edition 15
CountingCounting
![Page 16: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/16.jpg)
Examples of counting statements:i= i + 1;n = n + 1;count = count + 1;j = j + 2;m = m + 2;kk = kk + 3;
A First Book of C++ 4th Edition 16
Counting (cont'd.)Counting (cont'd.)
![Page 17: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/17.jpg)
Examples of the increment operator:
A First Book of C++ 4th Edition 17
Counting (cont'd.)Counting (cont'd.)
![Page 18: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/18.jpg)
Counting (cont'd.)Counting (cont'd.)
A First Book of C++ 4th Edition 18
![Page 19: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/19.jpg)
Prefix increment operator: the ++ or -- operator appears before a variable
# The expression k = ++n performs two actions:n = n + 1; // increment n firstk = n; // assign n’s value to k
Postfix increment operator: the ++ or -- operator appears after a variable
# The expression k = n++ works differently:k = n; // assign n’s value to kn = n + 1; // and then increment n
A First Book of C++ 4th Edition 19
Counting (cont'd.)Counting (cont'd.)
![Page 20: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/20.jpg)
A program must present results attractively Field width manipulators: control format of
numbers displayed by cout Manipulators included in the output stream
A First Book of C++ 4th Edition 20
Formatted OutputFormatted Output
![Page 21: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/21.jpg)
Formatted Output (cont'd.)Formatted Output (cont'd.)
A First Book of C++ 4th Edition 21
manipulators
![Page 22: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/22.jpg)
A First Book of C++ 4th Edition 22
![Page 23: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/23.jpg)
Formatted Output (cont'd.)Formatted Output (cont'd.)
A First Book of C++ 4th Edition 23
//Program 3.5
#include <iostream>#include <iomanip>using namespace std;
int main(){
cout << setw(4) << 6 << endl // set total width of display to 4 << setw(4) << fixed << setprecision(2) << 18.5 << endl << setw(4) << setiosflags(ios::fixed) << 124 << endl << “---\n” << (6+18.5+124) << endl;return 0;}
![Page 24: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/24.jpg)
Field justification manipulator Example: cout << “|” << setw(10) << setiosflags(ios::left) << 142 << “|”;
A First Book of C++ 4th Edition 24
The The setiosflags()setiosflags() ManipulatorManipulator
![Page 25: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/25.jpg)
Field justification manipulator Example: cout << “|” << setw(10) << setiosflags(ios::left) << 142 << “|”;
A First Book of C++ 4th Edition 25
The The setiosflags()setiosflags() ManipulatorManipulator
![Page 26: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/26.jpg)
oct and hex manipulators are used for conversions to octal and hexadecimal
Display of integer values in one of three possible numbering systems (decimal, octal, and hexadecimal) doesn’t affect how the number is stored in a computer All numbers are stored by using the
computer’s internal codes
A First Book of C++ 4th Edition 26
Hexadecimal and Octal I/OHexadecimal and Octal I/O
![Page 27: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/27.jpg)
Hexadecimal and Octal I/O Hexadecimal and Octal I/O (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 27
![Page 28: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/28.jpg)
A First Book of C++ 4th Edition 28
![Page 29: Csc1100 lecture03 ch03-pt1-s14](https://reader031.fdocuments.in/reader031/viewer/2022021922/58a1a0401a28abe6468b484b/html5/thumbnails/29.jpg)
Hexadecimal and Octal I/O Hexadecimal and Octal I/O (cont'd.)(cont'd.)
A First Book of C++ 4th Edition 29