1 / 27

Algorithms

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. Assignment operator =. Value assigning differs from equality

zakrzewski
Download Presentation

Algorithms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  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 cosc175/operators

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

  3. variable = expression • variable -physical location in computer memory • expression • 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

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

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

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

  7. cosc175/operators

  8. Arithmetic Operators cosc175/operators

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

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

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

  12. 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

  13. Modulus % Remainder 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

  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 cosc175/operators

  15. Example 1:Problem Definition 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 output Step 2: define list of actions. Hint: Use verbs, these steps usually involve the input and output defined in step 1 cosc175/operators

  16. 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

  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; } cosc175/operators

  18. Relation cosc175/operators

  19. cosc175/operators

  20. cosc175/operators

  21. cosc175/operators

  22. cosc175/operators

  23. 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 StatementWhat 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;

  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 cosc175/operators

  25. 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

  26. cosc175/operators

  27. cosc175/operators

More Related