CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring 2009 1 Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.

Slides:



Advertisements
Similar presentations
Standard I/O Lesson Outline
Advertisements

Standard I/O Lesson CS1313 Spring Standard I/O Lesson Outline 1.Standard I/O Lesson Outline 2.Output via printf 3.Placeholders 4.Placeholders for.
Computer Programming w/ Eng. Applications
Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.
Types and Arithmetic Operators
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
 Monday, 9/9/02, Slide #1 CS106 Introduction to CS1 Monday, 9/9/02  QUESTIONS?? (Exercises, Lab #1, etc.)  Today:  More on float objects  C++ concept:
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
Mathematical Operators: working with floating point numbers and more operators  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
The scanf Function The scanf function reads input from the standard input device into one or more variables Example: scanf(“%lf”, &miles); Reads a real.
Basic C Programming Data Types and Arithmetic Operations 01/30/15.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
User Defined Functions Lesson 2 CS1313 Spring User Defined Functions 2 Outline 1.User Defined Functions 2 Outline 2.Argument Order When Passing.
For Loop Lesson 3 CS1313 Spring for Loop Lesson 3 Outline 1. for Loop Lesson 3 Outline 2. for Loop with a float Counter: BAD! 3. float Counter Example.
For Loop Lesson 2 CS1313 Spring for Loop Lesson 2 Outline 1. for Loop Lesson 2 Outline 2. for Loop Application 3. Factorial 4. Factorial Program.
CS1313: Standard Library Functions Lesson CS1313 Spring Standard Library Functions Outline 1.Standard Library Functions Outline 2.Functions in Mathematics.
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Array Lesson 1 CS1313 Spring Array Lesson 1 Outline 1.Array Lesson 1 Outline 2.Mean of a List of Numbers 3.mean: Declarations 4.mean: Greeting,
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
Agenda  Commenting  Inputting Data from Keyboard (scanf)  Arithmetic Operators  ( ) * / + - %  Order of Operations  Mixing Different Numeric Data.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Input, Output, and Processing
Array Lesson 2 CS1313 Spring Array Lesson 2 Outline 1.Array Lesson 2 Outline 2.Reading Array Values Using for Loop #1 3.Reading Array Values Using.
For Loop Lesson 1 CS1313 Spring for Loop Lesson 1 Outline 1. for Loop Lesson 1 Outline 2.A while Loop That Counts #1 3.A while Loop That Counts.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
C OMPUTER P ROGRAMMING 1 Assignment. A SSIGNMENT We have used gets to input a value into variable The second way to give a variable a value is known as.
Structures Lesson CS1313 Spring Structures Lesson Outline 1.Structures Lesson Outline 2.Beyond Arrays 3.A Company and Its Employees #1 4.A Company.
Variables & Constants Lesson 2 Outline
Data Types Declarations Expressions Data storage C++ Basics.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
Operators and Expressions. 2 String Concatenation  The plus operator (+) is also used for arithmetic addition  The function that the + operator performs.
Arithmetic Expressions in C++. CSCE 1062 Outline Data declaration {section 2.3} Arithmetic operators in C++ {section 2.6} Mixed data type arithmetic in.
Types of C Variables:  The following are some types of C variables on the basis of constants values it has. For example: ○ An integer variable can hold.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
 Most C programs perform calculations using the C arithmetic operators (Fig. 2.9).  Note the use of various special symbols not used in algebra.  The.
