Software Development Method 1. Problem Specification Get a statement of the problem Understand what is being asked for. Example: Write a program that reads in the miles of a trip and displays the equivalent mileage in kilometers. Modify this program to read the mileage for your car and the cost of a gallon of gasoline and display the total cost of the trip. Questions: 1. Do you still want to display the distance in kilometers? 2. What exactly is "the mileage for a car"? 3. Are any of these items "constants" or should they all be read as data? Analysis List the problem inputs List the problem outputs List other variables you may need to keep track of. List any formulas that may be relevant. Problem Inputs kilometers distance in kilometers mileage miles per gallon for your car galcost cost of a gallon Problem Outputs miles distance in miles tripcost cost of trip Additional Variables gallons number of gallons used Formulas 1 mile = 1.609 kilometers gallons = miles / mileage tripcost = gallons x galcost 3. Design Write an algorithm (list of steps) to solve the problem. Don't attempt to solve every detail at once. First, provide an overview similar to an outline. Add detailed refinements for each step that needs clarification separately. ("Divide and conquer") Algorithm 1. Read the miles, mileage, and cost of gasoline 2. Calculate the distance in kms. 3. Calculate the cost of the trip 4. Display the distance in kms and the cost of the trip. Refine step 3: 3.1 Calculate the number of gallons needed. 3.2 Calculate the cost of the trip. Implementation Write (or code) each step in C. Testing Compile and debug the program. Validate the program output for several representative test cases that you calculate by hand. Implementation (partial) /* Figure 1.9 Miles-to-Kilometers * Conversion Program * Converts distance in miles to kms */ #include /* printf, scanf definitions */ #define KMS_PER_MILE 1.609 int main(void) { double miles; /* input - dist. in miles. */ double kms; /* output - dist. in kms */ /* List other variables here */ ... /* Read miles, mileage, and cost of a gallon. */ printf("Enter dist in miles> "); scanf("%lf", &miles); ... /* Calculate dist. in kms. */ kms = KMS_PER_MILE * miles; /* Calculate cost of trip */ ... /* Display dist in kilometers. */ printf("That equals %f kms.\n", kms); ... return (0); } Different kinds of statements: Compiler directives #include Include the source code for library file stdio.h. Enables C compiler to recognize printf and scanf from this library. #define KMS_PER_MILE 1.609 Substitute 1.609 for the name KMS_PER_MILE wherever it appears. Comments - statements that clarify the program - ignored by compiler but "read" by humans. /* Calculate cost of trip */ Declaration of identifiers or variables in the program. These are names of memory cells that will store information. double miles; Declares the variable miles that can store a real number (with a fractional part). Other declarations: int kids, courses; Declares the variables kids and courses that can store integer values. char initial; Declares the variable initial that can store a single character. Calls to library functions printf("Enter dist in miles> "); scanf("%lf", &miles); printf("That equals %f kms.\n", kms); Each call to printf and scanf begins with a format string in double quotes. printf(format string); printf(format string, output list); scanf(format string, input list); After the format string (following comma) comes an input list (for scanf) or an output list (for printf). The variables named in the input list are preceded by an & which means address of (e.g., &miles). %lf and %f are placeholders - They indicate the data type and position of a value in a format string. Use: %lf in scanf for type double value %f in printf for type double value %d in printf and scanf for type int value %c in printf and scanf for type char value The two-character sequence \n represents the "newline" character - carriage return, line feed. Assignment statements Assign a value to a variable. The value to be assigned is written on the right hand of the assignment operator =. The variable getting the value is on the left hand side. sum = x + y; sum gets the value of x + y. Variables x and y must be "defined" beforehand - they must have data stored in them. kids = kids + 1; kids gets the value of the "current value of kids" + 1. If kids was 5, its new value will be 6 (strange, but true!) hypotenuse = sqrt(side1 * side1 + side2 * side2); Assignment statements can be quite complex! Notice that x + y = sum; is invalid. Why?