A program is a sequence of instructions that enables the computer to solve a problem. Writing a program to solve the problem can be a formidable task if the problem is complex. The overall programming process can often be simplified by using a top-down approach. The process is called a top-down design because the programmer starts with the original problem and breaks it down into smaller tasks which can be addressed separately. First, the large tasks are identified and then each of these tasks is refined filling in more details. This process allows the programmer to initially concentrate on the overall steps without getting bogged down in the details of the program, thus making the programming task more manageable. Another advantage of breaking a program down into tasks is that each task can be checked for correctness apart from the other tasks. An algorithm or plan, in the form of a flowchart or pseudocode (English-like statements), can be used to describe the problem solving process.
Find the surface area and volume of a sphere with a given radius.
Enter the radius of the sphere. 64.0 64.0000 is the value of the radius. In a sphere of radius 64.0000 , the surface area is 51471.9 and its volume is 1.09807E+06.
The following explains each of the keywords used in the example code:
2. Using a text editor, key in the code given for the sphere problem.
4. Once your program compiles, execute it by keying the name of the executable code: a.out
radius | area | volume |
---|---|---|
2.0 | ||
4.0 | ||
8.0 | ||
16.0 | ||
32.0 | ||
64.0 |
After reviewing the data in Table 1, answer the following question:
As the radius of the sphere increases by a factor of 2, the area increases by a factor of _______ and the volume increases by a factor of _______.
5. Problem Statement
Find the area of a triangle given the
length of its base and height.
Create a file called area.f. Key in the following code and make it more readable by inserting comments and blank lines. Your comments should explain the purpose of the program and the coded steps of the algorithm. You should also add your name and exercise number as comments at the beginning of the code. Then compile and execute area.f.
PROGRAM area REAL base, height, area PRINT *,'Enter the values for the base and height of a triangle.' READ *, base, height area = (1.0/2.0) * base * height PRINT *,'The area of a triangle with base ', base PRINT *,'and height ', height,' is ', area STOP END