# Problem Solving & Program Design

Tom Kelliher, CS 102

Feb. 19, 1999

### From Last Time

Internet Timeline.

### Outline

1. Problem Solving.

2. Program Design: structural elements.

3. Examples.

### Coming Up

Structural elements of JavaScript, I/O.

# Problem Solving

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:

1. Calculating a tip.
2. Double checking a grocery receipt.
3. A web browser.
Inputs? Calculations? Outputs?

## Problem Analysis

Details to be determined:

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

# Algorithm Design and Representation

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:

1. Input --- usually required.
2. Output.
3. Unambiguousness --- computers don't accept ambiguity.
4. Generality --- solves a class of problems.
5. Correctness --- correctly solve the given problem.
6. Finiteness --- termination.
7. 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.

## Pseudocode Structural Elements

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

### The Sequence Control Structure

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 Selection Control Structure

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");
document.write("D");
document.write("C");
document.write("B");
else
document.write("A");
```

### The Repetition Control Structure

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);
```

# Example Problems

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

Thomas P. Kelliher
Thu Feb 18 16:16:51 EST 1999
Tom Kelliher