Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/bin/bash
- function inputMatrix(){
- echo "For Matrix A:-"
- for (( i=0; i<rowA; i++ ))
- do
- for (( j=0; j<columnA; j++ ))
- do
- echo "Value at [$i][$j] : "
- read MatrixA[$i$j]
- done
- done
- echo "For Matrix B:-"
- for (( i=0; i<rowB; i++ ))
- do
- for (( j=0; j<columnB; j++ ))
- do
- echo "Value at [$i][$j] : "
- read MatrixB[$i$j]
- done
- done
- }
- function add(){
- for (( i=0; i<rowA; i++ ))
- do
- for (( j=0; j<columnA; j++ ))
- do
- let MatrixC[$i$j]=${MatrixA[$i$j]}+${MatrixB[$i$j]}
- done
- done
- }
- function sub(){
- for (( i=0; i<rowA; i++ ))
- do
- for (( j=0; j<columnA; j++ ))
- do
- let MatrixC[$i$j]=${MatrixA[$i$j]}-${MatrixB[$i$j]}
- done
- done
- }
- function mul(){
- for (( i=0; i<rowA; i++ ))
- do
- for (( j=0; j<columnB; j++ ))
- do
- sum=0
- for (( k=0; k<columnA; k++ ))
- do
- sum=`expr "$sum+(${MatrixA[$i$k]} * ${MatrixB[$k$j]})"|bc`
- done
- MatrixC[$i$j]=$sum
- done
- done
- }
- function displayMatrix(){
- for (( i=0; i<rowA; i++ ))
- do
- for (( j=0; j<columnB; j++ ))
- do
- echo -n ${MatrixC[$i$j]}" "
- done
- echo ""
- done
- }
- rowA=2
- columnA=2
- rowB=2
- columnB=2
- inputMatrix
- echo -n "1.Add 2. Subtract 3. Multiply: "
- read ip
- case $ip in
- 1):
- add;;
- 2):
- sub;;
- 3):
- mul;;
- esac
- displayMatrix
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement