Arithmetic, Functions and Input 9/16/13. Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not.

Slides:



Advertisements
Similar presentations
Computer Programming w/ Eng. Applications
Advertisements

Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Chapter 2 Part B CISS 241. Take a few moments and work with another student to develop an algorithm for a program that will add two whole numbers (integers)
CSE202: Lecture 2The Ohio State University1 Variables and C++ Data Types.
Chapter 3 Assignment and Interactive Input. 2 Objectives You should be able to describe: Assignment Operators Mathematical Library Functions Interactive.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 3: Expressions and Interactivity.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.
ICS 103 Lab 2-Arithmetic Expressions. Lab Objectives Learn different arithmetic operators Learn different arithmetic operators Learn how to use arithmetic.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 3: Expressions and Interactivity.
Introduction to C++ Programming
CSCI 1730 January 17 th, 2012 © by Pearson Education, Inc. All Rights Reserved.
Computer Science 1000 Spreadsheets II Permission to redistribute these slides is strictly prohibited without permission.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter Two: Fundamental Data Types
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Slides by Evan Gallagher Fundamental Data Types 09/09/13.
Computer Programming Fundamental Data Types Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons.
1 Number Types  Every value in Java is either: 1.a reference to an object or 2.one of the eight primitive types  eight primitive types: a.four integer.
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.
CNG 140 C Programming Lecture Notes 2 Processing and Interactive Input Spring 2007.
A First Book of ANSI C Fourth Edition Chapter 3 Processing and Interactive Input.
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.
Input, Output, and Processing
1 CS 1430: Programming in C++. 2 Literal Values Literal values of int Literal values of float
Summary of what we learned yesterday Basics of C++ Format of a program Syntax of literals, keywords, symbols, variables Simple data types and arithmetic.
Formatting, Casts, Special Operators and Round Off Errors 09/18/13.
A Computer Science Tapestry 3.1 Programs that Respond to Input l Programs in chapters one and two generate the same output each time they are executed.
CSE1222: Lecture 2The Ohio State University1. mathExample2.cpp // math example #include using namespace std; int main() { cout
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Big Java by Cay Horstmann Copyright © 2009 by John Wiley & Sons. All rights reserved. int: integers, no fractional part: 1, -4, 0 double : floating-point.
CPS120: Introduction to Computer Science Operations Lecture 9.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 6 September 17, 2009.
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 
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 2 Input, Processing, and Output.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Loops Wrap Up 10/21/13. Topics *Sentinel Loops *Nested Loops *Random Numbers.
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Functions Overview Functions are sequence of statements with its own local variables supports modularity, reduces code duplication Data transfer between.
Expressions and Interactivity. 3.1 The cin Object.
Input Validation 10/09/13. Input Validation with if Statements You, the C++ programmer, doing Quality Assurance (by hand!) C++ for Everyone by Cay Horstmann.
C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved Chapter Two: Fundamental Data Types Slides by Evan Gallagher.
Recap……Last Time [Variables, Data Types and Constants]
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
29 January 2016Birkbeck College, U. London1 Introduction to Programming Lecturer: Steve Maybank Department of Computer Science and Information Systems.
Expressions Version Topics Arithmetic expressions Conversions Operator precedence String class 2.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
Problem Solving and Program Design. Problem Solving Process Define and analyze the problem. Develop a solution. Write down the solution steps in detail.
Variables, Operators, and Expressions
Today Variable declaration Mathematical Operators Input and Output Lab
Chapter Two: Fundamental Data Types
Fundamental Data Types
Chapter 3 Assignment and Interactive Input.
Completing the Problem-Solving Process
Introduction to Programming
Fundamental Data Types
Wednesday 09/23/13.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
Summary of what we learned yesterday
4.3 Arithmetic Operators and Mathematical Functions
Presentation transcript:

Arithmetic, Functions and Input 9/16/13

Arithmetic Operators C++ has the same arithmetic operators as a calculator: * for multiplication: a * b – Not implied e.g. a(b+c) – Not a · b or ab as in math / for division: a / b - Not ÷ or a fraction bar as in math - Doesn't act as a delimiter + for addition: a + b - for subtraction: a – b C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Arithmetic Operators Just as in regular algebraic notation, * and / have higher precedence than + and –. In a + b / 2, the b / 2 happens first. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Uninary Operators Int b = 7; Int a = -b; Uniary + and - have precedence over binary operators In a + - b * 2 the -b happens first. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

The % operator computes the remainder of an integer division. It is called the modulus operator (also modulo and mod) It has nothing to do with the % key on a calculator Integer Division and Remainder C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Integer Division and Remainder Hi, I’m Penny. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Integer Division and Remainder Time to break open the piggy bank. You want to determine the value in dollars and cents stored in the piggy bank. You obtain the dollars through an integer division by 100. The integer division discards the remainder. To obtain the remainder, use the % operator: int pennies = 1729; int dollars = pennies / 100; // Sets dollars to 17 int cents = pennies % 100; // Sets cents to 29 (yes, 100 is a magic number) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Converting Floating-Point Numbers to Integers When a floating-point value is assigned to an integer variable, the fractional part is discarded: double price = 2.55; int dollars = price; // Sets dollars to 2 You probably want to round to the nearest integer. To round a positive floating-point value to the nearest integer, add 0.5 and then convert to an integer: int dollars = price + 0.5; // Rounds to the nearest integer C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

