Docstoc

Lab 12 Elementary Computer Programming

Document Sample
Lab 12  Elementary Computer Programming Powered By Docstoc
					Lab 7: Elementary Computer Programming -- 40 points In this lab you will learn elementary concepts of computer programming. We will be using the BASIC programming language. Although it is not used much in business or industry, it is very useful for introducing the concepts of programming. 1. Computer programming involves two steps: (1) designing the program (i.e., specifying the operations that the computer is to perform), called writing the program, and (2) running, or executing, the program (i.e., having the computer actually perform the operations specified in the program you designed). In executing the program, you must supply the computer with any input data that the program requires. Since we are going to program in BASIC, you must access the software for creating the running programs in BASIC. To do this, double-click on the Microsoft Quick BASIC icon in the applications group window. A screen will appear that includes a menu bar at the top. Press the ESC key to clear the dialog box in the middle of the screen. Click on each of the menus to see what is there (I call this “taking a walking tour of the menus”). Notice that there is an Open option on the File menu for opening a file stored on disk. The mouse uses a small colored rectangle instead of an arrow. Move the mouse to the left side of your screen and click there to move the input cursor to this point. In a typical basic program, each statement is preceded by a line number; the numbers increase for successive statements in the program. You can later use the line number to refer to a particular statement. It is best not to use consecutive numbers, or else you will find it difficult to add a new line to a program. Typically one might use increments of 20 for consecutive statement numbers. Type in the following BASIC program, substituting your own name for *** : 10 20 40 print “My name is ***.“ print “This is CIS-101.” print “Either one loves or hates programming.”

2.

3.

4. 5.

This is an extremely simple computer program. The print statement tells the computer to print the characters enclosed in the quotation marks. 6. Now that you have entered a program into the computer’s memory, you want to run or execute it. Running or executing a program means having the computer perform the operations specified in the program. To do this, select Start from the Run menu. Notice that the computer executes the program, printing out the strings that you specified. To return to the qbasic screen, press ENTER.

7.

Now add the following three lines to your program: 60 80 100 input like print like print “ students love programming.”

Note the space immediately after the first set of quotation marks in line 100 – all it does is cause the computer to print a space followed by the indicated string of characters. The input instruction in line 60 tells the computer to read a piece of input data (a number) and assign the number as the value of the variable like. After the input statement, whenever we refer to like, the value that was input will be used (unless we later change the value of the variable like). Notice that the print statement in line 80 refers to this variable like and tells the computer to print the value of this variable. 8. Execute this revised program. Notice that a question mark appears. This means that he computer has gotten to a statement that requires input data and it is waiting for you to provide it. Type in any integer value you want and hit the ENTER key. Notice that the machine prints the value of like as specified by line 80 of your program, and then on the next line prints the string “ students love programming.” Rather than just having a question mark and the program waiting for input, it is often helpful to prompt the user for the information that is desired. Add the following line to the program: 59 print “How many students like programming?”

9.

Now run the program again. 10. But now you would like the two lines that were output by your program (the number that was input and the string of characters enclosed in quotes on line 100 of your program) to appear on the same output line. A. Erase line 100 of your program – you can do this by moving the mouse pointer to the end of line 100, clicking the left mouse button, and then backspacing repeatedly, or by highlighting line 100 with the mouse and then selecting Cut from the Edit menu. You can add to a statement by positioning the cursor at the point you want to add, and then typing in the additions. Revise line 80 so that it looks like the following: 80 C. print like; “ students love programming.”

B.

Now run your revised program. Notice now that after you input a value for the variable called like, statement 80 causes both the value of like and the string to be printed on the same line of output. This is because you used a single print statement, merely separating the items to be printed by a semicolon. Notice that the space
2

immediately after the first pair of quotation marks helps to make sure that the value of like does not run into the word Most when the output line is printed. 11. Now we’d like to revise the program so that it computes the number of students who do NOT like programming (hopefully just a few!) If the value of like tells how many students like programming, then subtracting like from 200 will tell us how many students dislike programming (assuming the total enrollment is 200). A. Add the following statements to your program: 100 120 let dislike = 200 – like print dislike; “ students strongly dislike programming”

