CprS 157 Schedule
CprS 157 Class 2
CprS 157 Class 4

### Numeric Datatypes, Expressions, and Formatted Output Chapter 3 - Class 3

• Arithmetic Expressions

• numeric datatypes: short, int, long, float, double
arithmetic operators: +, -, *, /, %
increment and decrement operators: ++, --
precedence rules for correctly evaluating expressions with several arithmetic operations
• Using Manipulators to Format Floating-point Numbers

• add new functionality with iomanip
use setw(6) to adjust the display width of a floating-point number to 6 spaces
use fixed to avoid use of the exponent notation for floating-point numbers
use showpoint to force the display of a decimal point in a floating-point number
use setprecision(2) to display 2 digits past the decimal point of a floating-point number
see today's sample programs for some illustrative examples
• Coercion and Conversion (Casting)

• explicit type conversion
implicit type coercion
• Functions

• function declarations
function definitions
formal and actual parameters
return type
return statements
void functions
invoking a function (function calls)
functions used in expressions
library functions

freezeBoil.cpp
houseCost.cpp
walk.cpp

// FreezeBoil program
// This program computes the midpoint between
// the freezing and boiling points of water

// HouseCost program
// This program computes the cost per square foot of
// living space for a house, given the dimensions of
// the house, the number of stories, the size of the
// nonliving space, and the total cost less land

// Walk program
// This program computes the mileage (rounded to tenths of a mile)
// for each of four distances between points in a city, given
// the measurements on a map with a scale of one inch equal to
// one quarter of a mile

Modify freezeBoil.cpp so that it uses a function to compute the average of two floating-point numbers. Modify houseCost.cpp so that it uses a function to round off floating-point numbers.

### Programming Exercises

• Programming Warm-up Exercises (pages 148-151)

• 3.3abef (arithmetic operations)
3.4acef (library functions: abs,fabs,pow,sqrt)
3.8 (rectangle)

Solve these exercises in class, to your own satisfaction. Demonstrate your solutions to the instructor or to the class TA. There is nothing to hand in from this activity.

### Homework For Chapter 3

• Programming Problem 3.3, page 151 (Temperature Conversion)

•
The "Hello, World!" program is surely the most often rewritten C++ program, and the temperature conversion program might be a close second. Here we go! Be the first kid on your block to rewrite the temperature conversion program in C++. Once you have it working correctly, let's see if we can organize things so that your program takes its input from the keyboard. Enhance your program so that it also converts its input into degrees Kelvin. Strive for clear and elegant solutions to your programming exercises. Correctly and attractively formatting your code helps to convey the structure and logic of your work to your readers. Drop an electronic copy of your program into the CS157 drop box before class time, 9:30 AM, next Tuesday. Use the file-naming conventions we have developed in class.