Basics, Lecture 3

  1. The process of creating software
    1. Example problem:   Write a program that converts a temperature in Fahrenheit to both Celsius and Kelvin.  The formula for conversion of Fahrenheit to Celsius is C = (5/9)(F-32), where F is temperature in Fahrenheit and C is temperature in Celsius.  Degrees Kelvin are just degrees Celsius shifted by a constant amount. The zero of the Kelvin scale is -273.15 degrees Celsius.   Your output should look something like: 32 Fahreheit is equivalent to 0 Celsius and 273.15 Kelvin.
    2. Analyze the problem
      1. Read the information given about the problem carefully.  If you don't fully understand something, get more information about it.
      2. Determine what outputs the program must produce:
        1. What is the form of the output?
        2. What parts of the output are computed and what parts are fixed?
          1. In the example, the 0 and -273.15 parts are computed.  Everything else in the output example except for the 32 is fixed.
      3. Determine what items are considered inputs in the problem
        1. An input is something that can be changed each time the program is run
        2. In the example, Fahrenheit temperature is an input item and 32 is the input value used for this input item in the example.
    3. Develop (design) a solution
      1. List the steps of the overall solution (a solution outline)
        1. Assign input value to the Fahrenheit temperature input item
        2. Compute the Celsius and Kelvin values corresponding to the input Fahrenheit value
        3. Output the input and computed values
      2. If one of the steps appears to be even a little difficult to write in C++, refine the step (break it down into substeps and add detail)
        1. The "Compute" step in the example is not obvious.  Refine it into two substeps and supply detailed formula for computing each value:
          1. Compute Celsius using the formula C = (5/9)(F-32)
          2. Compute Kelvin using the formula K = C + 273.15
      3. Trace (aka desk check) your solution on at least one example to make sure that it gives correct answers.
        1. Draw a box for each variable (F, C, and K in the example) and for the "screen" where the output can be displayed
        2. Go through the steps of the outline and change the boxes as directed
        3. Step
          a 32    
          b.i 32 0    
          b.ii 32 0 273.15  
          c 32 0 273.15 32 Fahreheit is equivalent to 0 Celsius and 273.15 Kelvin
        4. Verify that the result is what you expected.  If not, correct problems in your analysis or solution.
    4. Write your solution in C++ ("code" your solution)
      1. Write the standard parts of the program (#include, main, return, etc.)
      2. Identify data types of input/output variables
        1. In this example, F, C, and K should probably all be float because of the decimal in the Kelvin value in the example.
      3. Write declaration statements for all variables.  The variable names used may or may not be those used in the design.  Include appropriate comments.
        1. Example:

        2. float fahrenheit;  // input temperature in fahrenheit
          float celsius;         // temperature in celsius corresponding to input fahrenheit temp
          float kelvin;          // temperature in kelvin corresp. to celsius and fahrenheit
      4. Write other C++ statements corresponding to each line in the solution outline, including comments corresponding to at least highest level of the solution outline.
        1. Be careful about various C++ idiosyncracies:
          1. Integer arithmetic (truncation of quotient to an integer)
          2. Multiplication must be indicated explicitly
        2. Example:

        3. // Assign value to fahrenheit input item
          fahrenheit = 32.0;

          // Compute corresponding values on other temperature scales
          celsius = (5.0/9.0)*(fahrenheit - 32.0);
          kelvin = celsius + 273.15;

          // Output the results
          cout << fahrenheit << " Fahreheit is equivalent to "
                   << celsius << " Celsius and " << kelvin << " Kelvin" << endl;

    5. Test your program
      1. First, fix any "compile" errors (errors that the computer explicitly tells you about).  If fixing any of these leads you to change your analysis or the outline of your solution, go back and rethink those as needed.  Then fix your C++ code.
      2. Once your program compiles, run all traces that you performed during design.
      3. If the answer from your program does not agree with the answer from your desk check, debug your program.
        1. Typically, this involves carefully checking everything in your code to try to understand why the program result differs from your hand check result.
        2. Also double check that you did the hand calculation properly!
        3. For a large program with many intermediate calculations that are not output, temporarily add cout statements that output some or all of these values so that they can be compared with intermediate values of your desk check calculations.
        4. If you're still stuck, ask someone else (your professor, a tutor, a classmate) to see if they can help you find the problem.  As a rule of thumb, if you've been trying unsuccessfully for 15 minutes to find a bug, it's time to get some help.
        5. When you do find a bug, be sure to go back and fix any analysis or design (solution outline) problems that are related to the bug.  Then modify your code.
      4. Design and run additional tests (at least one in addition to the traces) that are relatively easy to check.
        1. In the example, we know that -273.15 Celsius is equivalent to 0 Kelvin.  So we could manually convert -273.15 Celsius to Fahrenheit and input the Fahrenheit value to our program.  We would then know what values to expect as output (-273.15 Celsius and 0 Kelvin).