Try it on paper first. A hint, the key idea and the worked solution are here when you need them.
A school library keeps a record for each book. The record holds the book's title, its number of pages, its price in dollars (such as 12.95), the first letter of the shelf it sits on, and whether it is on loan.
Write a DECLARE statement for each of the five items. Choose meaningful identifiers and the best data type for each.
A variable is declared with its type: DECLARE Name : INTEGER. The five types are INTEGER, REAL, CHAR, STRING and BOOLEAN. A constant takes a literal: CONSTANT Name ← 5. Assignment is ←; = compares. DIV(A, B) is the whole part of , and MOD(A, B) is the remainder.
Have you tried it?
Work it out on paper first. It is fine to get stuck or make a mistake: that is how we learn. When you are ready, go through the solution one step at a time, and check each step against your own.
Plan. Match each item to one of the five data types, then write it in the syllabus form DECLARE <identifier> : <data type>.
DECLARE Title : STRING
DECLARE NumberOfPages : INTEGER
DECLARE Price : REAL
DECLARE ShelfLetter : CHAR
DECLARE OnLoan : BOOLEANWhy. A title is text of many characters, so STRING. Pages are counted in whole numbers, so INTEGER. A price such as 12.95 has a fractional part, so REAL. The shelf letter is exactly one character, so CHAR. "On loan or not" has two values, so BOOLEAN.
Watch out. Number of pages with spaces, or number_of_pages with underscores, breaks the identifier rules of the syllabus. A price stored as INTEGER loses the cents.
Log in to join the discussion.
Loading…