Calculating Your GPA (BASH script) For this script we are going to build a small
ID: 3871366 • Letter: C
Question
Calculating Your GPA (BASH script)
For this script we are going to build a small tool that will take in an x number of grade points and at the end spit out our average grade points and grade letter. For this script you will need
Name This Script: myGPA
To use a while loop since this loop will run for an x amount of time
Continues to prompt the user to enter their grade points
Stops prompting a user when the user enters 99. Use a ‘break’ to exit the loop
DO NOT INCLUDE THE NUMBER 99 IN YOUR CALCULATIONS
Use the ‘bc’ command since we are dealing with decimals
At the end spit out the average GPA along with the Grade Letter.
Equation: Add Up All The Numbers and then divide by the total numbers inputted (IGNORE THE NUMBER 99)
Use this chart to determine which grade letter to show
To compare floating point types (real numbers): while (( $(echo "$grade != 99" | bc ) ))
To add F.P. Type: sum=$(echo "scale=2;($sum+$grade)" | bc)
Grade Points
Grade Letter
4.00 and up
A
3.00 - 3.99
B
2.00 - 2.99
C
1.00 - 1.99
D
0.99 and below
F
Grade Points
Grade Letter
4.00 and up
A
3.00 - 3.99
B
2.00 - 2.99
C
1.00 - 1.99
D
0.99 and below
F
Explanation / Answer
code:
# Hello World Program in Bash Shell
i=0
startTime=`date +%s`
timeSpan=10
endTime=timeSpan+startTime # fix the start time end time
count=0
sum=0
while (( `date +%s` < endTime )) ; do
echo "enter grade"
read grade
if [ $grade == 99 ]; then
break
fi
#echo $grade
sum=$(echo "scale=2;($sum+$grade)" | bc)
count=$(echo "${count} + 1" | bc)
#echo $sum
done
echo $sum
avg=$(echo "${sum} / ${count}" | bc)
echo "$avg"
if [ $avg -ge 4 ]; then
echo "A"
elif [ $avg -ge 3 ]; then
echo "B"
elif [ $avg -ge 2 ]; then
echo "C"
elif [ $avg -ge 1 ]; then
echo "D"
else
echo "F"
fi
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.