# Socratic programming

PGSS Computer Science Core Slides

A sampling of real-world problems, ranging from the simple to the complex, whose solutions are discovered by the students.

### Problem class Square

• Input: real number x
• Output: the square of x

### Problem class ArrayAverage

(Use ArrayTotal() from Friday. We declared it as

```public static double ArrayTotal(double[] arr) {
...
}
```
in case you forgot.)

• Input: array of real numbers
• Output: average of numbers in array
• Example input:
```+----+----+----+----+----+
| 96 | 87 | 99 | 56 | 92 |
+----+----+----+----+----+
```
• Example output: 430 / 5 = 86.0

### Problem class AbsoluteValue

• Input: real number x
• Output: absolute value of x

### Problem class IsEven

• Input: integer n
• Output: 1 for even n, 0 for others

### Problem class Exponentiate

• Input: real number x and integer n
• Output: the nth power of x
• Example input:
```x == 2.5, n == 2
```
• Example output: 6.25

### Problem class ArrayFind

• Input: array of integers, integer n
• Output: index of first occurrence of n in array
• Example input:
```+----+----+----+----+----+----+
| 56 | 17 |  2 | 42 |  5 | 97 |        n == 42
+----+----+----+----+----+----+
```
• Example output: 3

### Problem class ArrayReplace

• Input: array of characters, character old, character new
• Output: same array, with every occurrence of old replaced with new
• Example input:
```+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 'I' | ' ' | 'l' | 'i' | 'k' | 'e' | ' ' | 'P' | 'G' | 'S' | 'S' |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

old == 'k', new == 'v'
```
• Example output:
```+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
| 'I' | ' ' | 'l' | 'i' | 'v' | 'e' | ' ' | 'P' | 'G' | 'S' | 'S' |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
```

### Problem class IntToBinary

• Input: integer n
• Output: array of integers giving bits of n, ordered as in Homework 1
• Example input: 17
• Example output:
```+---+---+---+---+---+
| 1 | 0 | 0 | 0 | 1 |
+---+---+---+---+---+
```

• Input: two-dimensional arrays of reals a and b
• Output: sum of a and b
• Example input:
```     +----+----+         +----+----+
|  5 |  8 |         |  6 | -5 |
+----+----+         +----+----+
a == |  0 | -1 |    b == |  2 |  1 |
+----+----+         +----+----+
|  2 |  5 |         |  0 |  0 |
+----+----+         +----+----+
```
• Example output:
```     +----+----+
| 11 |  3 |
+----+----+
|  2 |  0 |
+----+----+
|  2 |  5 |
+----+----+
```

### Problem class IsSubstring

• Input: two arrays of characters str and query
• Output: index of first location in str where all of query matches characters of str
• Example input:
```       +-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+
str == | 'I' | ' ' | 'l' | 'i' | 'k' | 'e' | ' ' | 'P' | 'g' | 'h' |
+-----+-----+-----+-----+-----+-----+-----+-----+-----+-----+

+-----+-----+-----+
query == | 'o' | 'v' | 'e' |
+-----+-----+-----+
```
• Example output: 3

### Problem class ArrayMoveToEnd

• Input: array of integers, integer n
• Output: same array, but with every instance of n. shifted to array's end
• Example input:
```+----+----+----+----+----+----+----+
|  5 |  8 | 42 |  1 |  3 |  5 |  6 |
+----+----+----+----+----+----+----+

n == 5
```
• Example output: 430 / 5 = 86.0
```+----+----+----+----+----+----+----+
|  8 | 42 |  1 |  3 |  6 |  5 |  5 |
+----+----+----+----+----+----+----+
```