**Tom Kelliher, CS 102**

**Feb. 19, 1999**

Internet Timeline.

- Problem Solving.
- Program Design: structural elements.
- Examples.

Structural elements of JavaScript, I/O.

The process of transforming the description of a problem into the solution of that problem by using our knowledge of the problem domain and by relying on our ability to select and use appropriate problem-solving strategies, techniques, and tools.

A few problem examples:

- Calculating a tip.
- Double checking a grocery receipt.
- A web browser.

Details to be determined:

- Inputs/Outputs, their form, their media.
- Special constraints or conditions. (Realtime processing.)
- Computational formulas, equations.

Algorithm:

A sequence of a finite number of steps arranged in a specific logical order which, when executed, produces the solution for a problem.

An algorithm must satisfy the following requirements:

- Input --- usually required.
- Output.
- Unambiguousness --- computers don't accept ambiguity.
- Generality --- solves a
*class*of problems. - Correctness --- correctly solve the given problem.
- Finiteness --- termination.
- Efficiency --- recognition of finite computing resources: CPU cycles, memory.

Pseudocode:

A semiformal, English-like language with a limited vocabulary that can be used to design and describe algorithms.

- Meta-programming language.
- Algorithm representation.

C. Bohm and G. Jacopini proved in 1966 than pseudocode required only three structural elements

A series of steps or statements that are executed in the order they are written in an algorithm.

Example:

name = prompt("What is your name?", ""); document.write("Hello " + name + "!<br><br>"); age = prompt(name + ", how old are you?", ""); birthYear = currentYear - age - 1;

Statements terminate with a ` ;`.

`{`

/`}`

pair grouping for constructing a * compound*
statement:

{ amountDue = overDue + currentBilling + penalty; document.write("You owe: " + amountDue); }

The alternatives of two courses of action only one of which is taken depending on the outcome of a condition, which is either true or false.

if (condition) then_statement; else else_statement;

Structure of `then_part`

, `else_part`

:

- A single statement.
- A set of statements enclosed by
`{`

/`}`

.

if (payment is overdue) { amountDue = pastDue + currentBilling + penalty; document.write("You owe: " + amountDue); } else document.write("You owe: " + currentBilling);

Alternative nested if-else structure element: `else_if`

if (grade < 60) document.write("F"); else if (grade < 70) document.write("D"); else if (grade < 80) document.write("C"); else if (grade < 90) document.write("B"); else document.write("A");

Specifies a block of one or more statements that are repeatedly executed until a condition is satisfied.

while (condition) loop_statement;

sum = 0; while (there are input numbers to sum) { number = prompt("Next number: ", ""); sum = sum + number; } document.write("The sum is: " + sum);

- Design an algorithm that keeps reading numbers until the user enters a zero value, and outputs the sum of the numbers.
- Design an algorithm that keeps reading positive numbers until the user enters a zero value, and determines and outputs the largest number.
- Design an algorithm that reads an arbitrary number of numbers and outputs their arithmetic average.

Thu Feb 18 16:16:51 EST 1999