Docsity

Programming Exercise Three: Data Types, Expressions, and Mathematical Functions, Slides of Computer Science

Toggle sidebar
bg1
Programming exercise three February 14, 2006
1
Laboratory III
Laboratory III –
–Data type
Data type
conversions, expressions, and
conversions, expressions, and
mathematical functions
mathematical functions
Larry Caretto
Computer Science 106
Computing in Engineering and
Science
February 14, 2006
2
Outline
• Review exercise two
• Exercise three goals
• Summarize information on data types
• Review lecture material on expressions,
operator precedence and conversion
• Outline tasks for exercise three
3
Review Exercise Two
• Note that division by zero produces
infinity expressed as 1.#INF
• Zero divided by zero is indefinite 1.#IND
• Integer division truncates
• Cannot enter decimal or E notation
numbers as input for integers
• Learned how to produce spacing in
output
4
Multiple Data Inputs
• Can use space or enter key between
inputs
• Can have one or more input commands
– cin >> x >> y;
– cin >> x; cin >> y;
• For either the single or repeated cin
commands above you can use a space
or enter between data inputs
5
Exercise Three Goals
• Understand how to handle type
conversion in expressions
• Learn how to write expressions to
represent equations correctly
• Be able to define and use symbolic
constants
• Be able to use mathematical functions
from the C++ library
6
Expressions
•<variable>= <expression>;
• Expression is a constant, a variable, or
a collection of variables, constants and
operators
• Mathematical operators, in order of
precedence are [1] unary – (highest), [2]
multiplication (*), division (/), and mod
(%), [3] addition (+) and subtraction(-)
pf3

Partial preview of the text

Download Programming Exercise Three: Data Types, Expressions, and Mathematical Functions and more Slides Computer Science in PDF only on Docsity!

Laboratory IIILaboratory III – –Data typeData type

conversions, expressions, andconversions, expressions, and

mathematical functionsmathematical functions

Larry Caretto Computer Science 106 Computing in Engineering and Science

February 14, 2006

2

Outline

  • Review exercise two
  • Exercise three goals
  • Summarize information on data types
  • Review lecture material on expressions, operator precedence and conversion
  • Outline tasks for exercise three

3

Review Exercise Two

  • Note that division by zero produces infinity expressed as 1.#INF
  • Zero divided by zero is indefinite 1.#IND
  • Integer division truncates
  • Cannot enter decimal or E notation numbers as input for integers
  • Learned how to produce spacing in output

4

Multiple Data Inputs

  • Can use space or enter key between inputs
  • Can have one or more input commands
    • cin >> x >> y;
    • cin >> x; cin >> y;
  • For either the single or repeated cin commands above you can use a space or enter between data inputs

5

Exercise Three Goals

  • Understand how to handle type conversion in expressions
  • Learn how to write expressions to represent equations correctly
  • Be able to define and use symbolic constants
  • Be able to use mathematical functions from the C++ library

6

Expressions

  • < variable > = < expression> ;
  • Expression is a constant, a variable, or a collection of variables, constants and operators
  • Mathematical operators, in order of precedence are [1] unary – (highest), [2] multiplication (*), division (/), and mod (%), [3] addition (+) and subtraction(-)

7

Expressions (continued)

  • Use parentheses to override normal operator precedence
  • Can use extra parentheses for clarity
  • Example equation

x y w u v

= +

  • Code is w = ( u + v ) / ( x + y );
  • Without parentheses we would get incorrect result

8

Converting Data Types

  • Conversion occurs when a value of one type is assigned to another type - int x = 6; double y = x; // result is y = 6. - double u = 7.89; v = u; // result is v = 7
  • When two operands are different types, the lower type is promoted to the higher type. ( E. g. gives a result.)

9

Converting data types

  • Expressions evaluated with no knowledge of left-hand side
  • Can force conversion by functions like int() and double(). - double z = 1/2; // gives z = 0. - double y = double(1)/2; // gives z = 0.
  • Watch out for expressions like KE = (1/2) * m * V * V;

10

Tasks for Exercise Three

  • One – copy and paste code with various conversions; correct one error; run and study results
  • Two – Write a program to get results of three expressions for three sets of data
  • Three and four – write programs using math functions and a symbolic constant for π to get relationships for a circle.

11

Task One – Copy and Paste

double a; int x = 27, y = 4, z; z = x / y; cout << "Example of output from different” << “ data types. \nIn these results ” << “x = 27, y = 4, and z are type “ << “ int; \na is type double.\n\n"; cout << "For z = x / y, z = " << z; a = x / y; cout << “\nFor a = x / y, a = " << a; z = double( x ) / y; cout << “\nFor z = double( x )/ y, z = "

Study and understand the results 12

Task Two

  • Write a program that
    • Inputs w, x, y, and z
    • Computes a, b, and c
    • Prints the results
  • Execute for three data sets
  • Compute results with calculator and make sure that your program is correct

y z

w x a −

y

w x

x

z

b

wy

x

z

y

x

w

c