Sequential Problem Solving and Program Design

Fundamental Methodology of Problem-Solving in Programming

  • Solving programming problems is analogous to solving high school mathematics word and story problems.
  • The process begins with reading the problem to identify the specific requirements and logical steps needed for a solution.
  • A standard approach involves isolating three core sets of information to ensure the resulting program is accurate: inputs, processes, and outputs.

Step 1: Identifying and Categorizing Inputs

  • The first step in any problem is to understand the inputs. This requires distinguishing critical data from non-essential information included in the problem description.
  • For a computer program to process these inputs, the programmer must define specific characteristics about the data:
    • Data Types:
    • Numeric Inputs: These can be classified as whole numbers (counting numbers or integers) which may be positive or negative. Alternatively, they may be floating-point numbers or real numbers, which include decimals.
    • Character Inputs: Individual letters or symbols. It is essential to determine if casing matters (uppercase vs. lowercase).
    • Strings: These are defined as collections of characters or full sequences of words.
    • Restrictions and Limitations:
    • Programs must define if a number needs to be positive, negative, or a zero.
    • Specific data types, such as those used for money, have their own unique logic and restriction requirements.
  • It is necessary to determine the method by which the program will receive these inputs from various devices.

Step 2: Determining Calculations and Unit Conversions

  • The second step in problem-solving is identifying the calculations or "process" required to reach the solution.
  • Formulas and Functions:
    • Basic geometric formulas include the circumference of a circle or the volume of a cube (v3v^3).
    • More complex formulas involve conversions, such as converting Temperatures to Celsius.
    • Advanced mathematical operations may utilize trigonometric functions, exponentials, or logarithms (ln\ln).
  • Handling Units and Percentages:
    • Unit conversions are a vital part of the process identifying phase.
    • For instance, if a problem provides a percentage of 10%10\%, the calculation within the program must use its decimal equivalent, which is 0.10.1. This transition is achieved by moving the decimal point relative to the percent sign.

Practical Application: The Chicken Wood Fence Problem

  • To illustrate the Input-Process-Output (IPO) model, consider a story problem where a circular fence is being built around "chicken wood."
  • Identifying Inputs:
    • In this descriptive problem, the specified input is the "rating" or radius (rr).
  • The Process and Specific Calculations:
    • To find the required amount of fencing, one must calculate the circumference (CC).
    • Circumferential Formula: C=2×π×rC = 2 \times \pi \times r
    • Resource Allocation Logic:
    • Suppose the calculated circumference is 55 feet55\text{ feet}.
    • If the fence rolls are sold in units of 20 foot rolls20\text{ foot rolls}, you cannot purchase a partial roll (e.g., half a roll).
    • Simple division (5520\frac{55}{20}) results in 2.752.75. However, the practical requirement is three full rolls (3×20=60 feet3 \times 20 = 60\text{ feet}).
  • The Ceiling Function:
    • In programming terminology, rounding up to the next largest whole number is performed by the "ceiling" function.
    • Calculation Rule: Number of rolls=ceiling(Cfence roll length)\text{Number of rolls} = \text{ceiling}(\frac{C}{\text{fence roll length}})

Step 3: Defining the Output

  • The final step is determining what the program should display or produce.
  • In the fencing example, the output is the total number of fence rolls.
  • Data Integrity of Output:
    • The resulting value must be an integer, which is a whole, counting number.
    • The output for this counting process should be zero at its base level, representing a non-negative whole value.