Lecture 4 Constants and Equations - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Lecture 4 Constants and Equations

Description:

Mathematical operators used in an expression have an order of precedence ... Consider Precedence and use () when in doubt. Type conversion and when to cast ... – PowerPoint PPT presentation

Number of Views:22
Avg rating:3.0/5.0
Slides: 23
Provided by: jimand
Category:

less

Transcript and Presenter's Notes

Title: Lecture 4 Constants and Equations


1
Lecture 4Constants and Equations
  • ENGR17 Engineering Programming
  • Section 1
  • Fall 2001

9/13/01
2
Outline
  • Named Constants
  • Assignment Statements
  • Writing Equations
  • Precedence
  • Type conversion

3
Named Constants
  • Memory locations whose contents cannot change
    while the program is running
  • Make program more self-documenting and therefore
    easier to modify because of use of meaningful
    phrases
  • Different than a Literal Constant
  • Recall, Literal Constant A, 42, hello, etc.

4
Creating a Named Constant in C
  • Use the keyword const (constant) in a statement
    that uses the following syntax
  • Const datatype constantname expression
  • Once created, use the name of the named constant
    instead of its value

5
Named Constant Examples
6
Compare Constants and Variables
7
Assignment Statement
  • Syntax
  • Variablename expression
  • Assigns value of expression on right side of
    equal sign to variable whose variablename appears
    on left side of equal sign
  • Data type of expression must match data type of
    variable to which it is assigned
  • Avoid implicit type conversion by using a type
    cast
  • Variable can store only one item of data at any
    one time new data replaces existing data
  • Example of implicit/explicit type conversion

8
Assignment Examples
9
Using cin and gtgt to Store Data
  • Syntax
  • cin gtgt variablename
  • Program waits for user to enter information
  • Examplecout ltlt "Enter Value "cin gtgt
    valuecout ltlt "Value is " ltlt value ltlt endl
  • cin is the name of the standard input stream
  • Default setting is the keyboard as input
  • cout is the name of the standard output stream
  • Default setting is the console as output
  • ltlt is the insertion operator
  • gtgt is the extraction operator

10
Writing C Equations
  • Instruct computer to perform a calculation by
    writing an arithmetic equation that follows the
    syntax variablename expression
  • Mathematical operators used in an expression have
    an order of precedence
  • Use parentheses to override
  • Operations within parentheses are always
    performed before operations outside parentheses
  • Do not enter the comma, dollar sign, or percent
    sign in numeric literal constant

11
Mathematical Operators
Modulus Example (only used for integers) (5 /
2) is 2 (5 2) is 1 (1 is the remainder)
12
Equation Examples
  • int x 2
  • int y 3
  • int z 5
  • int v 0
  • v x y z
  • v x (y z)
  • v x z y
  • v x z y
  • x
  • y--
  • x yz

13
Floating Point versus Integer Divide
14
Explicit Type Conversion in an Equation
  • Use the same data types on both sides of the
  • If not, must use explicit type conversion
  • Precede item of data with the C keyword that
    represents the desired data type
  • Enclose the keyword in parentheses

15
Explicit Type Conversion in an Equation
16
How to Read a String
  • include ltiostream.hgt
  • int main ()
  • char name100 // string of length 100
  • cout ltlt Enter name
  • cin.getline(name,100)
  • cout ltlt Name is ltlt name ltlt endl

This is different than how the book does it. Read
a string this way ? not like the book!
17
How to Calculate a Power
  • include ltiostream.hgt
  • include ltmath.hgt
  • int main ()
  • double valueIn
  • double valueOut
  • const double POWER5 5
  • cout ltlt Enter number
  • cin gtgt valueIn
  • valueOut pow(valueIn,POWER5)
  • cout ltlt Result is ltlt valueOut ltlt endl

To get Y XN Code Y pow(X,N)
18
Summary
  • Named versus Literal Constants
  • Assignment Statements and data types
  • Writing Equations
  • Understand Operators
  • Consider Precedence and use () when in doubt
  • Type conversion and when to cast
  • Reading a string
  • Calculating a power

19
Questions
  • Create a named constant value and set it equal to
    42
  • const int VALUE 1
  • Create a named constant rate and set it equal to
    0.05
  • const double RATE 0.05
  • const float RATE (float) 0.05
  • Create a name constant letter and set it to A
  • const char LETTER A
  • Assign 42 to the integer variable value
  • value 42
  • Assign 42.7 to the integer variable value
  • value (int) 42.7

20
Questions
  • What will be the value of the integer variable
    answer?answer 10 / 4 3 - 5 2
  • 5
  • What will be the value of the float variable
    answer?answer 10 / 4 3 - 5 2
  • 6.5
  • Write an expression to multiply the floats x and
    y and store it in the integer z
  • z (int)(xy)
  • Write an expression to add the float x to the
    integer y and store in the integer z
  • z ((int) x) y

21
Example
  • Write a cashier program that displays each
    students name and the total amount the student
    owes for the semester, including tuition, room,
    and board. The tuition is 100 per semester hour,
    and room and board is 1800 per semester.

22
Example - Code
  • include ltiostream.hgt
  • int main ()
  • const int SEMHOURFEE 100
  • const int ROOMBOARD 1800
  • char name100
  • int hours 0
  • int total 0
  • // enter input data
  • cout ltlt "Enter student name "
  • cin.getline(name,100)
  • cout ltlt "Enter hours enrolled "
  • cin gtgt hours
  • // calculate total owed
  • total hours SEMHOURFEE ROOMBOARD
  • // display output
Write a Comment
User Comments (0)
About PowerShow.com