Advertisement
Shailrshah

2x2 Matrix Operations

Aug 31st, 2014
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.75 KB | None | 0 0
  1. #!/bin/bash
  2. function inputMat(){
  3.     echo "Enter Matrix A: "
  4.     read -a a
  5.     echo "Enter Matrix B: "
  6.     read -a b
  7. }
  8. function displayMat(){
  9.     for(( i=0; i<4; i++ ))
  10.     do
  11.         if(( i==2 )) # new line for next row
  12.             then echo ""
  13.         fi
  14.         echo -n "${c[i]}  "
  15.     done
  16. }
  17. function add(){
  18.     for(( i=0; i<4; i++ ))
  19.     do
  20.         let c[$i]=${a[$i]}+${b[$i]}
  21.     done
  22. }
  23. function sub(){
  24.     for(( i=0; i<4; i++ ))
  25.     do
  26.         let c[$i]=${a[$i]}-${b[$i]}
  27.     done
  28. }
  29. function mul(){
  30.     let c[0]=${a[0]}*${b[0]}+${a[1]}*${b[2]}
  31.     let c[1]=${a[0]}*${b[1]}+${a[1]}*${b[3]}
  32.     let c[2]=${a[2]}*${b[0]}+${a[3]}*${b[2]}
  33.     let c[3]=${a[2]}*${b[1]}+${a[3]}*${b[3]}
  34. }
  35. inputMat
  36. echo -n "1.Add 2. Subtract 3. Multiply: "
  37. read ip
  38. case $ip in
  39.     1):
  40.         add;;
  41.     2):
  42.         sub;;
  43.     3):
  44.         mul;;
  45. esac
  46. displayMat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement