Advertisement
Shailrshah

Array addition, subtraction and multiplication

Nov 2nd, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 0.56 KB | None | 0 0
  1. #! /bin/bash
  2.  
  3. echo  -n "Enter the size of the arrays: "
  4. read n
  5.  
  6. echo -n "Enter the first array: "
  7. read -a a
  8. if [ ${#a[@]} -ne $n ]; then
  9.     echo "Array not of specified length."
  10.     exit 1
  11. fi
  12.  
  13. echo -n "Enter the second array: "
  14. read -a b
  15. if [ ${#b[@]} -ne $n ]; then
  16.     echo "Array not of specified length."
  17.     exit 1
  18. fi
  19.  
  20. for(( i=0; i<$n; i++ )){
  21.     add[$i]=$((a[i]+b[i]));
  22.     sub[$i]=$((a[i]-b[i]));
  23.     mul[$i]=$((a[i]*b[i]));
  24. }
  25.  
  26. echo "The addition array is "${add[*]}
  27. echo "The subtraction array is "${sub[*]}
  28. echo "The multiplication array is "${mul[*]}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement