195k views
2 votes
Write a program that asks the user to enter five test scores in bash shell script, please.

The program should display a letter grade for each score and the average test score. Design the following functions in the program:

● calcAverage—This function should accept five test scores as arguments and
return the average of the scores.

● determineGrade—This function should accept a test score as an argument
and return a letter grade for the score (as a String), based on the following
grading scale:

Score Letter Grade
90–100 A
80–89 B
70–79 C
60–69 D
Below 60 F

User Nozomi
by
7.8k points

1 Answer

5 votes

Answer:

Here's a possible solution for a bash shell script that prompts the user to enter five test scores and calculates their average, as well as the letter grade for each score based on the determine Grade function:

bashCopy code#!/bin/bash # define determineGrade function determineGrade () { score=$1 if (( score >= 90 && score <= 100 )); then grade="A" elif (( score >= 80 && score <= 89 )); thengrade="B" elif (( score >= 70 && score <= 79 )); then grade="C" elif (( score >= 60 && score <= 69 )); then grade="D" else grade="F" fi echo $grade } # prompt user to enter five test scores read -p "Enter score 1: " score1 read -p "Enter score 2: " score2 read -p "Enter score 3: " score3 read -p "Enter score 4: " score4 read -p "Enter score 5: "score5 # calculate average score average=$(echo "scale=2; ($score1 + $score2 + $score3 + $score4 + $score5) / 5" | bc) # display letter grade and average for each score echo"Score 1: $score1 $(determineGrade $score1)" echo "Score 2: $score2 $(determineGrade $score2)" echo "Score 3: $score3 $(determineGrade $score3)" echo "Score 4: $score4$(determineGrade $score4)" echo "Score 5: $score5 $(determineGrade $score5)" echo"Average score: $average"

In this script, the determineGrade function takes a score as its argument and returns a letter grade based on the grading scale defined in the prompt. The main part of the script prompts the user to enter five test scores, calculates their average using the bc command, and calls the determine Grade function for each score to display the letter grade and average for each score.

User Jon Adams
by
8.2k points