Lab 1.2 Exercise Answers
This section gives you some suggested answers to the questions in Lab 1.2, with discussion related to how those answers resulted. The most important thing to realize is whether your answer works. You should figure out the implications of the answers here and what the effects are from any different answers you may come up with.
What is a top-down approach?
Answer: The technique of breaking a problem into parts and solving each part is called a top-down approach to problem solving. By breaking down the problem, it is easier to focus on possible solutions and manage each part. Once each part is fully understood, the solution to the overall problem can be readily developed.
What is structured programming?
Answer: Structured programming embodies a disciplined approach to writing clear code that is easy to understand, test, maintain, and modify. A program can be organized into modules called subroutines. These subroutines focus on a particular part of the overall problem that the program addresses. Subroutines are easier to understand and manage because they are only components of the overall program. Together, all of the subroutines compose the overall program.
Create the following selection structure: Determine which season each month of the year belongs to.
Answer: Your selection structure should look similar to the following:
IF MONTH IN ('DECEMBER', 'JANUARY', 'FEBRUARY') IT IS WINTER IF MONTH IN ('MARCH', 'APRIL', 'MAY') IT IS SPRING IF MONTH IN ('JUNE', 'JULY', 'AUGUST') IT IS SUMMER IF MONTH IN ('SEPTEMBER', 'OCTOBER', 'NOVEMBER') IT IS FALL
The test conditions of this selection structure use the operator IN. This operator allows you to construct the list of valid months for every season. It is important to understand the use of the parentheses. In this case, it is not done for the sake of a syntax rule. This use of parentheses allows us to define clearly the list of values for a specific month, hence helping us to outline the logic of the structure.
Now, consider the following fragment of the selection structure:
IF MONTH IS 'DECEMBER' IT IS WINTER IF MONTH IS 'JANUARY' IT IS WINTER IF MONTH IS 'FEBRUARY' IT IS WINTER ...
This selection structure results in the same outcome, yet it is much longer. As a result it does not look well structured, even though it has been formatted properly.
Create the following iteration structure: For every day of the week, display its name.
Answer: Your selection structure should look similar to the following:
WHILE THERE ARE MORE DAYS IN THE WEEK DISPLAY THE NAME OF THE CURRENT DAY GO TO THE NEXT DAY
Assume that you are starting your week on Mondaythere are six days left. Next, you will display the name of the current day of the week, which is Monday for the first iteration. Then, you move to the next day. The next day is Tuesday, and there are five more days in the week. So, you will display the name of the current dayTuesdayand move to the next day, and so forth. Once the name of the seventh day (Sunday) has been displayed, the iteration structure has completed.
Create the following structure: For every day that falls within the business week, display its name. For every day that falls on the weekend, display "The weekend is here, and it is here to stay!!!"
Answer: Your structure should look similar to the following:
WHILE THERE ARE MORE DAYS IN THE WEEK IF DAY BETWEEN 'MONDAY' AND 'FRIDAY' DISPLAY THE NAME OF THE CURRENT DAY IF DAY IN ('SATURDAY', 'SUNDAY') DISPLAY 'THE WEEKEND IS HERE, AND IT IS HERE TO STAY!!!' GO TO THE NEXT DAY
This structure is a combination of two structures: iteration and selection. The iteration structure will repeat its steps for each day of the week. The selection structure will display the name of the current day or the message "The weekend is...."
Assume that you are starting your week on Monday again. There are six days left. Next, control of the flow is passed to the selection structure. Because the current day happens to be Monday, and it falls within the business week, its name is displayed. Then, control of the flow is passed back to the iteration structure, and you are ready to move to the next day.
The next day is Tuesday, and there are five more days in the week. So, control is passed to the iteration structure again. Tuesday also falls within the business week, so its name is displayed as well. Next, control is passed back to the iteration structure, and you go to the next day, and so forth. Once the day falls on the weekend, the message "The weekend is . . ." is displayed.
1.2.2 ANSWERS
What is the reason for formatting your code?
Answer: A well-formatted program is easier to understand and maintain because format can reveal the logical structure of the program.
What are two main guidelines of good formatting?
Answer: First, the code of the program should be indented so that the logical structure of the program is clear. Second, the program should contain comments describing what is being accomplished.