Array Addition Shell Script




Shell script to enter numbers in 2 (2 dimension) arrays. Store it in 3rd array. Ask the user for: 2 arrays additions 2 arrays subtraction 2 arrays multiplication

clear

declare -A arr1 arr2 arr3 arr4

echo "Enter Matrix 1 Value"
for((r=0;r<3;r++))
do
    for((c=0;c<3;c++))
    do
        printf "Enter value of [$r][$c] :"
        read arr1[$r,$c]
    done
done

for((r=0;r<3;r++))
do
    for((c=0;c<3;c++))
    do
        printf ${arr1[$r,$c]}" "
    done
    echo
done

echo "Enter Matrix 2 Value"
for((r=0;r<3;r++))
do
    for((c=0;c<3;c++))
    do
        printf "Enter value of [$r][$c] :"
        read arr2[$r,$c]
    done
done

for((r=0;r<3;r++))
do
    for((c=0;c<3;c++))
    do
        printf ${arr3[$r,$c]}" "
    done
    echo
done

echo "1 For Matrix Addition"
echo "2 For Matrix Subtraction"
echo "3 For Matrix Multipication"
echo
read -p "Enter Your Choice :" ch

for((r=0;r<3;r++))
do
    for((c=0;c<3;c++))
    do
        tot=0
        for((k=0;k<3;k++))
        do
            if(($ch==1))
            then
                arr3[$r,$c]=$((${arr1[$r,$c]} + ${arr2[$r,$c]}))
            elif(($ch==2))
            then
                arr3[$r,$c]=$((${arr1[$r,$c]}-${arr2[$r,$c]}))
            elif(($ch==3))
            then
                tot=$((${arr1[$r,$k]}*${arr2[$k,$c]}))
            else
                echo "invalid Choice"
            fi
        done
        arr4[$r,$c]=$tot;
    done
           
   
done

for((r=0;r<3;r++))
do
    for((c=0;c<3;c++))
    do
        if((ch==3));then
            printf ${arr4[$r,$c]}" "
        else
            printf ${arr3[$r,$c]}" "
        fi
    done
    echo
done

Array Addition Shell Script Array Addition Shell Script Reviewed by Unknown on 6:30 pm Rating: 5

No comments: