133k views
3 votes
Topic: Pseudo-codes

Example 5
A shop sells books, maps and magazines. Each item is identified by a unique 4 –
digit code. All books have a code starting with a 1, all maps have a code starting
with a 2 and all magazines have a code beginning with a 3. The code 9999 is used
to end the program.
Write an algorithm using pseudocode which input the codes for all items in stock
and outputs the number of books, maps and magazine in stock. Include any
validation checks necessary.
(NOTE: A 4-digit code implies all books have a code lying between 1000 and
1999, all maps have a code lying between 2000 and 2999 and all magazines a code
lying between 3000 and 3999. Anything outside this range is an error)

User DylanJ
by
5.4k points

1 Answer

4 votes

Answer:

//program_start

//variable_inicialization

books = 0

maps = 0

magazines = 0

//End variable_inicialization

//operation_selection_routine

display "Select operation: (I) for input item to stock, (O) for output items in stock

operation = read_keyboard_input

IF (operation = "I")

goto input_routine

ELSE IF (operation = "O")

goto output_routine

ELSE

display "Incorrect option, try again"

goto operation_selection_routine

ENDIF

//End operation_selection_routine

//input_routine

display "Insert item code to add to stock"

input_code = read_keyboard_input

IF (input_code >= 1000 & input_code < 2000)

IF (input_code NOT duplicated_in_stock)

add_to_stock (input_code)

books = books + 1

display "Book added"

ELSE

display "Item Code is already present in stock"

ENDIF

ELSE IF (input_code >= 2000 & input_code < 3000)

IF (input_code NOT duplicated_in_stock)

add_to_stock (input_code)

maps = maps + 1

display "Map added"

ELSE

display "Item Code is already present in stock"

ENDIF

ELSE IF (input_code >= 3000 & input_code < 4000)

IF (input_code NOT duplicated_in_stock)

add_to_stock (input_code)

magazines = magazines + 1

display "Magazine added"

ELSE

display "Item Code is already present in stock"

ENDIF

ELSE IF (input_code = 9999)

goto program_end

ELSE

display "Incorrect option, try again"

goto input_routine

ENDIF

goto operation_selection_routine

//End input_routine

//output_routine

display "Items in stock"

display "Number of books: " + books

display "Number of maps: " + maps

display "Number of magazines: " + magazines

goto operation_selection_routine

//End output_routine

//program_end

display "Terminating stock program"

stop_program

Step-by-step explanation:

Pseudo code is a way to design an algorithm using normal words and terms, that are not specific to any programming language. It is easier to understand how the program works and once finished, can be translated to the desired programming language.

This algorithm uses 5 blocks of code:

*variable_inicialization, where item counters are initially set to zero.

*operation_selection_routine, where the program request user input to select if the user will add an item or show existing items in stock. If an incorrect option is given, it will repeat this same routine.

*input_routine, the program will request the four digit code to add, and will check if it is not already present (to avoid duplicated items), and if the item code belongs to a book, map or magazine, increasing the corresponding item counter. Also, if the code is 9999 it will go to the program_end routine, otherwise it will display an error message and request a new item code. After a valid item code it will return to the operation_selection_routine.

*output_routine, it will display on screen the number of items for each category. Then, returns to the operation_selection_routine.

*program_end, it will simply display a goodbye message and terminate the program.

User Jlhuertas
by
5.0k points