Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

ASSIGNAEN ter 11 Menu-Driven Programs Algorithm Workbench 1. Design an algorithm

ID: 3727919 • Letter: A

Question

ASSIGNAEN ter 11 Menu-Driven Programs Algorithm Workbench 1. Design an algorithm that displays the following menu, gets the user's selection, and validates the selection. Main Menu 1. Open a new document. 2. Close the current document. 3. Print the current document. 4. Exit the program. Enter your selection. 2. Design a case structure that can be used with the algorithm you designed for question 1. The case structure should call a module named openDocument if the user selected item 1, should call a module named closeDocument if the user selected item 2, and should call a module named printDocument if the user selected item 3. 3. Put the algorithms that you designed for questions 1 and 2 together inside a loop that redisplays the menu after the user's selected operation is performed, or exits if the user selects item 4 from the menu. 4. Look for ways to modularize the algorithm that you designed for question 3 and modify it accordingly.

Explanation / Answer

(1) Psuedocode/Algorithm

Declare selection as integer

Display "Menu"

Display "1. Open a new document."

Display "2. Close the current document."

Display "3. Print the current document."

Display "4. Exit the program."

Display "Enter your selection"

Read value into the variable selection

IF selection is (greater than or equal to one) and (less than or equal to 4)

IF selection is 1

Do Open a new document

Else IF selection is 2

Do Close the current document

Else IF selection is 3

Do Print the current document

Else IF selection is 4

Do Exit the program

Else

Display "Selection is not valid, Please re-enter a valid value for appropiate Selection"

Read value into variable selection

End IF

(2) Psuedocode/Algorithm

select case selection


case 1
if not FileExists( filename ) then
ShowMessage( "Invalid Filename, please try once again.")
else
openDocument(filename)
end if

case 2
closeDocument

case 3
if not FileExists( filename ) then
ShowMessage( "Invalid Filename, please try once again.")
else
printDocument(filename)

end if


case "Exit"
Application.End

end select

(3) Algorithm/Psuedocode

Declare selection as int

Display "Menu"

Display "1. Open a new document."

Display "2. Close the current document."

Display "3. Print the current document."

Display "4. Exit the program."

Display "Enter your selection"

Read value into the variable selection

IF selection is (greater than or equal to one) and (less than or equal to 4)

select case selection
case 1
if not FileExists( filename ) then
ShowMessage( "Invalid Filename, please try once again.")
else
openDocument(filename)
end if

case 2
closeDocument

case 3
if not FileExists( filename ) then
ShowMessage( "Invalid Filename, please try once again.")
else
printDocument(filename)
end if

case "Exit"
Application.End

end select


else

Display "Selection is not valid, Please re-enter a valid value for appropiate selection"

Read value into variable selection

End IF

(4) Algorithm/Psuedocode

// A program that displays the following menu:
geometry calculator
1. calculate the area of a circle
2. calculate the area of a rectangle
3. calculatle the area of a triangle
4. quit //

REPEAT

SELECT:

PRINT ‘Geometry Calculator’
PRINT ‘1. Calculate the area of a circle’
PRINT ‘2. Calculate the area of a rectangle’
PRINT ‘3. Calculate the area of a triangle’
PRINT ‘4. Quit’

PRINT ‘Enter your choice’
READ choice

IF selection is NOT 1,2,3,or 4
PRINT ‘You entered an invalid choice. Please enter again.’
GOTO SELECT
END IF

case 1: RADIUS:
PRINT ‘Enter radius’
READ radius
IF radius is NOT a +ve integer
PRINT ‘You entered an invalid radius. Please enter again.’
GOTO RADIUS
END IF

circleArea =3.14 x radius x radius
PRINT circleArea
clearScreen()
GOTO SELECT

case 2: LENGTH:
PRINT ‘Enter length’
READ length
IF length is NOT a +ve integer
PRINT ‘You entered an invalid length. Please enter again.’
GOTO LENGTH
END IF

BREADTH:
PRINT ‘Enter breadth’
READ breadth
IF breadth is NOT a +ve integer
PRINT ‘You entered an invalid breadth. Please enter again.’
GOTO BREADTH
END IF

rectangleArea =length x breadth
PRINT rectangleArea
clearScreen()
GOTO SELECT

case 3: BASE:
PRINT ‘Enter base’
READ base
IF base is NOT a +ve integer
PRINT ‘You entered an invalid base. Please enter again.’
GOTO BASE
END IF

HEIGHT:
PRINT ‘Enter height’
READ height
IF height is NOT a +ve integer
PRINT ‘You entered an invalid height. Please enter again.’
GOTO HEIGHT
END IF

triangleArea =1/2 x base x height
PRINT triangleArea
clearScreen()
GOTO SELECT

UNTIL choice is NOT EQUAL TO 4