Software Development Method

 

1. Problem Specification

Get a statement of the problem

Understand what is being asked for.

 

Example:

1.       Write a program that reads in the miles of a trip and displays the equivalent mileage in kilometers.

2.       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?

 

2. Analyze the Problem

·         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 the Algorithm

·         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.

 

 

4. Implementation

Write (or code) each step in C.

 

5. 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 <stdio.h>  

/* 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);

}

 

 

Top-Down Design with Functions

 

Reading Assignment – Section 3.1 in the textbook

 

Library Functions

 

- C provides many predefined functions in libraries such as stdio.h, math.h, stdlib.h, time.h (look at Appendix B)

- examples of library functions: printf, scanf, sqrt

            Function call:

y = sqrt(x);    /* x is argument, sqrt is function name */

- other examples from math.h: cos(x), floor(x), log10(x), pow(x,y)

 

Case Study: Drawing Diagrams

 

- read Section 3.3 in the textbook

- the task is to draw the figure below using printf . The task can be decomposed into several subproblems. Solution of each subproblem can be used as a component in similar problems (e.g. draw the figure consisting of 3 circles placed on top of two triangles)

 

   *

 *   *               draw_circle

  * *

   /\

  /  \

 /    \                     draw_triangle” (consists of “draw_intersecting_lines

/      \                    and “draw_base”)

---------

   /\

  /  \               draw_intersecting_lines

 /    \

/      \

 

Functions without Arguments

 

- They do not require any value to be passed during the function call

- Examples (see Figure 3.14):

draw_circle(), draw_intersecting_lines(), draw_base(), draw_triangle()

- There are 3 basic steps that need to performed to write and use a function:

- write “function prototype

- write “function definition

- use function in the program

- Function prototype:

- must be declared before it can be referenced in the program

- it should be inserted before the main function

- general form:

ftype fname(void)

- example:

void draw_circle(void);

-Function definition:

- similar form as the main function

- must declare its own variables (variables from main are invisible, unless they are passed as input arguments)

- function definition should be written after the main function

- general form:

ftype fname(void)

{

     local declaration

     executable statements

}

- examples: see functions declared in Figure 3.14

Function call

- general form (shows how function is called in the program):

fname();

- example:

draw_circle();

- Order of execution

- when function is called the control of the program goes to function execution; the control is transferred back to the main after the last statement is executed

- how the memory is allocated: when the function is called, the values of all variables from main function are frozen, additional space is allocated to store variables declared by the function; when control goes back to the main function, the memory occupied by the local variables is released

- Advantages of functions

- procedural abstraction (decompose a big problem into subproblems and solve each subproblem separately)

- reuse of functions