Algorithms computer as the tool process – algorithm

Slides:



Advertisements
Similar presentations
True or false A variable of type char can hold the value 301. ( F )
Advertisements

Data types and variables
Chapter 2 Data Types, Declarations, and Displays
1 Expressions, Operators Expressions Operators and Precedence Reading for this class: L&L, 2.4.
CSci 142 Data and Expressions. 2  Topics  Strings  Primitive data types  Using variables and constants  Expressions and operator precedence  Data.
Objectives You should be able to describe: Data Types
Variable = expression type name; int x;x____________________ int y;y____________________ int z;z____________________.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
CSC 1051 – Data Structures and Algorithms I Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University Course website:
1 INTRODUCTION TO PROBLEM SOLVING AND PROGRAMMING.
CPS120: Introduction to Computer Science Operations Lecture 9.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for.
By: Mr. Baha Hanene Chapter 6. LEARNING OUTCOMES This chapter will cover the learning outcome 02 i.e. 2.Use basic data-types and input / output in C programs.
Tokens in C  Keywords  These are reserved words of the C language. For example int, float, if, else, for, while etc.  Identifiers  An Identifier is.
© 2007 Pearson Addison-Wesley. All rights reserved2-1 Character Strings A string of characters can be represented as a string literal by putting double.
1 CS161 Introduction to Computer Science Topic #6.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 2 September 3, 2009.
CompSci 230 S Programming Techniques
CSE 220 – C Programming Expressions.
Chapter 2 Variables.
Chapter 3 Control Statements
Lecture 4: Expressions and Variables
INSPIRING CREATIVE AND INNOVATIVE MINDS
Building Java Programs
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.
Multiple variables can be created in one declaration
Primitive Data, Variables, Loops (Maybe)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
CMSC201 Computer Science I for Majors Lecture 03 – Operators
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Operators and Expressions
Arrays, For loop While loop Do while loop
Building Java Programs Chapter 2
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Introduction to C++ Programming
Building Java Programs
Building Java Programs
Chapter 2 Variables.
Building Java Programs Chapter 2
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.
Chapter 2: Java Fundamentals
Building Java Programs
Data Types and Expressions
Building Java Programs
Engineering Problem Solving with C++ An Object Based Approach
Engineering Problem Solving with C++ An Object Based Approach
Building Java Programs
Building Java Programs
Building Java Programs
Lecture 4: Expressions and Variables
Building Java Programs
Building Java Programs
Building Java Programs
Building Java Programs
Arithmetic Operators Topics Arithmetic Operators Operator Precedence
Building Java Programs Chapter 2
Building Java Programs
Chapter 2 Variables.
Using C++ Arithmetic Operators and Control Structures
Building Java Programs
Building Java Programs
Data Types and Expressions
Building Java Programs
Data Types and Expressions
Introduction to Python
Presentation transcript:

Algorithms computer as the tool process – algorithm Arithmetic: addition,subtraction,multiplication,division Save information for future use Receive and put out information - i/o Comparison Repeat any group of operations cosc175/operators

Assignment operator = Value assigning differs from equality x = 3 vs if x = 3  variable = expression cosc175/operators

variable = expression variable -physical location in computer memory constant x = 10; x = PI; another variable to which a value has previously been assigned y = x; a formula to be evaluated y = a* x /2 + 3; cosc175/operators

assignment Processor evaluates the right member of an assignment statement to get a single value it places this value in the memory location (variable name) given as the left member. Not a mathematical equation, can't be switched stamp = 14 is a valid assignment statement 14 = stamp is not  The left member of an assignment statement must always be the name of a computer memory location (variable name). It cannot be a constant or formula. cosc175/operators

Invalid assignment 14 = x; a + x = 2 + y; Why? cosc175/operators

Given the declarations: int stuff; string name; int widget; int numWidgets; cosc175/operators