The let statement is used to compute a new value for a variable. In this case, the value of the variable dislike will be the difference between 200 and the value of the variable like. Therefore, if like has the value 150, then dislike will have the value 50. B. Run your program. When the question mark appears, be sure to input a value for like that is between 0 and 200 -- since if the enrollment in CS-101 is 200, then somewhere between 0 and 200 students must like the class. Notice that you now get two lines of output, one produced by the print statement on line 80 and one produced by the print statement on line 120.

C. 12.

Sometimes we will want to tell the computer to make a decision based on the value of some variable. We do this with the if-then statement. It says that If the condition given immediately after the if is true, then the computer should execute the instruction following the then, and if the condition is not true, then the computer should skip the instruction following the then. A. Add the following statement to your program: 140 if dislike < 50 then print “Programming is a great.”

Statement 140 tells the computer that if the value of the variable dislike is calculated to be less than 50, then it should output the statement that appears within the quotation marks (which states that this is a great course). B. C. Run your revised program, typing 175 as your input data. Notice that you get three lines of output, one of which states that this is a great course. Run your revised program again, this time typing 40 as your input data. Notice that this time you only get two lines of output, since the value of the variable dislike is now computed to be 160 and thus the if-then statement in line 140 says to skip the print statement following the then statement in line 140.

3

13.

But sometimes you don’t just want to make a decision about whether to skip an instruction or not – instead you often want to perform one instruction if the condition is true and a different instruction if the condition is not true. This is the purpose of the if-then-else statement. It tells the computer to execute the instruction following the then if the specified condition is true and to execute the instruction following else otherwise. A. Revise line 140 of your program so that it looks like the following: 140 if dislike < 50 then print “Programming is a great.” else print “Programming is

hard.” B. C. 14. 15. Run your revised program, entering 175 as the input data. Notice that you get three lines of output, as before, including the statement about programming is great. Run your program again, entering 60 as the input data. Notice that you again get three lines of output, but this time you get the statement that programming is hard.

You want to print your current program. Select Print from the File menu and make the appropriate selections in the dialog box that appears. Computer programs often involve loops. This means that a sequence of instructions is repeated more than once. We must be careful to specify exactly when the repetition is to stop, or else we will get into what is called an infinite loop which means that your program never terminates and just runs forever! The for statement allows us to specify repetitions of statements. A. B. Select New from the File menu. You do not need to save your previous program, but you can if you want to. Enter the following program, with your name substituted for *** : 10 20 40 60 80 print “My name is *** .“ for j = 1 to 10 print 5 next j print “Looping is fun!”

The for statement tells the computer that it should repeat the statements from this point until the next statement (which you find on line 60 in this program) and that it should assign the variable j the value 1 on the first time through the statements, then the value 2, then the value 3, etc., until finally j is given the value 10. Once the statements have been executed with j having the value 10, the loop is finished and the computer moves to the statement immediately following the loop, in this case statement 80.

4

C. D. E.

Run your program. What happens? Be sure you understand why it printed the number 5 ten times. Change your program so that instead of printing the number 5 ten times, it prints it 15 times. Run your revised program to make sure that it works. Change your program so that instead of printing the number 5 fifteen times, it prints the number 3 twenty-five times. Note that you will have to change both statement 20 and statement 40. Print your revised program. Now suppose that instead of printing one number many times, you want to print all numbers in sequence between 1 and 10. Revise your program so that it looks like the following (with your name substituted for *** ): 10 20 40 60 80 print “My name is ***.” for j = 1 to 10 print j next j print “Looping is fun!”

F.

Notice that line 40 tells the computer to print the value of the variable j. Since j has the value 1 the first time through the loop, the number 1 will be printed. On the second time through the loop, j will have the value 2 and so the number 2 will be printed. Similarly, on the third time through the loop, j will have the value 3 and the number 3 will be printed, etc. G. H. I. Run your program and make sure you understand why your output consists of the number 1 through 10, followed by the message that looping is fun. Change your program so that instead of printing the numbers 1 through 10, it prints the numbers 1 through 25. Run your revised program. Change your program so that instead of printing the numbers 1 through 25, it prints the numbers 50 through 60. Print this revised program once you have it running correctly. The variable j in our for statement is called a counter. It need not have the variable name j, but instead can be any variable name you choose. Suppose that instead of incrementing our counter by 1 each time through the loop, we want to increment it by 5. For example, suppose that we want to print the numbers 15, 20, 25, 30, 35, 40. Then we can change our program so that it is the following (and you should know by now what to do with the *** ): 10 20 40 print “My name is ***.” for j = 15 to 40 step 5 print j
5