What about this? Inside the parentheses is easy: 1 + (r / 100) But that raised to the n? Powers and Roots C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Powers and Roots In C++, there are no symbols for powers and roots. To compute them, you must call functions. The C++ library defines many mathematical functions such as sqrt (square root) and pow (raising to a power). To use the functions in this library, called the cmath library, you must place the line: #include at the top of your program file. It is also necessary to include using namespace std; at the top of your program file. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Powers and Roots The power function has the base followed by a comma followed by the power to raise the base to: pow(base, exponent) Using the pow function: b * pow(1 + r / 100, n) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Powers and Roots C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Other Mathematical Functions C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Other Mathematical Functions C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved log(x) gives ln x exp(x) is e x

Input Sometimes the programmer needs to get a value from the user. Input value from the user Prompt to ask for value –Prompts are done in output statements Get value from keyboard. –With an input statement – cin C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input The input statement –To read values from the keyboard, you input them from an object called cin. –>> is the extraction operator cin >> bottles; is an input statement. Of course, bottles must be defined earlier. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input You can read more than one value in a single input statement: cout << "Enter the number of bottles and cans: "; cin >> bottles >> cans; The user can supply both inputs on the same line: Enter the number of bottles and cans: 2 6 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input You can read more than one value in a single input statement: cout << "Enter the number of bottles and cans: "; cin >> bottles >> cans; Alternatively, the user can press the Enter key after each input: Enter the number of bottles and cans: 2 6 C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Input Statement C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Write a program. The formula to find the period of a Pendulum is: Using this formula, write a C++ program to calculate the period of a pendulum after asking the user for the length of the string, L in meters. g is the acceleration of gravity in M/sec 2.

Submit the Program To submit a program. 1.Create the program. 2.Make sure it is in a directory by itself. 3.Type this Linux command: submit jjarboe cs The 1 is the number of the program assignment.  For program 2 it will be 2.

Numeric Types & Errors 9/18/13

Numeric Types in C++ In addition to the int and double types, C++ has several other numeric types. For this course you only need to know about int and double C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Numeric Types in C++ C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Numeric Ranges and Precisions The int type has a limited range: On most platforms, it can represent numbers up to a little more than two billion. OverFlow Result of computation is outside the int range, the result overflows. No error message is displayed Incorrect value is stored ovfl.cpp C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Unintended Integer Division If both arguments of / are integers, the remainder is discarded: 7 / 3 is 2, not 2.5 but 7.0 / / / 4 all yield C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Unintended Integer Division It is unfortunate that C++ uses the same symbol: / for both integer and floating-point division. These are really quite different operations. It is a common error to use integer division by accident. Consider this segment that computes the average of three integers: cout << "Please enter your last three test scores: "; int s1; int s2; int s3; cin >> s1 >> s2 >> s3; double average = (s1 + s2 + s3) / 3; cout << "Your average score is " << average << endl; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved    Error

Common Error – Unintended Integer Division What could be wrong with that? Of course, the average of s1, s2, and s3 is ( s1+ s2+ s3) / 3 Here, however, the / does not mean division in the mathematical sense. It denotes integer division because both (s1 + s2 + s3) and 3 are integers. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Unintended Integer Division For example, if the scores add up to 14, the average is computed to be 4. WHAT? Yes, the result of the integer division of 14 by 3 is 4 How many times does 3 evenly divide into 14? Right! That integer 4 is then moved into the floating-point variable average. So 4.0 is stored. That’s not what I want! C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Unintended Integer Division The remedy is to make the numerator or denominator into a floating-point number: double total = s1 + s2 + s3; double average = total / 3; or double average = (s1 + s2 + s3) / 3.0; C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Unbalanced Parentheses Consider the expression (-(b * b - 4 * a * c) / (2 * a) What is wrong with it? The parentheses are unbalanced. This is very common with complicated expressions. ? C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Unbalanced Parentheses Now consider this expression -(b * b - (4 * a * c))) / 2 * a) It is still is not correct. There are too many closing parentheses. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Unbalanced Parentheses – A Solution The Muttering Method Count starting with 1 at the 1 st parenthesis add one for each ( subtract one for each ) - ( b * b - ( 4 * a * c ) ) ) / 2 * a ) If your count is not 0 when you finish, or if you ever drop to -1, STOP, something is wrong OH NO! (still to yourself – careful!) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved (not out loud, of course!)

Common Error – Forgetting Header Files Every program that carries out input or output needs the header. If you use mathematical functions such as sqrt, you need to include. If you forget to include the appropriate header file, the compiler will not know symbols such as cout or sqrt. If the compiler complains about an undefined function or symbol, check your header files. C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved

Common Error – Forgetting Header Files Sometimes you may not know which header file to include. Suppose you want to compute the absolute value of an integer using the abs function. As it happens, this version of abs is not defined in the header but in. How can you find the correct header file? A reference site on the Internet such as: is a great help. (Note: do not attempt to click the link if this slide is being projected.) C++ for Everyone by Cay Horstmann Copyright © 2012 by John Wiley & Sons. All rights reserved