the value 10 is placed in the memory location numWidgets Assignment Statement What happens in memory numWidgets = 10; the value 10 is placed in the memory location numWidgets numWidgets = numWidgets + 1; numWidget is evaluated. Contains a 10, one is added to 10, 11 is stored at the memory location numWidgets name = "toby" the four characters t-o-b-y are stored in the memory location name (note: The single quotes are used as delimiters and are not stored in memory.) stuff = numWidgets; Assuming the value 11 in the memory location numWidgets, this assignment statement places the value 11 in answer (note: Now both stuff and numWidgets have the same value.) widget = numWidgets * 2 the ALU evaluates the expression on the right and places the value 30 in the memory location widget cosc175/operators

Arithmetic Operators Op Operation + Addition - Subtraction * Multiplication / Division pow Exponentiation % Modulus cosc175/operators

Addition + int x; x = x + 1; // this is an increment float item1Price; float item2Price; float total; total = item1Price + item2Price; cosc175/operators

Subtraction - int x; x = x - 1; // this is a decrement float grossPay; float tax; float netPay; netPay = grossPay - tax; cosc175/operators

Multiplication * int x; x = x * 2; float bill; float tip; float totalBill; tip = bill * .20; // could use constant here totalBill = bill + tip; cosc175/operators

Division / Can’t divide by zero integer division yields integer result 5/10 => 0 10/3 => 3 5/10.0 => .5 (float) num1/num2 => float result cosc175/operators

Modulus % Remainder Useful for determining even or odd numbers 10 % 5 => 0 23 % 5 => 3 7 % 2 => 1 58 % 10 => 8 Useful for determining even or odd numbers if (num1 % 2 == 0) cout << “even”; cosc175/operators

Precedence Order of Operations exponentiation multiplication and division addition and subtraction left to right  Z * X * Y => (Z * X ) * Y  evaluate parentheses first innermost first cosc175/operators

Example 1:Problem Definition Read three numbers, add them together and print the total. Step 1: define input and output Step 2: define list of actions. Hint: Use verbs, these steps usually involve the input and output defined in step 1 cosc175/operators

Example 1: Solution Algorithm int main() { int num1; int num2; int num3; int sum; cout << "Input three numbers“; cin >> num1 >> num2 >> num3; sum = num1+num2+num3; cout << "Sum is " << sum; } cosc175/operators

Why is this better? int main() { int num1; int num2; int num3; int sum; cout << "Input three numbers“; cin >> num1 >> num2 >> num3; sum = num1+num2+num3; cout << num1 << " + " << num2 << " + " << num3 << " = " << sum; } cosc175/operators

Relation op operation < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to != Not equal to cosc175/operators

true only if both are true AND && exp1 && exp2 true only if both are true OR || exp1 || exp2 true if either or both are true NOT ! ! exp1 true if exp1 is false cosc175/operators

x y x && y TRUE FALSE cosc175/operators

x y x || y TRUE FALSE cosc175/operators

x !x TRUE FALSE cosc175/operators

Assignment Statement What happens in memory cup = 2; saucer = cup; Assume that: cup, saucer, and plate are integer variables name1 and name2 are string variables hival, lowval, and midval are boolean variables Explain what the processor will do with each of the following assignment statements. Assume the assignment statements are in sequence. (Helpful hint: First, evaluate the expression given the right member and then place that value in the memory location specified as the left member of the assignment statement.) Assignment Statement What happens in memory  cup = 2;  saucer = cup;  plate = 15 * cup – saucer;  cup = cup + cup;  saucer = cup * (plate - saucer);  name1 = “jefferson”;  name2 = name1;  hival = TRUE;  lowval = FALSE ; midval = hival && lowval;  lowval = !(hival || lowval);  hival = !hival;

Trace Trace - simulate the algorithm using known results (desk check)  Most major logic errors occur during the development of the algorithm  test data- simple input cosc175/operators

list variables across the top of the page Include column for output Step through code one line at a time (pretend to be the computer) Fill in variables as they change Fill in output column if appropriate Do for 3 sets of data cosc175/operators

Sample trace (shown one line at a time) num1 num2 num3 sum Output Enter three numbers 1 2 3 6 Sum = 6 cosc175/operators

Sample of 3 traces num1 num2 num3 sum Output 1 2 3 6 Enter three numbers Sum = 6 Sum = 0 -3 -2 -1 -6 Sum = -6 cosc175/operators