Arithmetic Expressions Lesson #1 CS1313 Spring 2015 1 Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #1 Outline 2.A Less Simple.

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.
1 C Fundamentals - I Math 130 Lecture # 2 B Smith: Sp05: With discussion on labs, this took 53 minutes. Score 3. Many caveats. B Smith: Sp05: With discussion.
Types and Arithmetic Operators
Introduction to C Programming
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
C Introduction Lesson CS1313 Spring C Introduction Lesson Outline 1.C Introduction Lesson Outline 2. hello_world.c 3.C Character Set 4.C is Case.
1 CSE1301 Computer Programming Lecture 5 C Primitives 2.
1 ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions.
C Programming Basics Lecture 5 Engineering H192 Winter 2005 Lecture 05
Software Lesson #1 CS1313 Spring Software Lesson 1 Outline 1.Software Lesson 1 Outline 2.What is Software? A Program? Data? 3.What are Instructions?
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Expressions An expression is a sequence of operands and operators that reduces to a single value expression operator operand An operator is a language-specific.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1)
Performing Computations C provides operators that can be applied to calculate expressions: example: tax is 8.5% of the total sale expression: tax =
Chapter 4: Operators Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills /1436.
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,
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Numeric Data Types Lesson CS1313 Spring Numeric Data Types Outline 1.Numeric Data Types Outline 2.Data Types 3.Integers in Mathematics 4.Integers.
LESSON 6 – Arithmetic Operators
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 CSE1301 Computer Programming Lecture 5: Components of a C Program (Part 1) Linda M c Iver.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Arithmetic Operations. Review function statement input/output comment #include data type variable identifier constant declaration.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
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.
1 Expressions –Arithmetic Operators (+, -, *, /, %, ++, --) Preprocessor and defined Constants –#define directive Today’s Material.
CSC 107 – Programming For Science. Announcements  Memorization is not important, but…  … you will all still be responsible for information  Instead.
Chapter 2 Overview of C++. 2 Overview  2.1 Language Elements  2.2 Reserved Words & Identifiers  2.3 Data Types & Declarations  2.4 Input/Output 
C Operators. CONTENTS C OPERATORS TYPES OF OPERATOR UNARY BINARY TERNARY ARITHMATIC RELATIONAL LOGICAL.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Operators Precedence - Operators with the highest precedence will be executed first. Page 54 of the book and Appendix B list C's operator precedence. Parenthesis.
Variables & Constants Lesson 2 Outline
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.
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
JavaScript 101 Lesson 3: Variables and Arithmetic.
4. EXPRESSIONS. Display the value of pi, to 5 decimal places, right justified, in 9 columns Read in someone’s height in feet and inches using.
User Defined Functions Lesson 1 CS1313 Spring User Defined Functions 1 Outline 1.User Defined Functions 1 Outline 2.Standard Library Not Enough.
Doing math In java.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
CS1313: Arithmetic Expressions Lesson #2 CS1313 Spring Arithmetic Expressions Lesson #2 Outline 1.Arithmetic Expressions Lesson #2 Outline 2.Named.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
If Lesson 1 CS1313 Fall if Lesson 1 Outline 1.if Lesson 1 Outline 2.Absolute Value 3.Absolute Value Definition 4.Absolute Value Implementation 5.What.
 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.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
OPERATORS IN C CHAPTER 3. Expressions can be built up from literals, variables and operators. The operators define how the variables and literals in the.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
From Algorithms to Programs Both are sets of instructions on how to do a task Algorithm: –talking to humans, easy to understand –in plain (English) language.
ICS103 Programming in C Lecture 4: Data Types, Operators & Expressions
INSPIRING CREATIVE AND INNOVATIVE MINDS
Boolean Data Lesson #1 Outline
Assignment and Arithmetic expressions
Operators and Expressions
Boolean Data Lesson #1 Outline
Boolean Data Lesson #2 Outline
C Operators, Operands, Expressions & Statements
Lecture3.
THE COMPUTE STATEMENT Purpose: performs mathematical calculations
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Arithmetic Expressions Lesson #2 Outline
Boolean Data Outline Boolean Data Lesson Boolean Data Outline
Introduction to C Programming
Arithmetic Expressions Lesson #1 Outline
Presentation transcript:

Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expressions Lesson #1 Outline 1.Arithmetic Expressions Lesson #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 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

Arithmetic Expressions Lesson #1 CS1313 Spring A Less Simple C Program #1 /* ************************************************ *** Program: my_add *** *** Author: Henry Neeman *** *** Course: CS Spring 2015 *** *** Lab: Sec 012 Fridays 1:00pm *** *** Description: Input two integers, compute *** *** their sum and output the result. *** ************************************************ */ #include 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.

Arithmetic Expressions Lesson #1 CS1313 Spring 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.

Arithmetic Expressions Lesson #1 CS1313 Spring A Less Simple C Program #3 /* ************************** * Calculation Subsection * ************************** * * Calculate the sum. */ sum = addend + augend; /* ********************* * Output Subsection * ********************* * * Output the sum. */ 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 equals sign is an arithmetic expression.

Arithmetic Expressions Lesson #1 CS1313 Spring A Less Simple C Program #4 #include 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 equals sign is an arithmetic expression.

Arithmetic Expressions Lesson #1 CS1313 Spring 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? The sum of 1593 and 9832 is

Arithmetic Expressions Lesson #1 CS1313 Spring Flowchart for my_add.c A rectangle denotes an operation other than I/O or branching (for example, calculation).

Arithmetic Expressions Lesson #1 CS1313 Spring Named Constant Example Program % cat circlecalc.c #include int main () { /* main */ const float pi = ; 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 and the area is

Arithmetic Expressions Lesson #1 CS1313 Spring Named Constant Example Program % cat circlecalc.c #include int main () { /* main */ const float pi = ; 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 and the area is

Arithmetic Expressions Lesson #1 CS1313 Spring Tax Program with Named Constants % cat tax1997_named.c #include int main () { /* main */ const float standard_deduction = ; const float single_exemption = ; 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? The 1997 federal income tax on $ was $

Arithmetic Expressions Lesson #1 CS1313 Spring 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.)

Arithmetic Expressions Lesson #1 CS1313 Spring 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: ( )

Arithmetic Expressions Lesson #1 CS1313 Spring 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: ( )

Arithmetic Expressions Lesson #1 CS1313 Spring 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: ( )

Arithmetic Expressions Lesson #1 CS1313 Spring 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: ( )

Arithmetic Expressions Lesson #1 CS1313 Spring 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): && Disjunction (OR): || Parentheses: ( ) We’ll learn about these later.

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

Arithmetic Expressions Lesson #1 CS1313 Spring 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: ( )

Arithmetic Expressions Lesson #1 CS1313 Spring 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: ( )

Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Expression Examples x +x -x x + y x - y x * y x / y x % y x + y - (z % 22) * 7 / cos(theta)

Arithmetic Expressions Lesson #1 CS1313 Spring 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.

Arithmetic Expressions Lesson #1 CS1313 Spring Arithmetic Operations OperationKindOper- ator UsageValue IdentityUnary + none +x Value of x NegationUnary --x Additive inverse of x AdditionBinary +x + y Sum of x and y SubtractionBinary -x – y Difference between x and y MultiplicationBinary *x * y Product of x times y (i.e., x. y ) DivisionBinary /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)

Arithmetic Expressions Lesson #1 CS1313 Spring 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

Arithmetic Expressions Lesson #1 CS1313 Spring 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).

Arithmetic Expressions Lesson #1 CS1313 Spring 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.

Arithmetic Expressions Lesson #1 CS1313 Spring 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.

Arithmetic Expressions Lesson #1 CS1313 Spring Precedence Order Examples = = -4 but (2 - 3) = 1 - (-1) = * = = = 11 but 13 (1 + 2) * = 3 * = = / 2 * 4 = 12 * 4 = 48 but 3 24 / (2 * 4) = 24 / 8 = % 6 / 2 = / 2 = = 7 but % (6 / 2) = % 3 = = 6 but 0 (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.”

Arithmetic Expressions Lesson #1 CS1313 Spring Precedence Order Example: int #1 #include int main () { /* main */ printf(" = %d\n", ); printf("1 - (2 - 3) = %d\n", 1 - (2 - 3)); printf("\n"); printf(" * = %d\n", * 3 + 4); printf("(1 + 2) * = %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(" % 6 / 2 = %d\n", % 6 / 2); printf(" % (6 / 2) = %d\n", % (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.

Arithmetic Expressions Lesson #1 CS1313 Spring Precedence Order Example: int #2 % gcc -o int_expressions int_expressions.c % int_expressions – 3 = (2 - 3) = * = 11 (1 + 2) * = / 2 * 4 = / (2 * 4) = % 6 / 2 = % (6 / 2) = 6 (5 + 4) % (6 / 2) = 0

Arithmetic Expressions Lesson #1 CS1313 Spring Precedence Order Example: float #1 #include int main () { /* main */ printf(" = %f\n", ); printf("1.0 - ( ) = %f\n", ( )); printf("\n"); printf(" * = %f\n", * ); printf("( ) * = %f\n", ( ) * ); 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.

Arithmetic Expressions Lesson #1 CS1313 Spring Precedence Order Example: float #2 % gcc -o real_expressions real_expressions.c % real_expressions = ( ) = * = ( ) * = / 2.0 * 4.0 = / (2.0 * 4.0) =