Arithmetic Expressions Lesson #1 Outline

Similar documents
Constants Lesson Outline

Software Lesson 1 Outline

CS Fall 2007 Homework #5

CS Spring 2018 Homework #5

Boolean Data Outline

for Loop Lesson 3 Outline

Numeric Data Types Outline

Boolean Data Lesson #2 Outline

Operators. Lecture 3 COP 3014 Spring January 16, 2018

What is Software? Software, for our purposes, is just a word meaning programs. CS1313: Software Lesson Fall

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

Arithmetic Expressions Lesson #2 Outline

Computer Programming CS F111

Array Lesson 1 Outline

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Variables and Operators 2/20/01 Lecture #

Operators & Expressions

Expressions. Arithmetic expressions. Logical expressions. Assignment expression. n Variables and constants linked with operators

Characters Lesson Outline

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

CS102: Variables and Expressions

for Loop Lesson 1 Outline

User Defined Functions 1 Outline

Fundamental of Programming (C)

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

Programming and Data Structures

CSE 1001 Fundamentals of Software Development 1. Identifiers, Variables, and Data Types Dr. H. Crawford Fall 2018

Lecture 3 Tao Wang 1

LECTURE 3 C++ Basics Part 2

Chapter 4: Basic C Operators

Outline of Fortran 90 Topics

while Loop Example #1

Arithmetic Expressions in C

Section we will not cover section 2.11 feel free to read it on your own

Chapter 3 Structure of a C Program

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

Chapter 2. Designing a Program. Input, Processing, and Output Fall 2016, CSUS. Chapter 2.1

Chapter 2, Part III Arithmetic Operators and Decision Making

Module 2 - Part 2 DATA TYPES AND EXPRESSIONS 1/15/19 CSE 1321 MODULE 2 1

Operators. Java operators are classified into three categories:

Lecture 3. More About C

Structured programming

Informatica e Sistemi in Tempo Reale

Expression and Operator

Fundamentals of Programming

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Announcements. Lab Friday, 1-2:30 and 3-4:30 in Boot your laptop and start Forte, if you brought your laptop

Absolute Value. if Lesson 1 CS1313 Spring

C Introduction Lesson Outline

Fundamentals of Programming

LESSON 1. A C program is constructed as a sequence of characters. Among the characters that can be used in a program are:

User Defined Functions 2 Outline

for Loop Lesson 2 Outline

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

VARIABLES & ASSIGNMENTS

Basic Assignment and Arithmetic Operators

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

Outline. Performing Computations. Outline (cont) Expressions in C. Some Expression Formats. Types for Operands

Selection Statements. Pseudocode

MA 511: Computer Programming Lecture 2: Partha Sarathi Mandal

Basics of Java Programming

Week 2: Console I/O and Operators Arithmetic Operators. Integer Division. Arithmetic Operators. Gaddis: Chapter 3 (2.14,3.1-6,3.9-10,5.

Overview (4) CPE 101 mod/reusing slides from a UW course. Assignment Statement: Review. Why Study Expressions? D-1

C: How to Program. Week /Mar/05

ENGINEERING 1020 Introduction to Computer Programming M A Y 2 6, R E Z A S H A H I D I

2. Numbers In, Numbers Out

Standard Library Functions Outline

Unit 3. Operators. School of Science and Technology INTRODUCTION

LESSON 2 VARIABLES, OPERATORS, EXPRESSIONS, AND USER INPUT

Le L c e t c ur u e e 2 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Variables Operators

Introduction. Following are the types of operators: Unary requires a single operand Binary requires two operands Ternary requires three operands

Programming for Engineers Introduction to C

Chapter 2 - Introduction to C Programming

UIC. C Programming Primer. Bharathidasan University

CIS133J. Working with Numbers in Java

COP 3275: Chapter 04. Jonathan C.L. Liu, Ph.D. CISE Department University of Florida, USA

c) Comments do not cause any machine language object code to be generated. d) Lengthy comments can cause poor execution-time performance.

Add Subtract Multiply Divide

Structured programming. Exercises 3

CS : Programming for Non-majors, Summer 2007 Programming Project #2: Census Due by 12:00pm (noon) Wednesday June