Announcements Assignment 2 Out Today Quiz today - so I need to shut up at 4:25 1.
Lecture2.
CSE 220 – C Programming Expressions.
Topics Designing a Program Input, Processing, and Output
Boolean Data Lesson #1 Outline
for Loop 1 Outline for Loop 1 Outline for Loop
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
Multiple variables can be created in one declaration
Assignment and Arithmetic expressions
Arithmetic Operator Operation Example + addition x + y
Boolean Data Lesson #1 Outline
Expressions Chapter 4 Copyright © 2008 W. W. Norton & Company.
User Defined Functions 2 Outline
for Loop Lesson 3 Outline
Lecture3.
CS150 Introduction to Computer Science 1
Expressions An expression is a portion of a C++ statement that performs an evaluation of some kind Generally requires that a computation or data manipulation.
CS150 Introduction to Computer Science 1
Arithmetic Expressions Lesson #2 Outline
Topics Designing a Program Input, Processing, and Output
Topics Designing a Program Input, Processing, and Output
Boolean Data Outline Boolean Data Lesson Boolean Data Outline
EECE.2160 ECE Application Programming
Variables Lesson Outline
EECE.2160 ECE Application Programming
Arithmetic Expressions Lesson #1 Outline
Presentation transcript:

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named Constant & Variable Operands #1 3.Named Constant & Variable Operands #2 4.Named Constant & Variable Operands #2 5.Constant-Valued Expressions #1 6.Constant-Valued Expressions #2 7.Constant-Valued Expressions #3 8.Assignments W/O Expressions: Not Very Useful 9.Assignments with Expressions: Crucial 10.Meaning of Assignment w/Expression 11.Assignment w/Expression Example 12.Assignment w/Same Variable on Both Sides 13.Same Variable on Both Sides: Meaning 14.Same Variable on Both Sides: Example 15.Single Mode Arithmetic 16. int vs float Arithmetic 17. int vs float Division 18. int Division Truncates 19.Division By Zero 20.Division By Zero Example #1 21.Division By Zero Example #2 22.Floating Point Exception 23.Mixed Mode Arithmetic #1 24.Mixed Mode Arithmetic #2 25.Promoting an int to a float 26.Exercise: Writing a Program

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Named Constant & Variable Operands #1 So far, many of the examples of expressions that we’ve looked at have used numeric literal constants as operands. But of course we already know that using numeric literal constants in the body of a program is BAD BAD BAD. So instead, we want to use named constants and variables as operands.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Named Constant & Variable Operands #2 #include int main () { /* main */ const int days_in_a_year = 365; const int hours_in_a_day = 24; const int minutes_in_an_hour = 60; const int seconds_in_a_minute = 60; int year_of_birth, current_year, age_in_seconds; printf("Let me guess your age in seconds!\n"); printf("What year were you born?\n"); scanf("%d", &year_of_birth); printf("What year is this?\n"); scanf("%d", &current_year); age_in_seconds = (current_year - year_of_birth) * days_in_a_year * hours_in_a_day * minutes_in_an_hour * seconds_in_a_minute); printf("I’d guess that your age is about"); printf(" %d seconds.\n", age_in_seconds); } /* main */

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Named Constant & Variable Operands #2 % gcc -o age age.c % age Let me guess your age in seconds! What year were you born? 1946 What year is this? 2003 I’d guess that your age is about seconds.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Constant-Valued Expressions #1 If we have an expression whose terms are all constants (either literal constants or named constants), then we can use that expression in the initialization of a named constant: const float C_to_F_factor = 9.0 / 5.0; const float C_to_F_increase = 32.0; const float C_water_boiling_temperature = 100.0; const float F_water_boiling_temperature = C_water_boiling_temperature * C_to_F_factor + C_to_F_increase;

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Constant-Valued Expressions #2 #include int main () { /* main */ const float C_to_F_factor = 9.0 / 5.0; const float C_to_F_increase = 32.0; const float C_water_boiling_temperature = 100.0; const float F_water_boiling_temperature = C_water_boiling_temperature * C_to_F_factor + C_to_F_increase; printf("Water boils at %f degrees C,\n", C_water_boiling_temperature); printf(" which is %f degrees F.\n", F_water_boiling_temperature); } /* main */

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Constant-Valued Expressions #3 % gcc -o constexpr constexpr.c % constexpr Water boils at degrees C, which is degrees F. Note: In the initialization of a named constant, we CANNOT have an expression whose value is NOT a constant.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Assignments W/O Expressions: Not Very Useful So far, many of the assignment statements that we’ve seen have simply assigned a literal value to a variable: % cat varassn.c #include int main () { /* main */ int x; x = 5; printf("x = %d\n", x); } /* main */ % gcc -o varassn varassn.c % varassn x = 5 Unfortunately, this is not very interesting and won’t accomplish much in an actual real life program. To make a program useful, most of the assignments have to have expressions on the right hand side.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Assignments with Expressions: Crucial % cat triangle_area.c #include int main () { /* main */ const float height_factor = 0.5; float base, height, area; printf("This program calculates the area of a\n"); printf(" triangle from its base and height.\n"); printf("What are the base and height?\n"); scanf("%f %f", &base, &height); area = height_factor * base * height; printf("The area of a triangle of base %f\n", base); printf(" and height %f is %f.\n", height, area); } /* main */ % gcc -o triangle_area triangle_area.c % triangle_area This program calculates the area of a triangle from its base and height. What are the base and height? 5 7 The area of a triangle of base and height is

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Meaning of Assignment w/Expression Suppose that we have an expression on the right hand side of an assignment: x = y + 1; The compiler interprets this statement to mean: “first, evaluate the expression that’s on the right hand side of the equals sign; then, put the resulting value into the variable that’s on the left side of the equals sign.” In the example above, the assignment statement means: “evaluate y + 1, then put the resulting value into x.”

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Assignment w/Expression Example % cat xgetsyplus1.c #include int main () { /* main */ int x, y; y = 5; printf("y = %d\n", y); x = y + 1; printf("x = %d\n", x); } /* main */ % gcc -o xgetsyplus1 xgetsyplus1.c % xgetsyplus1 y = 5 x = 6

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Assignment w/Same Variable on Both Sides Here’s another assignment: x = x + 1; The assignment statement above may be confusing, because it has the same variable, x, on both the left hand side and the right hand side of the equals sign.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Same Variable on Both Sides: Meaning x = x + 1; In general, the compiler interprets an assignment statement to mean: “first, evaluate the expression that’s on the right hand side of the equals sign; then, put the resulting value into the variable that’s on the left hand side of the equals sign.” So, the assignment statement above means: “get the current value of x, then add 1 to it, then place the new value back into x.”

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Same Variable on Both Sides: Example % cat assignself.c #include int main () { /* main */ int x; x = 5; printf("After 1st assignment, x = %d\n", x); x = x + 1; printf("After 2nd assignment, x = %d\n", x); } /* main */ % gcc -o assignself assignself.c % assignself After 1st assignment, x = 5 After 2nd assignment, x = 6

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Single Mode Arithmetic In C, when we have an arithmetic expression whose terms all evaluate to a single data type (e.g., all int -valued terms or all float -valued terms), we call this single mode arithmetic. In C, single mode int arithmetic behaves like single mode float arithmetic most of the time.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring int vs float Arithmetic In C, single mode int arithmetic behaves like single mode float arithmetic most of the time is 12.0 and 5+7 is is -2.0 and 5-7 is *7.0 is 35.0 and 5*7 is 35 But, division is different for int vs float !

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring int vs float Division Division is different for int vs float ! 5.0/7.0 is 0.71 BUT 5/7 is 0 float division in C works the same way that division works in mathematics. But int division is a little bit strange. In int division, the result is truncated to the nearest int immediately less than the mathematical result. Truncate: to cut off (for example, to cut off the digits to the right of the decimal point)

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring int Division Truncates 4.0/ is 1.0 and 4/4 is 1 5.0/4.0 is 1.25 BUT 5/4 is 1 6.0/4.0 is 1.5 BUT 6/4 is 1 7.0/4.0 is 1.75 BUT 7/4 is 1 8.0/4.0 is 2.0 and 8/4 is 2

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Division By Zero Mathematically, division by zero gives an infinite result: c – = ∞ for c ≠ 0 0 Computationally, division by zero causes an error.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Division By Zero Example #1 % cat divbyzeroconst.c #include int main () { /* main */ printf("5 / 0 = %d\n", 5 / 0); } /* main */ % gcc -o divbyzeroconst divbyzeroconst.c % divbyzeroconst Floating exception (core dumped) Note that, in the context of computing, the word exception means “a very dumb thing to do.” As in, “I take exception to that.”

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Division By Zero Example #2 % cat divbyzero.c #include int main () { /* main */ int numerator, denominator; printf("What’s the numerator?\n"); scanf("%d", &numerator); printf("What’s the denominator?\n"); scanf("%d", &denominator); printf("numerator = %d\n", numerator); printf("denominator = %d\n", denominator); printf("numerator / denominator = %d\n", numerator / denominator); } /* main */ % gcc -o divbyzero divbyzero.c % divbyzero What’s the numerator? 5 What’s the denominator? 0 numerator = 5 denominator = 0 Floating exception (core dumped)

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Floating Point Exception % gcc -o divbyzero divbyzero.c % divbyzero What’s the numerator? 5 What’s the denominator? 0 numerator = 5 denominator = 0 Floating exception (core dumped) Note that, in the context of computing, the word exception means “a very dumb thing to do.” As in, “I take exception to that.”

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Mixed Mode Arithmetic #1 In principle, we might like our numeric statements to have either all int -valued terms or all float -valued terms. In practice, we can, and often must, mix int -valued and float -valued terms – literals, named constants, variables and subexpressions – subject to the rule that an operation with operands of both data types has a float result. We call such expressions mixed mode arithmetic.

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Mixed Mode Arithmetic #2 1+2 is 3 BUT is 3.0 and is is BUT is and is 1*2 is 2 BUT 1.0*2 is 2.0 and 1*2.0 is 2.0 1/2 is 0 BUT 1.0/2 is 0.5 and 1/2.0 is 0.5

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Promoting an int to a float For mixed mode arithmetic, we say that an int operand is promoted to float. 1/2 is 0 BUT 1/2.0 is 1.0/2.0 is /(3 / 2) is 4.0 BUT 4.0/(3.0 / 2) is 4.0/(3.0 / 2.0) is 2.666…

CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Exercise: Writing a Program Given a height in miles, convert to height in meters. Specifically, draw a flowchart and then write a C program that: 1.greets the user; 2.prompts the user and then inputs a height in miles; 3.calculates the height in meters; 4.outputs the height in meters. The body of the program must not have any numeric literal constants; all constants must be declared using appropriate identifiers. Don’t worry about comments.