After studying the Module 2 Game Skills 1 Chip, you will be able to:
Wow, that last lesson sure did cover a lot of information related to variables. Remember there are two types of variables we will work with in this course: numeric and string. The names you choose for your variables should be meaningful. Add a $ to the end of a string variable name to let the computer know it will be assigned alphanumeric values. Variables are assigned values using the LET statement. Numeric variables can be used in equations.
When a program is run, and a variable is encountered, the computer goes to its memory to look up the value assigned to the variable.
This lesson will focus on numeric variables. Let's practice writing a simple program with numeric variables. Ready? We will follow the Five Step Design Process as described in Lesson 1.04.
Write a program that will calculate the volume of a rectangular swimming pool. (For those of you in advanced math and science courses, this pool will be perfectly square, no rounded corners; all the same depth, no shallow or deep end. Also, the water will go all the way to the top.) The measurements for the length, width and depth are 25, 10, 5; respectively. For the output, display statements listing the length, width, depth, and volume of the pool.
Now that you know what the program is supposed to do when it is run, let's move to step 2 and plan the program.
An algorithm is your plan and will be used as a guide as you code the program. You can write an algorithm in everyday language (pseudocode) or with pictures (flowcharts). Either way, your algorithm should start with the input (assign the given data to variables). Next is the processing section where calculations are performed to create useful information. Third, output is displayed for the user to view.
To help you see this a little better, the instructions which correspond directly to the 3 different sections of the program are color coded. Input - green, lines of output are shown in blue. The details "behind the scenes" are shown in orange.
PSEUDOCODE | FLOWCHART |
---|---|
INPUT The measurements for the length, width and depth are 25, 10, 5; respectively. Assign values for the length, width and depth to numeric variables (length1, width1, depth1) PROCESSING Calculate the volume of the pool using the formula: volume = length * width* depth Be sure to use the variables for length, width, and depth in the equation. OUTPUT PRINT statements listing the "Swimming Pool Dimensions" such as: The Length (ft) of the pool is: ### The Width (ft) of the pool is: ### The Depth (ft) of the pool is: ### The Volume (cubic feet) of the pool is: ### Be sure to use the variables for the numeric values in the PRINT statements and use HIGH CONTRAST color combinations while making it look professsional. |
Your algorithm should contain the details needed to write the program. Can you visualize what the program will do when it's run? Your algorithm doesn't show you how to write the code, just what should take place in the program. Extras such as your heading, CLS, color, blank lines, and END are not included in the pseudocode or flowchart.
With the program defined and an algorithm developed, we are ready for the third step - coding the program.
Only after the program is defined and an algorithm is developed should you attempt to write the code for a computer program. Take a look at the following program and notice how the algorithm has been translated into QBasic . The program has been divided up into sections clearly identified with REM statements. In addition, blank REM statements (achieved by using the apostrophe) break up the code even more. Your programs should look like this as well.
REM Swimming Pool Volume REM Programmer: Peter Piper REM 01/01/01 ' COLOR 14,1 CLS ’ REM INPUT - Pool dimensions LET length1 = 25 LET width1 = 10 LET depth1 = 5 ' REM PROCESSING - Pool Volume LET volume1 = length1 * width1 * depth1 ' REM OUTPUT - Display information for the User to read PRINT "The Length (ft) of the pool is: "; length1 PRINT "The Width (ft) of the pool is: "; width1 PRINT "The Depth (ft) of the pool is: "; depth1 COLOR 15, 1 PRINT "The Volume (cubic feet) of the pool is: "; volume1
END
Let’s talk about what happens as the user runs the program. Make sure you understand what each statement does. There are some important things to notice:
Study this example until you completely understand what each statement does.
The
following is an example of bad programming practice.
PRINT 2+3 * (8/2) |
It's time to actually see what kind of output this program produces. Open your QBasic editor and type this program exactly as you see it listed. Save the file as VOLUME.BAS. After you get the program to RUN, you must verify the output is correct. Make sure the literals are readable and the numeric values are correct. Did the volume calculate properly? Double check. Yes, you need to do the math! Pull out your paper and pencil or calculator to make sure the calculations are accurate. When this program is run, you should see the following on the output screen:
The Length (ft) of the pool is: 25
The Width (ft) of the pool is: 10
The Depth (ft)of the pool is: 5
The Volume (cubic feet) of the pool is: 1250
How did it go? Do you completely understand what each statement does? If not, please go back and look this lesson over again or contact your instructor.
Before you leave this example, try to modify it so that the first and second sections are printed in different colors.
This program is so simple that it doesn't need much more documentation than the REM statements that you have already included. That is typically the case on small programs; however, on really big programs, a whole documentation manual might be written to help future programmers and users understand the program when it needs to be modified.
Now it’s your turn to write a program. Be sure to go through the 5 steps for developing a program.
Program Definition: Write a program that calculates the area of a triangle if the base is 25 and the height is 6. Three sentences stating the base, height and area of the triangle should be displayed on the output screen.
Special Considerations: Use the example in this lesson as a model. You must assign values to variables and use variables to perform the calculation (in other words, do not use a PRINT statement to do the actual calculation.) It is good programming practice to use Meaningful Variable Names. For example: base1, height1, area1
Be sure to use the Meaningful Variable Names for the numeric values in the PRINT statements.
The formula for the area of a triangle is: one half the base times the height.
For the output the first two lines should be the same color, but make the last line a different color. Your output might look like:
The base of the triangle is: 25
The height of the triangle is: 6
The area of the triangle is: 75
Develop the Algorithm: You are required to develop an algorithm for this assignment. You may choose to write pseudocode or draw a flowchart. (It is preferred that you write pseudocode and it is suggested to use NotePad. Please see Lesson 2.04 for instructions.) Save the algorithm as TRI.xxx. (The extension will vary depending on the application used.)
COPY and PASTE your pseudocode from your NotePad into the Student Comments section of the assignment document. DO NOT ZIP IT, just copy and paste the pseudocode into the Student Comments section of the assignment document.
Coding the Program: Now that your algorithm is written, it is time to code the program in QBASIC. Be sure to use all of the "good programming practices". Save the file as TRI.BAS.
Remember to use high contrast color combinations and don't go overboard with using a lot of colors.
Test and Debug: Run your program. Did it work? If not, debug your program. Did the output look similar to the output above? It should. If not, go back and look at sample PRINT statements in the example programs. If you get error messages, which you probably will, read them carefully and think about what they might mean. They can be a bit confusing at first but once you’ve seen them a few times, they start to make sense.
Documentation: Now that your program is working properly, go back and make sure the documentation is complete and meaningful. Is your heading complete? Did you include the name of the program, your full name, and the date? Did you divide the program into sections and label them? Are there any additional notes you feel will be beneficial to future programmers who may need to read your code and make updates?
Check Your Understanding: This program should include statements that assign values to numeric variables. Those variables will then be used in calculations and output statements. The computer will look up the value assigned to the variable each time it encounters it.
As you wrote the program, "good programming practices" should have been used. At this stage of your experience, that means:
Submission Instructions: Make sure both files have been saved with the file name TRI. The extension for the algorithm file will vary depending on the application used. The program file will have a .BAS extension.
Grading Rubric: This assignment is worth 10 points and will be graded according to the following rubric.
We recommend that you do not attempt the next assignment until this assignment has been graded and until you have at least full credit on it.
This assignment is worth a total of 10 points. If you are dissatisfied with your grade, you may redo and resubmit the assignment.
Before you move on to the next assignment, please make sure that you can do the following.
Knowing how to use variables is critical to your development as a computer programmer. Keep practicing!
When you have completed this assignment, submit the algorithm and TRI.BAS file as Assignment 02.06 - Game Skills 1 in the Dropbox area. After the assignment has been graded, please move on to the next assignment. |