1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators.

Slides:



Advertisements
Similar presentations
Chapter 3: Expressions and Interactivity. Outline cin object Mathematical expressions Type Conversion and Some coding styles.
Advertisements

CS 1400 Chapter 2 sections 1, 2, 4 – 6, 8,
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
Announcements Quiz 1 Next Week. int : Integer Range of Typically -32,768 to 32,767 (machine and compiler dependent) float : Real Number (i.e., integer.
1 CS150 Introduction to Computer Science 1 Arithmetic Operators.
1 9/1/06CS150 Introduction to Computer Science 1 What Data Do We Have? CS 150 Introduction to Computer Science I.
© 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5/e Starting Out with C++: Early Objects 5 th Edition Chapter 2 Introduction.
1 9/20/06CS150 Introduction to Computer Science 1 Review: Exam 1.
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
1 September 6, 2005CS150 Introduction to Computer Science I What Actions Do We Have Part 1 CS150 Introduction to Computer Science I.
1 9/25/06CS150 Introduction to Computer Science 1 Nested Ifs, Logical Operators, exit() Page 194.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
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.
Chapter 2: Introduction to C++.
1 9/26/07CS150 Introduction to Computer Science 1 Exponents & Output page & Section 3.8.
COMP102 – Programming Fundamentals I LA2B (Mon 5-7pm) LA2E (Fri 3-5pm) LA2F (Fri 5-7pm) TA: Jackie Lo.
Basic Elements of C++ Chapter 2.
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.
CSC 125 Introduction to C++ Programming Chapter 2 Introduction to C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Seventh.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 2 Introduction to C++
Chapter 3 Processing and Interactive Input. 2 Assignment  The general syntax for an assignment statement is variable = operand; The operand to the right.
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
C++ Programming: Basic Elements of C++.
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
Copyright © 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2: Introduction to C++ Starting Out with C++ Early Objects Sixth.
C++ Programming, Namiq Sultan1 Chapter 3 Expressions and Interactivity Namiq Sultan University of Duhok Department of Electrical and Computer Engineerin.
Expressions and Interactivity. 3.1 The cin Object.
1 09/27/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
PROGRAM ESSENTIALS. TOKENS  SMALLEST UNITS OF A PROGRAM LANGUAGE  Special Symbols  Mathematical Operators  Punctuation  Word Symbols  Key Words.
Programming Fundamentals with C++1 Chapter 3 COMPLETING THE BASICS.
Recap……Last Time [Variables, Data Types and Constants]
CS Jan 2007 Chapter 2 sections 1, 2, 4 – 6, 8,
Introduction to Java Applications Part II. In this chapter you will learn:  Different data types( Primitive data types).  How to declare variables?
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.
Chapter 3 – Variables and Arithmetic Operations. First Program – volume of a box /************************************************************/ /* Program.
12/14/2016CS150 Introduction to Computer Science 1 Announcements  Website is up!   All lecture slides, assignments,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
CS0007: Introduction to Computer Programming Primitive Data Types and Arithmetic Operations.
1 09/10/04CS150 Introduction to Computer Science 1 What Actions Do We Have Part 2.
1 Chapter 3 Numeric Types, Expressions, and Output Dale/Weems/Headington.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
2.1 The Part of a C++ Program. The Parts of a C++ Program // sample C++ program #include using namespace std; int main() { cout
Chapter Topics The Basics of a C++ Program Data Types
Chapter 2: Introduction to C++
BASIC ELEMENTS OF A COMPUTER PROGRAM
Basic Elements of C++.
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.
Basic Elements of C++ Chapter 2.
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Programming Funamental slides
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
Chapter 2: Introduction to C++.
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Arithmetic Operations
C++ for Engineers and Scientists Second Edition
Presentation transcript:

1 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators

//***************************************************************************** // File name: 02Rectangle.cpp // Author: Chadd Williams // Date: 09/08/06 // Assignment: Lab02 Challenge // Purpose: Calculate and display the area and perimeter of a rectangle based on the // length and width entered by a user. //***************************************************************************** #include "stdafx.h" #include using namespace std; // The main function int main() { /*const*/ int LENGTH /* = 8*/; /*const*/ int WIDTH /* = 3*/; int area; int perimeter; cout << "Please enter the width of the rectangle: "; cin >> WIDTH; cout << "Please enter the length of the rectangle: "; cin >> LENGTH; perimeter = (2 * LENGTH) + (2 * WIDTH); area = LENGTH * WIDTH; cout << "The area of the rectangle is: " << area << ".\n” ; cout << “It was calculated with the formula: area = LENGTH * WIDTH."<<endl; cout << "The perimeter of the rectangle is: " << perimeter << ".” ; cout << “\nIt was calculated with the formula: “;

3 9/08/06CS150 Introduction to Computer Science 1 Today  Arithmetic Operators & Expressions o sections 2.15 & 3.2 o Computation o Precedence o Algebra vs C++ o Exponents

4 9/08/06CS150 Introduction to Computer Science 1 Arithmetic Operators  Operators allow us to manipulate data o Unary: operator operand o Binary: operand operator operand OperatorMeaningTypeExample -NegationUnary - 5 =AssignmentBinary rate = 0.05 *MultiplicationBinary cost * rate /DivisionBinary cost / 2 %ModulusBinary cost % 2 +AdditionBinary cost + tax -SubtractionBinary total - tax (left hand side) (right hand side)

5 9/08/06CS150 Introduction to Computer Science 1 Division  grade = 100 / 20; o grade = ?  grade = 100 / 30; o grade = ?

6 9/08/06CS150 Introduction to Computer Science 1 Division  grade = 100 / 40; grade is 2 o If both operands of the division operator are integers, then integer division is performed.  the data type of grade is not considered, why? o We say the integer is truncated. Everything after the decimal point is dropped. No rounding.  grade = / 40; o grade is 2.5 o What data type should grade be declared as?

7 9/08/06CS150 Introduction to Computer Science 1 Modulus  Modulus is the remainder after integer division  grade = 100 % 20; o grade = ?  grade = 100 % 30; o grade = ?  rem = x % n; o What are the possible values for rem ?

8 9/08/06CS150 Introduction to Computer Science 1 Practice  What value is assigned to x? o x = 8 + 3; o x = 8 - 3; o x = 8 * 3; o x = 8 % 3; o x = 8 / 3;

9 9/08/06CS150 Introduction to Computer Science 1 Mathematical Expressions  Complex mathematical expressions are created by using multiple operators and grouping symbols o expression: programming statement that has value o sum = ; o number = 3; expression In these two examples, we assign the value of an expression to a variable

10 9/08/06CS150 Introduction to Computer Science 1 Examples  result = x;  result = 4 + result;  result = 15 / 3;  result = 22 * number;  result = a + b % c;  result = a + b + d / c – q + 42;  cout << “The value: “<< (sum / 2) <<endl;

11 9/08/06CS150 Introduction to Computer Science 1 Operator Precedence  result = a + b + d;  result = / 3; o result = ?  Rules on how to evaluate an arithmetic expression o arithmetic expressions are evaluated left to right o when there are two operators, do them in order of precedence

12 9/08/06CS150 Introduction to Computer Science 1 Operator Precedence Precedence of Arithmetic Operators (Highest to Lowest) (unary negation) - * / % + - If two operators have the same precedence, evaluate them from left to right as they appear in the expression

13 9/08/06CS150 Introduction to Computer Science 1 Practice  * 3  10 / 2 -1  *  %  * 9 / 3 * 4 - 9

14 9/08/06CS150 Introduction to Computer Science 1 Associativity  The order in which an operator works with its operands o left to right or right to left OperatorAssociativity (unary negation) –Right to left * / %Left to right + -Left to right

15 9/08/06CS150 Introduction to Computer Science 1 Grouping!  To override precedence we use grouping symbols, ( ) o average = ( a + b +c ) / 3;  (3 + 12) *  % (3 + 9)  * 9 / ((3 * 4) – 9) o Work from the inside ( ) outward

16 9/08/06CS150 Introduction to Computer Science 1 Algebraic Expressions  4x + 16 o This means: 4 * x + 16 o In C++, we must always write the * int result; int sum; result = 4sum + 8; // syntax error result = 4 * sum + 8;

17 9/08/06CS150 Introduction to Computer Science 1 Exponents  The exponent operator was missing from the list!x 2 y n  C++ does not provide an exponent operator as part of the language  Use pow() in the cmath library #include double area; area = pow(4, 2); // area = 4 2

18 9/08/06CS150 Introduction to Computer Science 1 pow()  pow() is not an operator o it is a function o like main() o double pow(double x, double y) o it takes as arguments two doubles  x and y o it produces a double

19 9/08/06CS150 Introduction to Computer Science 1 Practice // Where are the errors? #include double value, exp, result; int number; result = pow(value,exp); 14 = pow(value,2); number = pow(value,exp); result = 14 + pow(value,99);

20 9/08/06CS150 Introduction to Computer Science 1 Advanced Input & Output (3.1 & 3.8) int value, value2; cout << “Please enter an integer: “; cin >> value; cout << “Please enter two integers”; cout << “ separated by some spaces: “; cin >> value >> value2; cout << value2 << value <<endl; Please enter two integers separated by some spaces:

21 9/08/06CS150 Introduction to Computer Science 1 Advanced Input int value; float dec; char letter; cout << “Please enter one integer, “; cout << “one float and one character: ”; cin >> value >> dec >> letter; cout << value << ‘ ‘ << dec << “ “ ; cout << letter << endl; Please enter one integer, one float and one character: c c

22 9/08/06CS150 Introduction to Computer Science 1 Advanced Output  How can we force output to look a particular way? o Precision of numbers o Spacing around output Here are some floating point numbers: Here is a table of data: 4 cat

23 9/08/06CS150 Introduction to Computer Science 1 Precision of numbers #include #include //New Library! using namespace std; int main() { double number = ; cout << number << endl; // default output cout << fixed << setprecision(1) << number << endl; cout << fixed << setprecision(2) << number << endl; cout << fixed << setprecision(3) << number << endl; cout << fixed << setprecision(4) << number << endl; return 0; } These numbers are rounded! Explore on your own what happens if number is an integer.

24 9/08/06CS150 Introduction to Computer Science 1 Spacing around output #include #include //New Library! #include using namespace std; int main() { double number = ; string name = “cs150”; int integer = 42; cout << setw(6) << name << setw(6) << integer << endl; cout << setw(6) << fixed << setprecision(3) << number; cout << setw(4) << integer <<endl; return 0; } cs A represents a blank space

25 9/08/06CS150 Introduction to Computer Science 1 Summary  Today we have looked at: o Arithmetic Operators & Expressions  Next time we will: o ???  Completed sections 2.15 & 3.2