Lesson 3: Arithmetic & Casting. Pic 10A Ricardo Salazar

CpSc 111 Lab 3 Integer Variables, Mathematical Operations, & Redirection

} Evaluate the following expressions: 1. int x = 5 / 2 + 2; 2. int x = / 2; 3. int x = 5 / ; 4. double x = 5 / 2.

2. Numbers In, Numbers Out

Programming in C++ 5. Integral data types

Expressions and Casting. Data Manipulation. Simple Program 11/5/2013

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

CSE 115. Introduction to Computer Science I

Beginning C Programming for Engineers

More Things We Can Do With It! Overview. Circle Calculations. πr 2. π = More operators and expression types More statements

Will introduce various operators supported by C language Identify supported operations Present some of terms characterizing operators

Arithmetic and IO. 25 August 2017

Week 3 More Formatted Input/Output; Arithmetic and Assignment Operators

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen. Introduction

Programming Language A

The C language. Introductory course #1

Expressions and Casting

Chapter 17. Fundamental Concepts Expressed in JavaScript

Motivations. Chapter 3: Selections and Conditionals. Relational Operators 8/31/18. Objectives. Problem: A Simple Math Learning Tool

Transcription:

Outline 1. Outline 2. A Less Simple C Program #1 3. A Less Simple C Program #2 4. A Less Simple C Program #3 5. A Less Simple C Program #4 6. A Less Simple C Program: Compile & Run 7. Flowchart for my_add.c 8. Named Constant Example Program 9. Named Constant Example Program 10. 1997 Tax Program with Named Constants 11. What is an Expression? #1 12. What is an Expression? #2 13. What is an Expression? #3 14. What is an Expression? #4 15. What is an Expression? #5 16. What is an Expression? #6 17. What is an Arithmetic Expression? #1 18. What is an Arithmetic Expression? #2 19. What is an Arithmetic Expression? #3 20. Arithmetic Expression Examples 21. Unary & Binary Arithmetic Operations 22. Arithmetic Operations 23. Structure of Arithmetic Expressions #1 24. Structure of Arithmetic Expressions #2 25. int-valued & float-valued Expressions 26. Precedence Order 27. Precedence Order Examples 28. Precedence Order Example: int #1 29. Precedence Order Example: int #2 30. Precedence Order Example: float #1 31. Precedence Order Example: float #2 CS1313 Fall 2017 1

A Less Simple C Program #1 /* ************************************************ *** Program: my_add *** *** Author: Henry Neeman (hneeman@ou.edu) *** *** Course: CS 1313 010 Fall 2017 *** *** Lab: Sec 014 Fridays 2:30pm *** *** Description: Input two integers, compute *** *** their sum and output the result. *** ************************************************ */ #include <stdio.h> int main () { /* main */ /* *************************** *** Declaration Section *** *************************** * ***************************** * Named Constant Subsection * ***************************** */ const int program_success_code = 0; /* ***************************** * Local Variable Subsection * ***************************** * * addend: the addend value that the user inputs. * augend: the augend value that the user inputs. * sum: the sum of the addend and the augend, * which is output. */ int addend, augend, sum; Continued on the next slide. CS1313 Fall 2017 2

A Less Simple C Program #2 /* ************************* *** Execution Section *** ************************* * *********************** * Greeting Subsection * *********************** * * Tell the user what the program does. */ printf("i'll add a pair of integers.\n"); /* ******************** * Input subsection * ******************** * * Prompt the user to input the addend & augend. */ printf("what pair of integers do you want to add?\n"); /* * Input the integers to be added. */ scanf("%d %d", &addend, &augend); Continued on the next slide. CS1313 Fall 2017 3

A Less Simple C Program #3 /* ************************** * Calculation Subsection * ************************** * The statement as a whole is an * Calculate the sum. */ assignment statement. sum = addend + augend; /* ********************* The stuff to the right of the equals * Output Subsection * sign is an arithmetic expression. ********************* * * Output the sum. */ printf("the sum of %d and %d is %d.\n", addend, augend, sum); return program_success_code; } /* main */ CS1313 Fall 2017 4

