Title: Lecture 4 Constants and Equations
1Lecture 4Constants and Equations
- ENGR17 Engineering Programming
- Section 1
- Fall 2001
9/13/01
2Outline
- Named Constants
- Assignment Statements
- Writing Equations
- Precedence
- Type conversion
3Named 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.
4Creating 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
5Named Constant Examples
6Compare Constants and Variables
7Assignment 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
8Assignment Examples
9Using 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
10Writing 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
11Mathematical Operators
Modulus Example (only used for integers) (5 /
2) is 2 (5 2) is 1 (1 is the remainder)
12Equation 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
13Floating Point versus Integer Divide
14Explicit 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
15Explicit Type Conversion in an Equation
16How 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!
17How 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)
18Summary
- 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
19Questions
- 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
20Questions
- 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
21Example
- 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.
22Example - 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