home *** CD-ROM | disk | FTP | other *** search
/ Grolier Multimedia Encyclopedia 1995 / GME95.iso / z1 / 0692671b.gif < prev    next >
Graphics Interchange Format  |  1994-08-24  |  13KB  |  454x607  |  4-bit (3 colors)
Labels: text | screenshot | font | number | black and white
OCR: Input - a list of numbers with the first number N indicating how many numbers are to Requirements follow it. Output - the sum of the numbers following N. Function - compute the sum of N numbers and print them out. Language - BASIC. Machine - personal computer. Exceptions - The program must not fail due to machine overflow. Design Specification Components - the program is to be a single component consisting of the input, output, and calculation sections. Data Structures - the numbers are to be stored in simple variables, Nand X, where Nis an integer and X is an integer or real number. Functionality - the program will check to make sure the sum is zero when N is less than or equal to zero; otherwise it will compute the sum by successive addition of each number as it is read into the machine from the keyboard. Coding The following is a rendering of the program in the language BASIC. This is how it would be entered into the computer 10 REM Program to read N, X (subscript 1), X (subscript 2) , ... X (subscript N) and display the sum 20 INPUT "Enter N="; N 25 SUM=0 30 FOR I =1 to N 35 INPUT X 40 SUM=SUM + X 50 NEXT I 60 PRINT "Sum="; SUM 99 END Notes In the BASIC language, the increasing numbers beginning each line serve to index each step. Statement 10 is a remark ( REM) that documents the program by indicating, to the user, what it accomplishes; line 20 is the instruction that prompts for the value of N and then waits for the user to enter N; line 25 initializes the sum to zero; line 30 indicates that the statements numbered 35 and 40 are to be repeated N times for I =1, 2,3 ... N; line 60 causes the answer to be displayed. In statement 40 the value of SUM is increased by X each time 40 is repeated. Testing Case 1 - let N=O, and run the program to get SUM=0. Case 2 - let N=1 , and run the program with X=5 to get SUM=5. Case 3 - let N=3, X=1 ,- 2,3 to get SUM=2. Maintenance The program may be changed many times during its lifetime. For example, a user may request that the program print an error message when the value of N is less than zero.