Section 2.3 Writing Algorithms An algorithm must be clear and complete for it to be used by a computer to calculate a result. Take care when writing an algorithm to ensure it is correct. Each step in the algorithm accomplishes one part of the solution. Once a way to solve the problem has been found, write the steps to form the algorithm. The algorithm can be turned into computer code. Then, the computer can calculate the result. Average Number of Days in a Month Each month of the year has a certain number of days, as shown in Figure 2-7. Some months have 30 days, while some months have 31 days. February has 28 days except in leap years. What if you needed to know the average number of days for all months? The problem-solving method can be used to write an algorithm to find this solution. Understand the Problem The first step is to understand the problem. In this case, the problem is the need to find the average number of days in a month. For this example, the year will not be a leap year. Devise a Plan The next step is to devise a plan. For this problem, a simple equation can be used to find the solution. Add the number of days in each month, and divide the sum by the number of months. Goodheart-Willcox Publisher andy0man/Shutterstock.com Figure 2-7. Writing an algorithm for finding the average number of days in a month. Problem: Find the average number of days in a month in a nonleap year. Solution: Add the number of days in each month, and divide the sum by the number of months in a year. Algorithm: 1. Find the number of days in each month. 2. Add the numbers. 3. Divide by 12. 4. Round the answer in Step 3 to a whole number of days. Copyright Goodheart-Willcox Co., Inc. Chapter 2 Solving Problems 31