Spinach - 1 year ago 82

Brainfuck Question

How to calculate average of 2 digits with brainfuck ?

I mean i have two digits for example

`2 3`

and its average is

`2.5`

for

`2 8`

we have

`5.0`

How can this be done?

Answer Source

Something along the lines of:

```
++>++++++++< this is input data
[<+>-] this adds the numbers
<[[->+<][->>+<<]] this does the calculation
```

The approach is to evenly distribute a sum of two numbers. By *evenly distribute* I mean that the difference between values is at most 1 (since there are no floating point numbers, you have to somehow represent e.g. 2.5). Once you have two neighbouring cells containing these values, feel free to do whatever you want with them (you can output it, then decrease numbers from each cell and if you are left with 1, then output ".5" to it).

Of course, the code above may have pointer errors, but it should be enough for a start and to debug. Furthermore, I would be really happy to see a good, efficient solution.