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

Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Embed Size (px)

Citation preview

Page 1: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 1

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

Page 2: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 2

Assignment operator=

• Value assigning differs from equalityx = 3 vs if x = 3

• variable = expression

Page 3: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

variable = expression

• variable -physical location in computer memory • expression

– constantx = 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 3

Page 4: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 4

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 switchedstamp = 14 is a valid assignment statement14 = 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.

Page 5: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Invalid assignment

14 = x;a + x = 2 + y;Why?

cosc175/operators 5

Page 6: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Given the declarations:

int stuff;

string name;

int widget;

int numWidgets;

cosc175/operators 6

Page 7: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 7

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

Page 8: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 8

Arithmetic OperatorsOp Operation+ Addition

- Subtraction

* Multiplication

/ Division

pow Exponentiation

% Modulus

Page 9: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Addition +

int x;

x = x + 1; // this is an increment

float item1Price;

float item2Price;

float total;

total = item1Price + item2Price;cosc175/operators 9

Page 10: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Subtraction -

int x;

x = x - 1; // this is a decrement

float grossPay;

float tax;

float netPay;

netPay = grossPay - tax;cosc175/operators 10

Page 11: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Multiplication *

int x;

x = x * 2;

float bill;

float tip;

float totalBill;

tip = bill * .20; // could use constant here

totalBill = bill + tip;cosc175/operators 11

Page 12: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

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 12

Page 13: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Modulus %

Remainder10 % 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 13

Page 14: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 14

PrecedenceOrder of Operations

• exponentiation• multiplication and division• addition and subtraction• left to right• Z * X * Y => (Z * X ) * Y• evaluate parentheses first• innermost first

Page 15: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 15

Example 1:Problem Definition

Input

Processing

Output

num1

Input num1, num2, num3

sum

num2

Calculate sum

num3

Display sum

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

Page 16: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 16

Example 1: Solution Algorithmint 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;

}

Page 17: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 17

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;}

Page 18: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 18

Relationop operation

< Less than

<= Less than or equal to

> Greater than

>= Greater than or equal to

== Equal to

!= Not equal to

Page 19: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 19

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

Page 20: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 20

x y x && y

TRUE TRUE TRUE

TRUE FALSE FALSE

FALSE TRUE FALSE

FALSE FALSE FALSE

Page 21: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 21

x y x || y

TRUE TRUE TRUE

TRUE FALSE TRUE

FALSE TRUE TRUE

FALSE FALSE FALSE

Page 22: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 22

x !x

TRUE FALSE

FALSE TRUE

Page 23: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

Assume that: cup, saucer, and plate are integer variablesname1 and name2 are string variableshival, lowval, and midval are boolean variablesExplain 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 memory1. cup = 2;2. saucer = cup;3. plate = 15 * cup – saucer;4. cup = cup + cup;5. saucer = cup * (plate - saucer);6. name1 = “jefferson”;7. name2 = name1;8. hival = TRUE;9. lowval = FALSE ;10. midval = hival && lowval;11. lowval = !(hival || lowval);12. hival = !hival;

Page 24: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 24

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

Page 25: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 25

1. list variables across the top of the page2. Include column for output3. Step through code one line at a time

(pretend to be the computer)4. Fill in variables as they change5. Fill in output column if appropriate6. Do for 3 sets of data

Page 26: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 26

Sample trace (shown one line at a time)

num1 num2 num3 sum Output

Enter three numbers

1 2 3

6

Sum = 6

Page 27: Cosc175/operators1 Algorithms computer as the tool process – algorithm –Arithmetic: addition,subtraction,multiplication,division –Save information for

cosc175/operators 27

Sample of 3 traces

num1 num2 num3 sum Output

1 2 3 6 Enter three numbers

Sum = 6

0 0 0 0 Enter three numbers

Sum = 0

-3 -2 -1 -6 Enter three numbers

Sum = -6