Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Simple straight forward attempt, with some issues:</p> <pre><code>#!/bin/bash echo "Enter some numbers separated by spaces" read numbers # Initialise min and max with first number in sequence for i in $numbers; do min=$i max=$i break done total=0 count=0 for i in $numbers; do total=$((total+i)) count=$((count+1)) if test $i -lt $min; then min=$i; fi if test $i -gt $max; then max=$i; fi done echo "Total: $total" echo "Avg: $((total/count))" echo "Min: $min" echo "Max: $max" </code></pre> <p>Also tested with /bin/sh, so you don't actually need bash, which is a much larger shell. Also note that this only works with integers, and average is truncated (not rounded).</p> <p>For floating point, you could use bc. But instead of dropping into a different interpreter multiple times, why not just write it in something a bit more suitable to the problem, such as python or perl, eg in python:</p> <pre><code>import sys from functools import partial sum = partial(reduce, lambda x, y: x+y) avg = lambda l: sum(l)/len(l) numbers = sys.stdin.readline() numbers = [float(x) for x in numbers.split()] print "Sum: " + str(sum(numbers)) print "Avg: " + str(avg(numbers)) print "Min: " + str(min(numbers)) print "Max: " + str(max(numbers)) </code></pre> <p>You could embed it in bash using a here document, see this question: <a href="https://stackoverflow.com/questions/2128949/how-to-pipe-a-here-document-through-a-command-and-capture-the-result-into-a-vari">How to pipe a here-document through a command and capture the result into a variable?</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload