printing numbers
DESCRIPTION
Floating point numbers are printed out in some default format, which is usually not how you want them to print out. For example, if you want to print out dollars and cents: double price = 78.50; coutTRANSCRIPT
Printing Numbers
Floating point numbers are printed out in some
default format, which is usually not how you want
them to print out. For example, if you want to print
out dollars and cents:
double price = 78.50;cout << “Price is $” << price << endl;
You might see
Price is $78.5
Formatting Numbers
If that's not what you want, you can use formatting
commands. The following commands tell the
computer to use a fixed format (rather than
scientific notation), to always show the decimal
point, and to show two digits to the right of the
decimal point:
cout.setf(ios::fixed);cout.setf(ios::showpoint);cout.precision(2);
More Formatting
You can also specify that something be printed out in
a field of a given size, that is, it will always take up
a certain number of columns in the output, padding
with spaces if necessary. You must include the
iomanip library to do so and use the setw(int)
modifer.
#include <iomanip>int main(void) { cout << setw(10) << 12 << endl;}
Formatting Example
#include <iostream>#include <iomanip>using namespace std;int main(void) { double price = 78.5; cout.setf(ios::fixed); cout.setf(ios::showpoint); cout.precision(2); cout << “Price is $” << setw(10) << price << endl;}
givesPrice is $ 78.50
Comments
Comments are human-readable lines of text which
can be used to document your program. The
computer ignores comments. There are two ways to
indicate a comment. Two slashes (“//”) indicate that
everything that follows them until the end of the
line is a comment. A block of text is treated as a
comment if it begins with “/*” and ends with “*/”.
Example Comments
/* This program was written by Don Simon on 01/15/2010 for the COSC 160 class */#include <iostream> // libraryusing namespace std; // namespace
Documentation Standards
For this class, each program should have the
following documentation:Author's name, date, name of programShort program descriptionDescription of each variable (if not obvious)Each function and method should have a short
description, including the inputs and outpusDescription of major sections of codeDetails of any tricky (non-obvious) code
Documentation Standards (cont'd)
The idea is that some one who reads the code should
understand what is going on. The comments should
not mimic the code, that is, restate the code in
English, but should provide the gist of what each
major section of code does. For example:a = a + 1; // add 1 to a.
is not an appropriate comment, but/* Read in the students' test scores and find the average */
is.
Namespaces
Namespaces are used to segment identifiers so that
different programmers and different modules or
libraries can use the same name without confusion.
You can create your own namespaces, but for now,
it is enough to know how to use a namespace. All
standard libraries use the std namespace, so your
programs should include the line:
using namespace std;
Libraries
The C++ system is divided into a core language and
a set of libraries. The libraries provide additional
functionality. One of the commonly used libraries
contains the I/O routines. Another has functions for
manipulating how things are printed out. To use a
library, you must have an #include line near the
top of your program to load the library functions:
#include <iostream>#include <iomanip>
Arithmetic Expressions
As noted before, you can use arithmetic operators in
expressions to manipulate numeric values:a + b * 7 – (b + 3)
There is an order of precedence which determines
which operators are done first: First *, /, and % are
performed, and then + and -. Two arithmetic
operators of the same precedence are done from left
to right. Parentheses can be used to change the
order.
Relational Operators
There are also operators which compare values and
return true or false (actually 1 or 0, but think of
them as true and false). For example “a < b”
returns true if a is less than b and false
otherwise. These are called relational operators.
The relational operators are == (equals), != (not
equals), < (less than), > (greater than), <= (less than
or equals), >= (greater than or equals).
Boolean Expressions
We can combine simple relational tests together into
more complex expressions using Boolean operators.
Boolean operators work on logical (or Boolean)
values (true, false) and return true and
false. The common operators are && (and), ||
(or), and ! (not).0 <= a && a <= 10 // a is between 0 and 10 incl.
Boolean Expressions (cont'd)
0 < a || 0 < b // a is greater than 0 // or b is greater than 0.!(a > 5) // a is not greater than 5.
Note that0 <= a <= 10
is not a valid expression.
Truth Tables
P QT T TT F F F T FF F F
P && Q P Q P || QT T TT F T F T TF F F
P !PT FF T
Order of Precedence
++ Post-increment n++
-- Post-decrement n--
++ ++n
-- --n
! Not !(a > b)
- Unary minus -n
+ Unary plus +n
* Multiply a * b
/ Divide a / b
% Remainder a % b
Pre-increment
Pre-decrement
Order of Precedence (cont'd)
+ Addition a – b
- Subtraction a + b
<< Insertion
>> Extraction
< Less than a < b
> Greater than a > b
<= Less than or equal a <= b
>= Greater than or equal a >= b
== Equal a == b
!= Not equal a != b
cout << a
cin >> a
Order of Precedence (cont'd)
&& And a > b && b > c
|| Or a > b || b > c
= Assignment a = b
+= Add and assign a += 2
-= Subtract and assign a -= 2
*= Multiply and assign a *= 2
/= Divide and assign a /= 2
%= Modulo and assign a %= 2
Short-Circuit Evaluation
The Boolean operators && and || are evaluated using
short-circuit evaluation. That is, if you have the
expression (a > b) && (b > c), and it turns out that a
is not greater than b (so a > b is false), then there is
no need to evaluate b > c (since the whole
expression is going to be false anyway). Similarly,
if you evaluate (a > b) || (b > c) and a is greater than
b, then the expression is true, so b > c is not
evaluated.
If-Else Statements
An If-Else statements allows the program to execute
one section of the program if a Boolean expression
is true, and a different section, if that Boolean
expression is false. This is called conditional
evaluation and allows the program to do different
things given different inputs.
If-Else Example
if (a > b) cout << a << endl;else cout << b << endl;
If a is greater than b, then then first line is executed
and the value of a is printed out. Otherwise, the
second line is executed and the value of b is printed.
If-Else Statement Syntax
The syntax for an If-Else statement is:
if <boolean expression> <stmt1>else <stmt2>
Compound Statements
If you want to do more than one statement if an IF-
else case, you can form a block of statements, or
compound statement, by enclosing the list of
statements in curly braces ({ }). The statements in
side of the braces will be executed sequential, and
the block counts as one statement.
Compound Statement Example
if (a > b) { c = a; cout << a << endl;}else { c = b; cout << b << endl;}
Exercise
Write a program that reads in two integers and then
prints out the smaller of the two followed by the
larger of the two.
You will need to:Prompt the user to type in two intsRead in the two intsCompare the two ints, and then depending on which
is larger, print them out in the right order.