A Less Simple C Program #4 #include <stdio.h> int main () { /* main */ const int program_success_code = 0; int addend, augend, sum; printf("i'll add a pair of integers.\n"); printf("what pair of integers do you want to add?\n"); scanf("%d %d", &addend, &augend); sum = addend + augend; printf("the sum of %d and %d is %d.\n", addend, augend, sum); return program_success_code; } /* main */ The statement as a whole is an assignment statement. The stuff to the right of the single equals sign is an arithmetic expression. CS1313 Fall 2017 5

A Less Simple C Program: Compile & Run % gcc -o my_add my_add.c % my_add I'll add a pair of integers. What pair of integers do you want to add? 5 7 The sum of 5 and 7 is 12. % my_add I'll add a pair of integers. What two integers do you want to add? 1593 09832 The sum of 1593 and 9832 is 11425. CS1313 Fall 2017 6

Flowchart for my_add.c A rectangle denotes an operation other than I/O or branching (for example, calculation). CS1313 Fall 2017 7

Named Constant Example Program % cat circlecalc.c #include <stdio.h> int main () { /* main */ const float pi = 3.1415926; const float diameter_factor = 2.0; const int program_success_code = 0; float radius, circumference, area; printf("i'm going to calculate a circle's\n"); printf(" circumference and area.\n"); printf("what's the radius of the circle?\n"); scanf("%f", &radius); circumference = pi * radius * diameter_factor; area = pi * radius * radius; printf("the circumference is %f\n", circumference); printf(" and the area is %f.\n", area); return program_success_code; } /* main */ % gcc -o circlecalc circlecalc.c % circlecalc I'm going to calculate a circle's circumference and area. What's the radius of the circle? 5 The circumference is 31.415924 and the area is 78.539810. CS1313 Fall 2017 8

Named Constant Example Program % cat circlecalc.c #include <stdio.h> int main () { /* main */ const float pi = 3.1415926; const float diameter_factor = 2.0; const int program_success_code = 0; float radius, circumference, area; printf("i'm going to calculate a circle's\n"); printf(" circumference and area.\n"); printf("what's the radius of the circle?\n"); scanf("%f", &radius); circumference = pi * radius * diameter_factor; area = pi * radius * radius; printf("the circumference is %f\n", circumference); printf(" and the area is %f.\n", area); return program_success_code; } /* main */ % gcc -o circlecalc circlecalc.c % circlecalc I'm going to calculate a circle's circumference and area. What's the radius of the circle? 5 The circumference is 31.415924 and the area is 78.539810. CS1313 Fall 2017 9

1997 Tax Program with Named Constants % cat tax1997_named.c #include <stdio.h> int main () { /* main */ const float standard_deduction = 4150.0; const float single_exemption = 2650.0; const float tax_rate = 0.15; const int tax_year = 1997; const int program_success_code = 0; float income, tax; printf("i'm going to calculate the federal income tax\n"); printf(" on your %d income.\n", tax_year); printf("what was your %d income in dollars?\n", tax_year); scanf("%f", &income); tax = (income - (standard_deduction + single_exemption)) * tax_rate; printf("the %d federal income tax on $%2.2f\n", tax_year, income); printf(" was $%2.2f.\n", tax); return program_success_code; } /* main */ % gcc -o tax1997_named tax1997_named.c % tax1997_named I'm going to calculate the federal income tax on your 1997 income. What was your 1997 income in dollars? 20000 The 1997 federal income tax on $20000.00 was $1980.00. CS1313 Fall 2017 10

What is an Expression? #1 a + b - c * d / e % f (398 + g) * 5981 / 15 % h In programming, an expression is a combination of: Operands Operators Parentheses: ( ) Not surprisingly, an expression in a program can look very much like an expression in math (though not necessarily identical). This is on purpose. NOTE: In C, the only characters you can use for parenthesizing are actual parentheses (unlike in math, where you can also use square brackets and curly braces as well.) CS1313 Fall 2017 11

What is an Expression? #2 a + b - c * d / e % f (398 + g) * 5981 / 15 % h In programming, an expression is a combination of: Operands, such as: Literal constants Named constants Variables Function invocations (which we ll discuss later) Operators Parentheses: ( ) CS1313 Fall 2017 12

