150 likes | 166 Views
CS1001 Lecture 2. Programming and problem solving Software engineering practices. Introduction. Use computers as a problem solving tool A computer program is a sequence of instructions that must be followed to solve a particular problem.
E N D
CS1001 Lecture 2 • Programming and problem solving • Software engineering practices
Introduction • Use computers as a problem solving tool • A computer program is a sequence of instructions that must be followed to solve a particular problem. • Software engineering is the study and application of programming and problem solving techniques and methodologies
Simple Program Design Process Start Specify & analyze the problem you are trying to solve Define required inputs and outputs Design the algorithm Convert algorithm into Fortran statements Test the resulting Fortran program Finished!
Problem Statement A nuclear physicist is conducting research with the radioactive element polonium. The half-life of polonium is 140 days, which means that because radioactive decay, the amount of polonium that remains after 140 days is one-half of the original amount. How much polonium will remain after 180 days if 10 milligrams are present initially. What information is given? … which ones are important ? What information must be produced?
Data analysis and organization Specific Input Output Initial amount: 10 mg Amount remaining Half-life: 140 days Time period: 180 days Generalize Input Output Initial amount Amount remaining Half-life Time period InitialAmount HalfLife Time AmountRemaining
Algorithm Design & Refinement Get values for InitialAmount, HalfLife, and Time Compute the value of AmountRemaining for a given Time Display AmountRemaining AmountRemaining = InitialAmount*(0.5)**(Time/HalfLife) Assignment = multiplication * division / raise to power ** Logics errors
Coding • General form of Fortran program: Heading specification execution subprogram END PROGRAM • Documentation • Opening documentation • http://www.cs.wpi.edu/Help/documentation-standard.html
Coding Program heading PROGRAM Radioactive_Decay !-------------------------------------------------------- ! This program calculates the amount of a radioactive ! substance that remains after a specified time, given ! an initial amount and its half-life. Variables used are: ! InitalAmount : initial amount of substance (mg) ! HalfLife : half-life of substance (days) ! Time : time at which the amount remaining ! is calculated (days) ! AmountRemaining : amount of substance remaining (mg) ! ! Input: InitialAmount, HalfLife, Time ! Output: AmountRemaining !--------------------------------------------------------- Opening documentation Other useful information: author, date, version number, etc. ! Use for Comments
Specification IMPLICIT NONE REAL :: InitialAmount, HalfLife, Time, AmountRemaining ! Get values for InitialAmount, HalfLife, and Time. PRINT *, "Enter initial amount (mg) of substance, & &its half-life (days)" PRINT *, "and time (days) at which to find amount remaining:" READ *, InitialAmount, HalfLife, Time ! Compute the amount remaining at the specified time. AmountRemaining = InitialAmount * 0.5 ** (Time / HalfLife) ! Display AmountRemaining. PRINT *, "Amount remaining =", AmountRemaining, "mg" END PROGRAM Radioactive_Decay Execution Prompt for, and obtain input PRINT, READ Continuation Display the result as an output PRINT END PROGRAM statement
Preparing a Program for Execution You enter the program and save it as a source file Revised Source file Syntax or compile-time errors Source file on disk You correct syntax error Oops! List of errors The compiler attempts to translate the program The loader places the load file into memory Good job! Other Object File New Object File Load File Executable program in memory The linker links the new object file with other object files Run-time errors
Testing • Initial test -- the ones that you can calculate easily by hand. E.g.: • InitialAmount = 2, HalfLife=140, Time=140. AmountRemaining should = 1.0000000 mg • InitialAmount = 4, HalfLife=140, Time=280. AmountRemaining should = 1.0000000 mg. • Then InitialAmount = 10, HalfLife=140, Time=180. AmountRemaining= 4.1016769 mg • “Once we are confident that the program is correct” • what? when? how? how much?
More Realistic Process Start Specify & analyze the problem you are trying to solve Define required inputs and outputs Decomposition Design the algorithm Stepwise refinement Convert algorithm into Fortran statements Test the resulting Fortran program Release End of life Maintain the resulting Fortran program
A typical testing process for a large program Start Unit testing of individual subtasks Subtasks validated separately Successive builds (adding subtasks to the program) As many times as necessary Subtasks combined into programs Alpha release As many times as necessary Major bugs fixed Beta release As many times as necessary Minor bugs fixed Release -- a little breather !
Program Design Tips • This process is often iterative because you may not initially know what all you want to print out, or input. • Design a little • Code a little • Test a little • Take a break • Repeat until finished
Summary • Program solving process, SE process • Fortran statements: PROGRAM program_name IMPLICIT NONE REAL:: list_of_real_variables INTEGER:: list_of_integer_variables Comments ! Continuation & Operations + - * / ** Assignment = Input READ*, input_list_of_variables Output PRINT *, output_list_of variables END PROGRAM program_name