Home > Net >  How to print sum of user terms that was produced using a formula where user selected variables
How to print sum of user terms that was produced using a formula where user selected variables

Time:11-30

So I admit, I am a complete noob with bash scripting and this assignment is completely dominating me. It is stated above. I believe I managed to figure out the first part in regard to the user input, printing out the amount of terms, and carrying out the correct operation. However, feel free to make suggestions if you notice an error or something that is not best practice. What I am struggling to figure out how to print the sum of the terms that the user has input. Any assistance is appreciated, thank you. My code this far along which is only tasked 1 is listed below; thanks in advance.

Task 3: Find the terms of a sequence given by the rule Term = an² bn c, where a and b and c are integers specified by the user and n is a positive integer. This task should give the user two options:

  • Option 1) Find a limited number of terms of the sequence and print them in order (for example, if the user choses a=3, b=4, and c=1 the first few terms of this sequence are: 8, 21, 40, 65, 96… ). The user also specifies how many terms the program should print. In addition, the program should print the sum of the terms found.
  • Option 2) Find a term in a position chosen by the user and determine whether this term is a multiple of a number chosen also by the user. For example, for the above sequence where a=3, b=4 and c=1, if the user requires to print the 10th term and to check whether this term is a multiple of 3, the program should print:
    341 is not a multiple of 3.
#!/bin/bash

#user selects values for a, b, c
read -p "Please provide value to a:" a
read -p "Please provide value to b:" b
read -p "Please provide value to c:" c

#menu begins
echo "SELECT ONE OPTION";
echo "1. Option1- Find Limited Number of terms of the sequence"
echo "2. Option2- Find a term in a position"
echo -n "Enter your menu choice [1-2]:"
#menu ends

read choice
case $choice in
  1) read -p "please provide number of terms to print:" num
  n=1
  arr=()
  while [ $n -le $num ]
  do
   echo term=$(($a*$n^2 $b*$n $c))
  n=$(( n   1))
  arr =("$term")
  done
  ;;

After 10 hours of trying to figure out how to execute this task, I've tried quite a few things. However, as I stated I was a noob, I believe they were all just from lack of experience and definitely errors on my end.

CodePudding user response:

Good practice is to only continue with validated inputs.

You should normally include logic similar to the following code segment for each such input:

a=""
while [ -z "${a}" ]
do
    echo ""
    read -p "Please provide value to a => " a
    if [ -n "${a}" ]
    then
        if [ $a -lt 0 ]
        then
            echo "\t Invalid integer input.  Please try again ..."
            a=""
        fi
    fi
done

Of course, the validation conditions would fit the context.

CodePudding user response:

This version of the script addresses the issue of incorrect operator for the exponential.

It also makes progressive and more explicit reporting of the assignments (development troubleshooting practices until logic is proven).

The way you are trying to assign the value to "term" is improper. You cannot embed that into an echo statement. It must stand on its own before doing the echo.

Again, I prefer explicit assignment of individual terms in the array.

#!/bin/bash
#Question:  https://stackoverflow.com/questions/74618838/how-to-print-sum-of-user-terms-that-was-produced-using-a-formula-where-user-sele/74620056#74620056
get_a(){
    a=""
    while [ -z "${a}" ]
    do
        echo ""
        read -p "Please provide value to a => " a
        if [ -n "${a}" ]
        then
            if [ $a -lt 0 ]
            then
                echo -e "\t Invalid integer input.  Please try again ..."
                a=""
            fi
        fi
    done
}
#get_a
a=3
b=4
c=1
# Expect Sequence:  8, 21, 40, 65, 96… 
#read choice
choice=1
case $choice in
    1 ) #read -p "How many terms would you like to print => " num
        num=3
        n=1
        arr=()
        while [ ${n} -le ${num} ]
        do
            printf "\n\t n = ${n} ...\n"
            printf "\t\t$(( ${a}*${n}**2 )) \n\t\t$(( ${b}*${n} )) \n\t\t$(( ${c} ))\n"
            term=$(( ${a}*${n}**2   ${b}*${n}   ${c} ))
            printf "\t term = ${term} ...\n"
            arr[${n}]=${term}
            n=$(( n 1 ))
        done
        echo ""
        for i in ${!arr[@]};
        do
            echo " arr[${i}] = ${arr[${i}]}"
        done
        ;;
    * ) ;;
esac

Session output looks like this:

ericthered@OasisMega1:/0__WORK$ ./test_61.sh

     n = 1 ...
        3 
        4 
        1
     term = 8 ...

     n = 2 ...
        12 
        8 
        1
     term = 21 ...

     n = 3 ...
        27 
        12 
        1
     term = 40 ...

 arr[1] = 8
 arr[2] = 21
 arr[3] = 40
ericthered@OasisMega1:/0__WORK$ 
  • Related