Written Assignment - Due: Name _________________________ Programming Project Ins
ID: 3849567 • Letter: W
Question
Written Assignment - Due:
Name _________________________
Programming Project
Instructions: Do all of the following questions. Please comment all lines as to show me what is happening.
Also make sure each script runs until the user decides to exit. Use your BASH Shell. In addition to
submitting a written copy of the code via the dropbox you MUST also create a folder in your UNIX account
named, project. In that folder should be all of the scripts you wrote for the +nal project, named +nal1,
+nal2, etc. You must also give me read and execute permissions. If you do not submit your code and allow
me to access your scripts you will not receive credit for the +nal project. Programs written in other
programming languages will not be accepted. Finally, all scripts MUST loop until the user is done using it.
1. Create a program that will ask the user if they want to add, subtract, multiply or divide two user inputted
numbers. Then give the response as follows:
The sum of number1 and number 2 is: Answer
The dierence of number1 and number2 is: Answer
The product of number1 and number2 is: Answer
The quotient of Number1 and Number2 is: Answer
Please note: ONLY IF the user enters 0 as the second number, let the user know that if they choose divide
that a division by zero error will occur.
2. Create a program that will allow a user to type in a number, while the program outputs that number in
words. Example, if you typed in 21, the computer would respond with twenty-one. Please allow the program
to work for 0-30. If a number typed in is not in that range, be sure to let the user know.
3. Write an If Statement that will allow the user to type in a month, then the program will respond with the
holiday in that month. (You only need to list 1 holiday per month.) If there is no holiday, then have the
computer respond there is no holiday in this month. Also let the user know if they typed a month that does
not exist.
4. Write a program that will allow a user to input as many numbers as the user wants (use "q" as the choice
that ends the user input). The program will then respond:
Highest Number: Answer
Lowest Number: Answer
Sum of the numbers: Answer
Average of the numbers: Answer
5. Create a Number guessing game. Have the computer pick a number from 0-60 using the Random
Number Generator I provided. Use the following code as the random number generator.
#Random Number Generator
RANGE=60
num=$RANDOM
let "num %= $RANGE"
echo $num
#RANGE=60 sets the RANDOM number from 0-60
#the echo $num prints the number
Then allow the user to guess at the number until the user gets it right. Please include the following:
a) Keep a counter of how many guesses were made, and then tell the user how many guesses it took.
b) As the user guesses, tell the user whether the guess is too high or too low.
6. Write a program that will allow a user to type in a number, then the number will either count up to the
number provided by the user or count down from that number. In addition, create a menu that will let the
user choose which counter they wish to use. In both cases that counter will either count down to 0, or start
counting from 0.
Instructions: Enter or paste your written work and/or click "Attachments" to upload
Explanation / Answer
NOTE: Out of 6 questions i have answered four questions and the rest i will share by tomorrow EOD.
Code.1)
#!/bin/bash
# Program to perform arithmetic operations like add, subtract, multiply and divide for the two numbers given
# reading input from user
echo "Enter number1? "
read num1
echo "Enter number2? "
read num2
# displaying the menu options
echo "Arithmetic Operations Menu"
echo "--------------------------"
echo "1. Addition"
echo "2. Subtract"
echo "3. Multiply"
echo "4. Divide"
echo "Enter the operation you want to do(1/2/3/4)? "
read op
# carrying out the operation based on the choice
if [ $op -eq 1 ]
then
sum=`expr $num1 + $num2`
echo "The sum of number1 and number2 is: $sum"
elif [ $op -eq 2 ]
then
diff=`expr $num1 - $num2`
echo "The difference of number1 and number2 is: $diff"
elif [ $op -eq 3 ]
then
prod=`expr $num1 * $num2`
echo "The product of number1 and number2 is: $prod"
elif [ $op -eq 4 ]
then
if [ $num2 -eq 0 ]
then
echo "Division by zero error will occur as number2 is 0. Do you want to continue(Y/N)? "
read choice
choice=`echo $choice|tr "[a-z]" "[A-Z]"`
if [ $choice == 'Y' ]
then
div=`expr $num1 / $num2`
echo "The division of number1 and number2 is: $div"
elif [ $choice == 'N' ]
then
echo "division is not continued as requested..."
else
echo "Wrong option. Please try options 'Y' or 'N'"
fi
else
div=`expr $num1 / $num2`
echo "The division of number1 and number2 is: $div"
fi
else
echo "Wrong option entered. You have to enter 1 or 2 or 3 or 4. Please try again"
fi
Execution and output:
Unix Terminal> ./math.sh
Enter number1?
10
Enter number2?
28
Arithmetic Operations Menu
--------------------------
1. Addition
2. Subtract
3. Multiply
4. Divide
Enter the operation you want to do(1/2/3/4)?
1
The sum of number1 and number2 is: 38
Unix Terminal> ./math.sh
Enter number1?
10
Enter number2?
8
Arithmetic Operations Menu
--------------------------
1. Addition
2. Subtract
3. Multiply
4. Divide
Enter the operation you want to do(1/2/3/4)?
2
The difference of number1 and number2 is: 2
Unix Terminal> ./math.sh
Enter number1?
30
Enter number2?
4
Arithmetic Operations Menu
--------------------------
1. Addition
2. Subtract
3. Multiply
4. Divide
Enter the operation you want to do(1/2/3/4)?
3
The product of number1 and number2 is: 120
Unix Terminal> ./math.sh
Enter number1?
120
Enter number2?
4
Arithmetic Operations Menu
--------------------------
1. Addition
2. Subtract
3. Multiply
4. Divide
Enter the operation you want to do(1/2/3/4)?
4
The division of number1 and number2 is: 30
Unix Terminal> ./math.sh
Enter number1?
10
Enter number2?
0
Arithmetic Operations Menu
--------------------------
1. Addition
2. Subtract
3. Multiply
4. Divide
Enter the operation you want to do(1/2/3/4)?
4
Division by zero error will occur as number2 is 0. Do you want to continue(Y/N)?
Y
expr: division by zero
The division of number1 and number2 is:
Unix Terminal> ./math.sh
Enter number1?
10
Enter number2?
0
Arithmetic Operations Menu
--------------------------
1. Addition
2. Subtract
3. Multiply
4. Divide
Enter the operation you want to do(1/2/3/4)?
4
Division by zero error will occur as number2 is 0. Do you want to continue(Y/N)?
N
division is not continued as requested...
Code.2)
#!/bin/bash
# program to convert numbers to words in the range of 0-30
# declaring an array of number words from zero to nineteen
declare -a words
words=(zero one two three four five six seven eight nine ten eleven twelve thirteen fourteen fifteen sixteen seventeen eighteen nineteen)
# Asking user to enter a number from 0-30
echo "Enter a number from the range of 0-30? "
read num
# if number less than 19
if [ $num -lt 19 ]
then
echo "Number $num in words is : ${words[$num]}"
# if number greater than 20 and less than 30
elif [ $num -ge 20 ] && [ $num -lt 30 ]
then
# getting the remainder so we know about other word.
# for example if number is 21, snum variable below will be 1 that means we have to print "twenty" and first index of array words i.e. one
snum=`expr $num % 10`
if [ $snum -ne 0 ]
then
echo "Number $num in words is : Twenty-${words[$snum]}"
else
# if number is 20
echo "Number $num in words is : Twenty"
fi
# if number is 30
elif [ $num -eq 30 ]
then
echo "Number $num in words is : Thirty"
# if number is not in the range of 0-30 this will execute
else
echo "Enter a number only from the range of 0-30"
fi
Execution and output:
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
6
Number 6 in words is : six
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
9
Number 9 in words is : nine
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
10
Number 10 in words is : ten
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
14
Number 14 in words is : fourteen
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
18
Number 18 in words is : eighteen
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
19
Enter a number only from the range of 0-30
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
20
Number 20 in words is : Twenty
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
28
Number 28 in words is : Twenty-eight
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
30
Number 30 in words is : Thirty
Unix Terminal> ./numToWord.sh
Enter a number from the range of 0-30?
40
Enter a number only from the range of 0-30
Code.4)
#!/bin/bash
# program to get highest, lowest, sum and average of given numbers
# initializing the variables like sum, count, flag to 0
sum=0
count=0
flag=0
# running an infinite loop
while true
do
# asking user to enter input
echo "Enter a number? "
read num
# initializing max, min variables to the first number entered
if [ $flag -eq 0 ]
then
max=$num
min=$num
flag=1
fi
# if user enters the choice as q, break out from the menu
num=`echo $num|tr "[A-Z]" "[a-z]"`
if [ $num == 'q' ]
then
break
fi
# getting max number out of given numbers
if [ $num -gt $max ]
then
max=$num
fi
# getting min number out of given numbers
if [ $num -lt $min ]
then
min=$num
fi
# calculating sum and count of numbers
sum=`expr $sum + $num`
count=`expr $count + 1`
done
# displaying the result
avg=`expr $sum / $count`
echo "Highest Number: $max"
echo "Lowest Number: $min"
echo "Sum of the numbers: $sum"
echo "Average of the numbers: $avg"
Execution and output:
Unix Terminal> ./operations.sh
Enter a number?
10
Enter a number?
20
Enter a number?
90
Enter a number?
18
Enter a number?
17
Enter a number?
29
Enter a number?
30
Enter a number?
40
Enter a number?
55
Enter a number?
q
Highest Number: 90
Lowest Number: 10
Sum of the numbers: 309
Average of the numbers: 34
Code.6)
#!/bin/bash
# ask user to enter a number
echo "Enter a number? "
read num
# displaying menu options and asking their choice
echo "Counter Menu"
echo "------------"
echo "1. Count up to the number"
echo "2. Count down to zero"
echo "Enter your choice(1/2)? "
read choice
# if choice is 1, start counting from 1 to num
if [ $choice -eq 1 ]
then
i=0
while [ $i -le $num ]
do
echo "Counting $i"
i=`expr $i + 1`
done
# if choice is 2, start counting down from number to zero
elif [ $choice -eq 2 ]
then
while [ $num -ge 0 ]
do
echo "Counting $num"
num=`expr $num - 1`
done
else
echo "Invalid choice. Enter only 1 or 2 as options. Try again..."
fi
Execution and output:
Unix Terminal> ./countDnUp.sh
Enter a number?
10
Counter Menu
------------
1. Count up to the number
2. Count down to zero
Enter your choice(1/2)?
1
Counting 0
Counting 1
Counting 2
Counting 3
Counting 4
Counting 5
Counting 6
Counting 7
Counting 8
Counting 9
Counting 10
Unix Terminal> ./countDnUp.sh
Enter a number?
10
Counter Menu
------------
1. Count up to the number
2. Count down to zero
Enter your choice(1/2)?
2
Counting 10
Counting 9
Counting 8
Counting 7
Counting 6
Counting 5
Counting 4
Counting 3
Counting 2
Counting 1
Counting 0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.