Lecture 5 -- 1Computer Science I - Martin Hardwick Arithmetic Expressions With Integers rOperators:result is always an integer SymbolNameExampleValue (x.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Other Types Of Data rVariables of type short use half a word (16 bits) to represent a value. l everything.
Advertisements

Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
© 2007 Lawrenceville Press Slide 1 Assignment Statement An assignment statement gives a value to a variable. Assignment can take several forms: x = 5;
Chapter Three Arithmetic Expressions and Assignment Statements
1 Chapter 3:Operators and Expressions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2006 | Last Updated: July 2006 Slide 1 Operators and Expressions.
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
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Computer Science 1620 Arithmetic. C++ Math we have seen how to use numbers in our program to represent data however, we can also manipulate this data.
Computer Science 1620 Other Data Types. Quick Review: checklist for performing user input: 1) Be sure variable is declared 2) Prompt the user for input.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
COMPSCI 125 Spring 2005 ©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 3: Numeric Data *Variables *Numeric data.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
Chapter 3 Numerical Data. Topics Variables Numeric data types Assignment Expressions.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
1 CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
COMPUTER SCIENCE I C++ INTRODUCTION
Objectives You should be able to describe: Data Types
Expressions and Interactivity Chapter 3. 2 The cin Object Standard input object Like cout, requires iostream file Used to read input from keyboard Often.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Fall, 2006Introduction to FORTRAN1 (2 + 2 = ???) Numbers, Arithmetic Nathan Friedman Fall, 2006.
Numeric Types, Expressions, and Output ROBERT REAVES.
CSE1222: Lecture 4The Ohio State University1. Mathematical Functions (1)  The math library file cmath Yes, this is a file with definitions for common.
CSE202: Lecture 4The Ohio State University1 Mathematical Functions.
Chapter 3 Expressions and Interactivity Department of Computer Science Missouri State Univeristy.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
Operators in Python. Arithmetic operators Some operators in Python will look familiar (+, -, *, /) Others are new to you (%, //, **) All of these do work.
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
C++ Programming: Basic Elements of C++.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 9, 2005 Lecture Number: 6.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Arithmetic OperatorOperationExample +additionx + y -subtractionx - y *multiplicationx * y /divisionx / y Mathematical FormulaC Expressions b 2 – 4acb *
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
1 10/3/05CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Simple Functions Writing Reuseable Formulas. Problem Using OCD, design and implement a program that computes the area and circumference of an Australian.
Doing math In java.
D-1 University of Washington Computer Programming I Lecture 4: Arithmetic Expressions © 2000 UW CSE.
2/4/2016Engineering Problem Solving with C++, Second Edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 2 Simple C++ Programs.
Lecture 5: Expressions and Interactivity Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
Programming Fundamentals. Summary of Previous Lectures Phases of C++ Environment Data Types cin and cout.
Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.
1 Lecture Three I/O Formatting and Arithmetic Dr. Sherif Mohamed Tawfik.
Intro. to Computer Programming Eng. Nehal A. Mohamed Spring Semester-2016.
Solving Quadratic Equations by Using the Quadratic Formula (9-5) Objective: Solve quadratic equations by using the Quadratic Formula. Use the discriminant.
Chapter INTRODUCTION Data Types and Arithmetic Calculations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Section 3.2b Arithmetic Operators Fundamentals of Java: AP Computer Science Essentials, 4th Edition Lambert / Osborne.
Today Variable declaration Mathematical Operators Input and Output Lab
Computing and Statistical Data Analysis Lecture 2
Lecture 4: Expressions and Variables
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.
Arithmetic Operator Operation Example + addition x + y
Building Java Programs
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Lecture 4: Expressions and Variables
Arithmetic Operations
Building Java Programs
Building Java Programs
Programming Fundamental-1
Presentation transcript:

Lecture Computer Science I - Martin Hardwick Arithmetic Expressions With Integers rOperators:result is always an integer SymbolNameExampleValue (x = 10, y=3) +addition x + y13 –subtractionx – y 7 *multiplicationx * y30 /quotient x / y 3 %remainderx % y 1 –unary minus –x-10 +unary plus +x 10 rYou can string the operators together to build longer expressions. l use parentheses to specify order of operations l precedence (after sub-expressions in parentheses) –first:unary plus and minus from right to left –second: *, / and % from left to right –third: + and – from left to right

Lecture Computer Science I - Martin Hardwick Arithmetic Expression Examples * / * 3 * (4 + 5) %

Lecture Computer Science I - Martin Hardwick Arithmetic Exprs With Doubles rArithmetic expressions with real numbers (numbers with decimal points) work the same way as with integers, with a few exceptions: l there is no remainder operator (%) l the / operator means divide, computing the answer to many decimal places l the result is a real value rather than an integer value rImportant: Real values are approximate and may contain errors in the last few digits. l about 7 digits of accuracy for type float (one word) l about 14 digits of accuracy for type double (two words) l so programmers today nearly always use doubles rReal values are often represented using scientific notation. l Example: x 10 2

Lecture Computer Science I - Martin Hardwick Mixed Mode Arithmetic Expressions rArithmetic expressions both integers and doubles can get tricky. l if both operands are integers, integer arithmetic is used l if either operand is a double, double arithmetic is used –an integer operand is converted to double for the operation rExample:assume a and b are int variables assume x and y are double variables (a * b + (a - b) * x) / (a + b) int double

Lecture Computer Science I - Martin Hardwick Roots Of A Quadratic Equation //Find roots of Ax 2 + Bx + C = 0 #include using namespace std; int main () { doubleA, B, C, x1, x2, sqrtOfDiscriminant; //get three inputs cout << "Enter the value for A: "; cin >> A; cout << "Enter the value for B: "; cin >> B; cout << "Enter the value for C: "; cin >> C; rThe roots of a quadratic equation Ax 2 + Bx + C = 0 are: rTo compute the roots, we need to ask the user for the values of A, B and C. l the cin statement does this l prompt user to enter data rNote that you can declare more than one variable per line. rThe C++ math library (cmath) contains a square root function. -B ± B 2 - 4AC 2A

Lecture Computer Science I - Martin Hardwick Roots Of A Quadratic Equation (continued) //compute the two solutions sqrtOfDiscriminant = sqrt(B*B- 4*A*C); x1 = (-B+sqrtOfDiscriminant) / (2*A); x2 = (-B-sqrtOfDiscriminant) / (2*A); //display the results cout << "First solution: " << x1 << endl; cout << "Second solution: " << x2 << endl; return 0; } rsqrt is the name of the square root function in the library resource. rWhen a variable appears after the << operator in a cout statement, the value of the variable is displayed. rNote that the parentheses in the computation of x1 and x2 are needed to produce the correct result.

Lecture Computer Science I - Martin Hardwick Common errors rNot initializing a variable before it is used // Author: Martin Hardwick #include using namespace std; int main () { int a, b; a = b + 6; return 0; }

Lecture Computer Science I - Martin Hardwick Increment and Decrement operators rThese operators are an easy way to increase (++) or decrease (--) a value by one. rUsed a lot in For loops int main () { int x, y; x = 5; y = x++; cout << x << y << endl; return 0; } int main () { int x, y; x = 5; y = ++x; cout << x << y << endl; return 0; } Prints 6, 5Prints 6, 6 Post-incrementPre-increment

Lecture Computer Science I - Martin Hardwick Conditional Execution -- Quadratic Formula //Find roots of Ax 2 + Bx + C = 0 #include using namespace std; int main () { doubleA, B, C, x1, x2; doublesqrtOfDiscriminant; //get three inputs cout << "Enter the value for A: "; cin >> A; cout << "Enter the value for B: "; cin >> B; cout << "Enter the value for C: "; cin >> C; rA quadratic equation may have only one root. l the quadratic formula computes this root twice l only want to display the root once rOur program needs some way to check that the two roots are different before displaying both. l this is the purpose of the IF statement in C++ rIF statements are also useful for verifying that the user entered correct data as will be seen in future classes.

Lecture Computer Science I - Martin Hardwick Quadratic Formula (continue) rWe need to create a special case that displays the second root only when it is different from the first. rAn IF statement specifies: l conditions that define the special case l sequence of statements to execute for the special case r!= means not equal rthe braces (i.e., { }) define a block of statements to execute. //compute the two solutions sqrtOfDiscriminant = sqrt( B*B - 4*A*C ); x1 = (-B+sqrtOfDiscriminant)/(2*A); x2 = (-B-sqrtOfDiscriminant)/(2*A); //display the first root cout << "First solution: " << x1 << endl; //display the second root if different if (x1 != x2) { cout << "Second solution: " << x2 << endl; } return 0; }

Lecture Computer Science I - Martin Hardwick IF Statements rSyntax:if ( condition ) { statement1; statement 2;... } rThe condition is any valid logical expression with relational operators and logical operators (more on this later). rThe statements inside the IF statement can be any valid C++ statements including other IF statements. rMeaning: condition statement1; statement2;... true false Handle one special case

Lecture Computer Science I - Martin Hardwick The Condition In An IF Statement rThe condition in an IF statement is any valid logical expression composed of relational and logical operators. rRelational operators: operatordescriptionexampleresult x = 10, y=2 <less thanx < yfalse >greater thanx > y+5true <=less or equalx/2 <= yfalse >=greater or equalx/5 >= ytrue ==equalx-1 == yfalse !=not equalx != ytrue rThe result of a relational operator is either true or false

Lecture Computer Science I - Martin Hardwick The Condition In An IF Statement (continued) rLogical operators: ABorandnot A || BA && B!A falsefalsefalsefalsetrue falsetruetruefalsetrue truefalsetruefalsefalse truetruetruetruefalse rPrecedence for a logical expression: l expressions in parentheses l arithmetic expressions l relational operators l and l or in order from left to right unless otherwise specified

Lecture Computer Science I - Martin Hardwick Example Conditions ( x = 10, y = 20) (x+1 > 0) || (y == x) && (x+y < 25)!x + 1 > 0 || y == x && x + y < 25 (11 > 0) TRUE (20 = = 10) FALSE (30 < 25) FALSE FALSE && FALSE FALSE TRUE || FALSE TRUE (11 > 0) (20 = = 10) (30 < 25) TRUE FALSE