Top Banner
C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13
20

C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Dec 17, 2015

Download

Documents

Oscar Fields
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

C++ for Everyone by Cay HorstmannCopyright © 2008 by John Wiley & Sons. All rights reserved

If Statement Continued

10/07/13

Page 2: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

C++ for Everyone by Cay HorstmannCopyright © 2008 by John Wiley & Sons. All rights reserved

Get Back Exams

• Get back corrected exams.• Other note: Program 4 input is m/s

Page 3: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

C++ for Everyone by Cay HorstmannCopyright © 2008 by John Wiley & Sons. All rights reserved

Objectives

• Indent to make if’s easier to read.• Comparing floating point values.– Prevent round off error

• Comparing strings.

Page 4: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Style guidelines

Meaningful names Separate words with underscore or

capital letters. feet_input, milesDriven

Constants Give constants meaningful names ALL CAPS EARTH_MASS, SPEED_OF_LIGHT

No “magic numbers”

Not, e = m * pow(299792458,2.0)

Page 5: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Style guidelines

Documentation Use comments to explain your program. Not necessary for every line.

Spacing At most, one statement per line. Blank lines included for clarity.

Indentation

Page 6: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Block-structured code has the property that nestedstatements are indented by one or more levels.

int main(){ int floor; ... if (floor > 13) { floor--; } ... return 0;} 0 1 2Indentation level

The if Statement – Indent when Nesting

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 7: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Using the tab key is a way to get this indentation

but …

not all tabs are the same width!

Luckily most development environments have settings to automatically convert all tabs to spaces.

The if Statement – Indent when Nesting

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 8: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Lexicographical Ordering of Strings

Comparing strings uses “lexicographical” order to decide which is larger or smaller or if two strings are equal.

“Dictionary order” is another way to think about “lexicographical” (and it’s a little bit easier to pronounce).

string name = "Tom";

if (name < "Dick")...

The test is false because “Dick”

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 9: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Lexicographical Ordering of Strings

Comparing strings uses “lexicographical” order to decide which is larger or smaller or if two strings are equal.

“Dictionary order” is another way to think about “lexicographical” (and it’s a little bit easier to pronounce).

string name = "Tom";

if (name < "Dick")...

The test is false because “Dick” would come before “Tom”

if they were words in a dictionary.

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 10: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Lexicographical Ordering of Strings

• All uppercase letters come before the lowercase letters.For example, "Z" comes before "a".

• The space character comes before all printable characters.

• Numbers come before letters.• The punctuation marks are ordered but

we won’t go into that now.

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 11: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Lexicographical Ordering of Strings

When comparing two strings,

you compare the first letters of each word, then the

second letters, and so on, until:– one of the strings ends– you find the first letter pair that doesn’t

match.

If one of the strings ends, the longer string is

considered the “larger” one.

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 12: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Lexicographical Ordering of Strings

For example, compare "car" with "cart".

c a r

c a r t

The first three letters match, and we reach the end of

the first string – making it less than the second.

Therefore "car" comes before "cart" in lexicographic

ordering.C++ for Everyone by Cay Horstmann

Copyright © 2012 by John Wiley & Sons. All rights reserved

Page 13: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Lexicographical Ordering of Strings

When you reach a mismatch, the string containingthe “larger” character is considered “larger”.

For example, let’s compare "cat" with "cart".

c a t

c a r t

The first two letters match.

Since t comes after r, the string "cat" comes after "cart“

in the lexicographic ordering.C++ for Everyone by Cay Horstmann

Copyright © 2012 by John Wiley & Sons. All rights reserved

Page 14: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Kinds of Error Messages

There are two kinds of errors:

Warnings

Errors

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 15: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Kinds of Error Messages

• Error messages are fatal.– The compiler will not translate a program

with one or more errors.• Warning messages are advisory.

– The compiler will translate the program,but there is a good chance that the program will not do what you expect it to do.

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 16: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Common Error – Exact Comparison of Floating-Point Numbers

Roundoff errors

Does == 2 ?

Let’s see (by writing code, of course) …

r 2

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 17: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Common Error – Exact Comparison of Floating-Point Numbers

double r = sqrt(2.0);

if (r * r == 2)

{

cout << "sqrt(2) squared is 2" << endl;

}

else

{

cout << "sqrt(2) squared is not 2 but "

<< setprecision(18) << r * r << endl;

}

This program displays:sqrt(2) squared is not 2 but 2.00000000000000044

roundoff error

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 18: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Common Error – Exact Comparison of Floating-Point Numbers

Roundoff errors – a solution

Close enough will do.

yx

C++ for Everyone by Cay HorstmannCopyright © 2012 by John Wiley & Sons. All rights reserved

Page 19: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

Common Error – Exact Comparison of Floating-Point Numbers

It is common to set ε to 10–14 when comparingdouble numbers:

const double EPSILON = 1E-14;

double r = sqrt(2.0);

if (fabs(r * r - 2) < EPSILON)

{

cout << "sqrt(2) squared is approximately ";

cout << r * r << endl;

}

Include the <cmath> header to use sqrt andthe fabs function which gives the absolute value.C++ for Everyone by Cay Horstmann

Copyright © 2012 by John Wiley & Sons. All rights reserved

Page 20: C++ for Everyone by Cay Horstmann Copyright © 2008 by John Wiley & Sons. All rights reserved If Statement Continued 10/07/13.

C++ for Everyone by Cay HorstmannCopyright © 2008 by John Wiley & Sons. All rights reserved

Write a Program

Write a program to classify an input angle measure. Output either “right”, “obtuse” or “acute”. Consider round-off error might make a right angle slight more or less than 90. Set an epsilon value for the error tolerance. Use that when checking for equal to 90 degrees.