What is an Expression? #3 a + b - c * d / e % f (398 + g) * 5981 / 15 % h In programming, an expression is a combination of: Operands Operators, such as: Arithmetic Operators Relational Operators Logical Operators Parentheses: ( ) CS1313 Fall 2017 13

What is an Expression? #4 a + b - c * d / e % f (398 + g) * 5981 / 15 % h In programming, an expression is a combination of: Operands Operators, such as: Arithmetic Operators Addition: + Subtraction: - Multiplication: * Division: / Modulus (remainder): % (only for int operands) Relational Operators Logical Operators Parentheses: ( ) CS1313 Fall 2017 14

What is an Expression? #5 a + b - c * d / e % f (398 + g) * 5981 / 15 % h In programming, an expression is a combination of: Operands Operators, such as: Arithmetic Operators Relational Operators Equal: == Not Equal:!= Less Than: < Less Than or Equal To: <= Greater Than: > Greater Than or Equal To: >= Logical Operators Parentheses: ( ) CS1313 Fall 2017 15

What is an Expression? #6 a + b - c * d / e % f (398 + g) * 5981 / 15 % h In programming, an expression is a combination of: Operands Operators, such as: Arithmetic Operators Relational Operators Logical Operators Negation (NOT):! Conjunction (AND): && We ll learn about these later. Disjunction (OR): Parentheses: ( ) CS1313 Fall 2017 16

What is an Arithmetic Expression? #1 An arithmetic expression (also called a numeric expression) is a combination of: Numeric operands Arithmetic Operators Parentheses: ( ) CS1313 Fall 2017 17

What is an Arithmetic Expression? #2 An arithmetic expression (also called a numeric expression) is a combination of: Numeric operands, such as: int & float literal constants (BAD BAD BAD) int & float named constants (GOOD) int & float variables int-valued & float-valued function invocations Arithmetic Operators Parentheses: ( ) CS1313 Fall 2017 18

What is an Arithmetic Expression? #3 An arithmetic expression (also called a numeric expression) is a combination of: Numeric operands Arithmetic Operators, such as: Identity: + Negation: - Addition: + Subtraction: - Multiplication: * Division: / Modulus (remainder): % (only for int operands) Parentheses: ( ) CS1313 Fall 2017 19

Arithmetic Expression Examples x +x -x x + y x - y x * y x / y x % y x + y - (z % 22) * 7 / cos(theta) CS1313 Fall 2017 20

Unary & Binary Arithmetic Operations Arithmetic operations come in two varieties: unary and binary. A unary operation is an operation that has only one operand. For example: -x Here, the operand is x, the operator is the minus sign, and the operation is negation. A binary operation uses two operands. For example: y + z Here, the operands are y and z, the operator is the plus sign, and the operation is addition. CS1313 Fall 2017 21

Operation Kind Operator Identity Unary + none Arithmetic Operations Usage +x +x Value Value of x Value of x Negation Unary - -x Additive inverse of x Addition Binary + x + y Sum of x and y Subtraction Binary - x y Difference between x and y Multiplication Binary * x * y Product of x times y (i.e., x. y) Division Binary / x / y Quotient of x divided by y (i.e., x y) Modulus (int only) Binary % x % y Remainder of x divided by y (i.e., x - x y. y) CS1313 Fall 2017 22

Structure of Arithmetic Expressions #1 An arithmetic expression can be long and complicated. For example: a + b - c * d / e % f Terms and operators can be mixed together in almost limitless variety, but they must follow the rule that a unary operator has a term immediately to its right and a binary operator has terms on both its left and its right: -a + b - c * d / e % f (398 + g) * 5981 / 15 % h Parentheses can be placed around any unary or binary subexpression: ((-a) + b - c) * d / e % f ((398 + g) * 5981 / 15) % h CS1313 Fall 2017 23

Structure of Arithmetic Expressions #2 Putting a term in parentheses may change the value of the expression, because a term inside parentheses will be calculated first. For example: a + b * c is evaluated as multiply b by c, then add a, but (a + b) * c is evaluated as add a and b, then multiply by c Note: As a general rule, you cannot put two operators in a row (but we ll see exceptions, sort of). CS1313 Fall 2017 24

int-valued & float-valued Expressions An int-valued expression is an expression that, when it is evaluated, has an int result. A float-valued expression is an expression that, when it is evaluated, has a float result. CS1313 Fall 2017 25

