06 numerical copy
TRANSCRIPT
-
8/6/2019 06 Numerical Copy
1/14
-
8/6/2019 06 Numerical Copy
2/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 2
Rules forDivision
C++ treats integers differently from decimalnumbers.
100 is an int type. 100.0 , 100.0000, and 100. are double type.
The general rule for division ofint and double
types is: double/double ->double (normal)
double/int ->double (normal)
int/double ->double (normal)
int/int ->int (note: the decimal part is discarded)
-
8/6/2019 06 Numerical Copy
3/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 3
Rules forDivision
Examples: 220. / 100.0 double/double ->double result is 2.2
220. / 100 double/int ->double result is 2.2
220 / 100.0 int/double ->double result is 2.2
220 / 100 int/int ->int result is 2
Summary: division is normal unless both thenumerator and denominator are int, then the resultis an int (the decimal part is discarded).
-
8/6/2019 06 Numerical Copy
4/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 4
Assignment Conversions
A decimal number assigned to an int type variable
is truncated.
An integer assigned to a double type variable is
converted to a decimal number. Example 1:
double yy = 2.7;
int i = 15;
int j = 10;
i = yy; // iisnow 2
yy = j; // yy isnow 10.0
-
8/6/2019 06 Numerical Copy
5/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 5
Assignment Conversions
Example 2:int m,n;
double xx;
m = 7;
n = 2.5;
xx = m / n;
n = xx + m / 2;
// Whatisthe value of n?
-
8/6/2019 06 Numerical Copy
6/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 6
Assignment Conversions
Example 2:int m,n;
double xx;
m = 7;
n = 2.5; // 2.5 convertedto 2 and assignedto n
xx = m/n; // 7/2=3 convertedto 3.0 and assignedto xx
n = xx+m/2;
// m/2=3 : integer division
// xx+m/2 : double addition because xx is double// convert result of m/2 to double (i.e. 3.0)
// xx+m/2=6.0
// convert result of xx+m/2 to int (i.e. 6)
// because nisint
-
8/6/2019 06 Numerical Copy
7/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 7
Forcinga Type Change
You can change the type of an expression with a castoperation.
Syntax:var
iable1 =
type(var
iable2);
variable1 = type(expression);
Example:int x=1, y=2;
double result1 = x/y; // result1 is 0.0
double result2 = double(x)/y; // result2 is 0.5double result3 = x/double(y); // result3 is 0.5
double result4 = double(x)/double(y);// result4is 0.5
double result5 = double(x/y); // result5 is 0.0
int cents = int(result4*100); // centsis 50
-
8/6/2019 06 Numerical Copy
8/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 8
Standard Input/Output
cin - the standard input stream
Input operator >>
Extracts data from input stream (the keyboard by
default).
Skips over white spaces.
Extracts only characters of the right form andperforms automatic conversion to the typespecified.
-
8/6/2019 06 Numerical Copy
9/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 9
Standard Input/Output
cout - the standard output stream
Output operator >score;
cout
-
8/6/2019 06 Numerical Copy
10/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 10
Standard Input/Output
Some special output characters:
\a bell
\t tab
\n new line
\' single quote
\" double quote
-
8/6/2019 06 Numerical Copy
11/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 11
Format Manipulation#include
setw(int size)
Specifies the number of characters to use in displaying the next value,which is right-justified.
Ex: cout
-
8/6/2019 06 Numerical Copy
12/14
COMP 102 Prog Fundamentals I:C++ Numerical Data, Input/Output /Slide 12
Format Manipulation
#include
#include
usingnamespace std;
int main(){
cout
-
8/6/2019 06 Numerical Copy
13/14
// Demonstrate the features of output formatmanipulators
// Input: cost of lunch,number of people attending lunch
// Output: lunch cost per person
#include// Use IO manipulators
#include
usingnamespace std;int main() {
double cost_of_lunch, cost_per_person;
int number_of_people;
cout cost_of_lunch;
cout >number_of_people;
cost_per_person = cost_of_lunch / number_of_people;
//cout
-
8/6/2019 06 Numerical Copy
14/14
cout