CS100: COMPUTATIONAL
PROBLEM SOLVING
(FALL 2022-23)
Lecture 7
REVISION
2
USING VARIABLES, CONSTANTS,
ASSIGNMENTS, PROGRAM
u Modify our program and compute the
volume of a six-pack of soda cans and
total volume of given numbers of six-
packs and two-liter bottles.
CHAPTER 2: FUNDAMENTAL
DATA TYPES-continued
TODAYS TOPICS:
u Introduction to Data Types, Mathematical
Operators
HELPFUL READINGS:
u Cay Horstmann:
§ Chapter 2: Sections 2.1-2.2
Variable Names
1. Variable names must start with a letter or the underscore
(_) character, and the remaining characters must be
letters numbers, or underscores.
2. You cannot use other symbols such as $ or %. Spaces
are not permitted inside names; you can use an
underscore instead, as in can_volume.
3. Variable names are case-sensitive, that is, can_volume
and can_Volume are different names.
For that reason, it is a good idea to use only lowercase
letters in variable names.
4. You cannot use reserved words such as double or
return as names; these words are reserved exclusively
for their special C++ meanings.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Variable Names
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Number Types
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Numeric Types in C++
Could differ in
other machines
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Why called “floating-point” ?
By the way, these numbers are called “floating-point”
because of their internal representation in the computer.
Consider the numbers 29600, 2.96, and 0.0296.
They can be represented in a very similar way: namely,
as a sequence of the significant digits: 296 and an
indication of the position of the decimal point.
When the values are multiplied or divided by 10, only the
position of the decimal point changes; it “floats”.
Computers use base 2, not base 10,
but the principle is the same.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Numeric Types in C++
In addition to the int type, C++ has these
additional integer types: short, long.
For each integer type, there is an unsigned
equivalent:
unsigned short, unsigned long
For example, the short type typically has
a range from –32,768 to 32,767, whereas
unsigned short has a range from 0 to
65,535. These strange-looking limits are
the result of the use of binary numbers in
computers.
A short value uses 16 binary digits, which
can encode 216 = 65,536 values.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Arithmetic Operators
C++ has the same arithmetic
operators as a calculator:
* for multiplication: a*b
(not a . b or ab as in math)
/ for division: a/b
(not ÷ or a fraction bar as in math)
+ for addition: a+b
- for subtraction: a–b
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Increment and Decrement
u Changing a variable by adding or subtracting 1 is so
common that there is a special shorthand for these:
The increment and decrement operators.
counter++; // add 1 to counter
counter--; // subtract 1 from counter
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Combining Assignment and Arithmetic
In C++, you can combine arithmetic and assignments.
For example, the statement
total += cans * CAN_VOLUME;
is a shortcut for
total = total + cans * CAN_VOLUME;
Similarly,
total *= 2;
is another way of writing
total = total * 2;
Many programmers prefer using this form of coding.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Number Ranges and Precisions
u All number types have limited number of
digits (no. of bits)!
§ Limited Range (overflow error):
u double is safer
§ Limited Precision (rounding off error)
Integer Division and Remainder
Time to break open the money pot.
You want to determine the value in dollars and
cents stored in the money pot.
You obtain the dollars through an integer
division
by 100.
The integer division discards the remainder.
To obtain the remainder, use the % operator:
int pennies = 1729;
int dollars = pennies / 100; // Sets dollars to 17
int cents = pennies % 100; // Sets cents to 29
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Integer Division and Remainder
u Note:
§ % is Mod (Modulo, Modulus): the remainder
§ Integer division is different from floating
point (normal) division
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Common Error – Unintended Integer
Division
u If both arguments of / are integers,
the remainder is discarded:
7 / 3 is 2, not 2.5
u but
7.0 / 4.0
7 / 4.0
7.0 / 4
all yield 1.75.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Unintended Integer Division: Example
Compute the average of three integers:
cout << "Please enter your last three test
scores: ";
int s1;
int s2;
int s3;
cin >> s1 >> s2 >> s3;
double average = (s1 + s2 + s3) / 3;
cout << "Your average score is " << average <<
endl;
Ans = 4 (wrong answer)
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Unintended Integer Division: Problem
fixed
The remedy is to make the numerator or denominator
into a floating-point number:
double total = s1 + s2 + s3;
double average = total / 3;
or
double average = (s1 + s2 + s3) / 3.0;
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Arithmetic Operators
Just as in regular algebraic notation,
* and / have higher precedence
than + and –.
In a + b / 2,
the b / 2 happens first.
Ø Use parenthesis!
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Error – Unbalanced Parentheses
Consider the expression
(-(b * b - 4 * a * c) / (2 * a)
What is wrong with it?
The parentheses are unbalanced.
This is very common with complicated expressions.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Powers and Roots
What about this?
n
⎛ r ⎞
b + ⎜1+ ⎟
⎝ 100 ⎠
Inside the parentheses is easy:
1 + (r / 100)
But that raised to the n?
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Powers and Roots
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Powers and Roots
The power function has the base followed by a
comma
followed by the power to raise the base to:
pow(base, exponent)
Using the pow function:
b + pow(1 + r / 100, n)
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Powers and Roots
u In C++, there are no symbols for powers and roots.
To compute them, you must call functions.
u The C++ library defines many mathematical functions
such as sqrt (square root) and pow (raising to a power).
u To use the functions in this library, called the cmath
library, you must place the line:
#include <cmath>
at the top of your program file.
u It is also necessary to include
using namespace std;
at the top of your program file.
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved
Other Mathematical Functions
C++ for Everyone by Cay Horstmann
Copyright © 2012 by John Wiley & Sons. All rights reserved