Precedence Order In the absence of parentheses that explicitly state the order of operations, the order of precedence (also known as the order of priority) is: first: multiplication and division, left to right, and then second: addition, subtraction, identity and negation, left to right. After taking into account the above rules, the expression as a whole is evaluated left to right. CS1313 Fall 2017 26

Precedence Order Examples 1-2 - 3 = -1-3 = -4 but 1 - (2-3) = 1 - (-1) = 2 1 + 2 * 3 + 4 = 1 + 6 + 4 = 7 + 4 = 11 but (1 + 2) * 3 + 4 = 3 * 3 + 4 = 9 + 4 = 13 24 / 2 * 4 = 12 * 4 = 48 but 24 / (2 * 4) = 24 / 8 = 3 5 + 4 % 6 / 2 = 5 + 4 / 2 = 5 + 2 = 7 but 5 + 4 % (6 / 2) = 5 + 4 % 3 = 5 + 1 = 6 but (5 + 4) % (6 / 2) = 9 % (6 / 2) = 9 % 3 = 0 Rule of Thumb: If you can t remember the precedence order of the operations, use lots of parentheses. But DON T overdo your use of parentheses, because then your code will be write only. CS1313 Fall 2017 27

Precedence Order Example: int #1 #include <stdio.h> int main () { /* main */ printf("1-2 - 3 = %d\n", 1-2 - 3); printf("1 - (2-3) = %d\n", 1 - (2-3)); printf("\n"); printf(" 1 + 2 * 3 + 4 = %d\n", 1 + 2 * 3 + 4); printf("(1 + 2) * 3 + 4 = %d\n", (1 + 2) * 3 + 4); printf("\n"); printf("24 / 2 * 4 = %d\n", 24 / 2 * 4); printf("24 / (2 * 4) = %d\n", 24 / (2 * 4)); printf("\n"); printf(" 5 + 4 % 6 / 2 = %d\n", 5 + 4 % 6 / 2); printf(" 5 + 4 % (6 / 2) = %d\n", 5 + 4 % (6 / 2)); printf("(5 + 4) % (6 / 2) = %d\n", (5 + 4) % (6 / 2)); } /* main */ Notice that a printf statement can output the value of an expression. CS1313 Fall 2017 28

Precedence Order Example: int #2 % gcc -o int_expressions int_expressions.c % int_expressions 1-2 3 = -4 1 - (2-3) = 2 1 + 2 * 3 + 4 = 11 (1 + 2) * 3 + 4 = 13 24 / 2 * 4 = 48 24 / (2 * 4) = 3 5 + 4 % 6 / 2 = 7 5 + 4 % (6 / 2) = 6 (5 + 4) % (6 / 2) = 0 CS1313 Fall 2017 29

Precedence Order Example: float #1 #include <stdio.h> int main () { /* main */ printf("1.0-2.0-3.0 = %f\n", 1.0-2.0-3.0); printf("1.0 - (2.0-3.0) = %f\n", 1.0 - (2.0-3.0)); printf("\n"); printf(" 1.0 + 2.0 * 3.0 + 4.0 = %f\n", 1.0 + 2.0 * 3.0 + 4.0); printf("(1.0 + 2.0) * 3.0 + 4.0 = %f\n", (1.0 + 2.0) * 3.0 + 4.0); printf("\n"); printf("24.0 / 2.0 * 4.0 = %f\n", 24.0 / 2.0 * 4.0); printf("24.0 / (2.0 * 4.0) = %f\n", 24.0 / (2.0 * 4.0)); } /* main */ Again, notice that a printf statement can output the value of an expression. CS1313 Fall 2017 30

Precedence Order Example: float #2 % gcc -o real_expressions real_expressions.c % real_expressions 1.0-2.0-3.0 = -4.000000 1.0 - (2.0-3.0) = 2.000000 1.0 + 2.0 * 3.0 + 4.0 = 11.000000 (1.0 + 2.0) * 3.0 + 4.0 = 13.000000 24.0 / 2.0 * 4.0 = 48.000000 24.0 / (2.0 * 4.0) = 3.000000 CS1313 Fall 2017 31