Hi people! I just want to get the sum, max number, average and multiplication of array with n numbers!
I already make the sum, but I tried different operators, ways for others and I can't get the another task without errors! Any help! Im really noobie on this Look!

    #!/bin/bash

    echo Input some numbers, separated by a space please:
    read -a choice

    for a in ${!choice[*]}
    do
        ((sum+=choice[a]))
    done

    echo The result after adding all numbers is: $sum

Hello,

try something like this:

#!/bin/bash
echo "Input  some numbers, separated by a space please:"
read -a choice
$result=0
for a in `echo $choice`
do
    $result=$result + $a
done
echo "The result after adding all numbers is: $result "

The echo $choice may be off a bit you may need just $choice (it has been a while).

Hi Sundown G!

It looks like you've got the sum down pretty well. What have you tried for the max number, average and multiplication options?
Multiplication should be fairly similar to what you're doing with the sum here (try something simple like sum=$((sum*choice[a])), but make sure you set sum=1 before the beginning of the 'for' loop)

Once you have that, average should be easy.

Max number will be a slightly different operation. Just call the first number "high", compare each number to the previous "high", and overwrite "high" if it's greater.

Thanks both really helpful, I did the multiplication option (Thanks Gromit)

Now i did the average but with a little problem for example:(average of 1 and 3 = 2) GOOD! but when i tried to find (average of 1 and 2 = its supposed to display 1.5, NOT 1). What im doing wrong???? Thanks

#!/bin/bash
echo Input some numbers, separated by a space please:
read -a choice

amount=${#choice[@]}

  for a in `seq 0 $amount`;
    do
      sum=$((sum + choice[$a]))
      average=$(($sum / $amount))
  done

   echo The average number is: $average

Nvm I solved previous reply with bc

Well Any ideas for max number???

Start with very low starting value and update with each value that is bigger than current max.

Show us what you've tried, and we might be able to help you figure out how to make it work! :)

Okay, because dangling threads make me crazy when I'm personally searching for something, here's a simple loop that will output the greatest number:

#!/bin/bash
echo Input some numbers, separated by a space please:
read -a choice

# Set "greatest" to the first number
# in the list
greatest=${choice[0]}

for num in ${choice[@]}; do

    # This will compare each number to the previous
    # "greatest" number, and set the "greatest" var
    if [ $num -gt $greatest ]; then
        greatest=$num
    fi

done

# After the loop runs out of numbers, print the "greatest"
echo "The greatest number is: $greatest"

I hope this helps!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.