go to previous page   go to home page   go to next page

Answer:

No.


Blocks as Black Boxes

A block should have one set of entry conditions and one set of exit conditions. The block should be a module that can be used for building larger structures. In C, blocks are made by putting braces around a group of statements. The code inside a block often has its own internal logic, but once the block is written and debugged it should be a black box.

Here is a small block:

/* Prompt for and input the coefficients of a quadratic equation. */
/* On Entry: a, b, and c are uninitialized double precision variables. */
/* On Exit:  a, b, and c hold the coefficients of an equation that has a real-number solution */

printf("Enter coefficient a --> ");
scanf("%lf ", &a );
printf("Enter coefficient b --> ");
scanf("%lf ", &b );
printf("Enter coefficient c --> ");
scanf("%lf ", &c );

Usually in programming you would not document such a small block so carefully, but you should think about them as a block as you write the code. Here is another block:

/* Compute the square root of the discriminant of a quadratic. */
/* On Entry: a, b, and c are the coefficients of an equation that has a real-number solution. */
/* On Exit:  disc is the square root of the discriminant */

disc = b*b - 4*a*c ;
disc = sqrt( disc );

Each block can be thought of as a module that is part of the solution to a problem.

With the above two blocks, the exit conditions of the first block match the entry conditions to the second block.



QUESTION 7:

Could these two blocks fit together in sequence?