开发者

Calculating average of 2 digits

How to calculate average of 2 digits with brainfuck ?

I mean i have two digits for example 2 3 and its average is 2.开发者_如何学Go5

for 2 8 we have 5.0

How can this be done?


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.


better solution:

,>,                               //input 1 and 2
>++++++[-<--------<-------->>]<<  //decrement both by 48
[->+<]>>++<                       //add them and then put 2 in the third cell(devisor)
[                                 // ... 
>[->+>+<<]
>[-<<-
[>]>>>[<[>>>-<<<[-]]>>]<<]        //integer division (4/2 = 2, 3/2 = 1...)
>>>+ 
<<[-<<+>>] 
<<<]
>[-]>>>>[-<<<<<+>>>>>]            //...
<<<<++++++[-<++++++++>]<.         //increment 28 and print result
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