J.

60 80

next j print “Looping is fun!”

The step part of the for statement tells the computer how much to increment the counter by on each pass through the loop. (If it is not given, it is assumed to be 1.) So in our revised program, the counter j first has the value 15, then on the second time through the loop it has the value 20, and on the third time through the loop it has the value 25, etc. K. 16. Run your revised program. It should display the numbers 15, 20, 25, 30, 35, 40. Print your revised program.

Now we will look at a more complex program involving a loop. It will also put together some of the previous concepts that we have learned. A. B. Select New from the File menu. Enter the following program (and you know what to do with the *** ): 5 10 20 40 60 80 100 120 140 print “My name is ***.” print “ “ let sum = 0 input n for j = 1 to n let sum = sum + j next j print “The sum of the numbers 1 through “; n; “ is “; sum print “ “

This program has several features that you should make sure you understand. The only purpose of the print statements on line 10 and 140 is to output blank lines preceding and following the output of the sum that you will be printing. Line 20 starts the variable sum out with the value 0; this is called initializing a variable. Line 40 gets a piece of input data and assigns it as the value of the variable n. The really interesting part is on lines 60 and 80. Note that your counter j will now start with the value 1 on the first pass through the loop and this value will be added to the current value of the variable sum on line 100, then on the next pass through the loop the counter j will have the value 2 and this will be added to the current value of the variable sum on line 80, then on the next pass through the loop the counter j will have the value 3 and this will be added to the current value of the variable sum, and so forth until finally the counter j gets whatever value was entered as input data and assigned as the value of the variable n. So, for example, if you enter 5 as your input data and thus give the variable n the value 5, then the variable sum will become the sum of the number 1, 2, 3, 4, 5.
6

C.

Run this new program, typing 5 as the input data. Make sure you understand why line 120 caused the output that you see. Run the program again, this time typing 8 as the input data. Make sure you understand why you get the output that is displayed. Revise your program so that it computes and prints the average of the numbers that it sums. This means that you will have to add a statement between lines 100 and 120 (call it statement 110) that divides the computed sum by the value of the variable n and assigns this as the value of a new variable (call this new variable ave). (Remember, division is represented by the symbol /; so, for example, setting a variable c to the result of dividing the value of a variable a by the value of another variable b would be represented by the following statement (preceded by a line number, of course): let c = a / b You can add a new line by moving the cursor to the end of the line preceding the location where you want to have a new line and then hitting the ENTER key. Also, you will have to add a new line after line 120 (call it line 130) that prints the value of the variable ave along with a string of characters saying that it is the average. Run your revised program and then print it once you have it working correctly. Now revise your program so that it reads two pieces of input data and assigns the first as the value of the variable st and the second as the value of the variable fin, and then computes and prints the sum of the numbers between the value of st and the value of fin. This means that you will have two input statements, one a revised line 40 that inputs a value for the variable st and an input statement on a new line 50 that inputs a value for the variable fin. You will also need to change line 60 so that the counter starts at the value of the variable st and ends at the value of the variable fin. You will also need to change your print statement on line 120 so that it prints the same information as before but allows the starting point to change instead of always being 1 – that is, you are now summing the numbers between the value of the variable st and the value of the variable fin instead of between 1 and the value of the variable n. In addition, you will need to remove lines 110 and 130 that you had previously added. Run your revised program, using 5 as the input data giving the value of the variable st and 10 as the input data giving the value of the variable fin. Run your program and then print your working program.

D.

E.

F.

7


				
DOCUMENT INFO
Jun Wang Jun Wang Dr
About Some of Those documents come from internet for research purpose,if you have the copyrights of one of them,tell me by mail vixychina@gmail.com.Thank you!