Module 4 - Game Skills 1
Assignment 4.06

 

Stay Tuned for Some Important Advice!

Hey, look at you! You are over half way finished with the course now.

Congratulations!

If there is one thing you have learned about computer programs, hopefully it is that you must "tell" the computer exactly what to do by writing code correctly...and that means paying attention to detail. The computer can't guess what you want to do--you have to tell it.

From now on, the programs you write will be a little different. They will be longer and more detailed. (No groaning, please.) You have reached a stage in learning to design and write computer programs where it is very important for you to see and study examples of code.

If you were in a regular classroom situation, your instructor would be able to go over examples on the board and you could ask questions; however, in the online setting, much more responsibility falls on you. In order to truly understand the concepts, you must read the material, study the examples, and ask yourself questions.

As you know, you have to define every single step in a program. Consequently, as we define more complicated problems, programs will get longer and more detailed. When this happens, some students get frustrated. You can avoid this with a simple strategy that you may have learned to improve study habits in other classes.

  1. Read the Objectives first so you know what the lesson is about. Note any new or highlighted words.
  2. Preview the lesson by scanning through the web page so you know what the major sections are. Get the "big picture."
  3. Read the material. You can't grasp the new information or details without reading the lesson.
  4. Study the examples of flowcharts and programs. They are there to assist you.
  5. Run the practice programs. These will help you gain a better understanding of how a program works.
  6. Make modifications. On your own, tinker with the practice programs and see what happens. This is probably the best thing you can do to increase your understanding.
  7. Use the five-step design process. You need a strategy for writing programs and this one works. Learn to apply it each time.
  8. Ask questions. If you don't understand something, e-mail or call your instructor and ask for help.
  9. Before you leave a lesson, make sure that you understand and can do the things listed in the Review box at the end of each assignment.

You can more easily learn to write computer programs if you read the material, study the examples, and ask yourself questions. Actually, that's what it takes to be successful in any course, right? So, let's get started with the last half of the course!

 

Objectives: After studying the Module 4 Game Skills 1, you will be able to:

  1. Apply the correct syntax when using READ - DATA statements.
  2. Apply the correct syntax when using PRINT USING to control the format of variables.
  3. Interpret a flowchart algorithm that uses READ - DATA statements and PRINT USING.
  4. Debug a simple program using READ - DATA statements and PRINT USING.
  5. Modify and expand on a program that uses READ - DATA statements and PRINT USING.

YOUR LESSON

Introduction

Earlier in Module 4, you learned that the READ-DATA statement is an excellent way to get large amounts of data into a computer program. There are several important things to remember about READ-DATA statements.

  • The keywords READ and DATA are a pair, so they must always be used together.
  • READ and DATA have a specific syntax. Variables in the READ statement are separated by commas. Data in the DATA statement are also separated by commas.
  • There is a one-to-one correspondence between the variables in READ statements and the data in a DATA statements.

READ-DATA statements can be used for almost any situation where you need to get data into a program, but one common use is applications where you need to enter repetitive sets of information.

It is also possible to combine numeric and alphanumeric formats in a single PRINT USING statement so you can print out numeric and string data at the same time. This is especially useful when printing tables of data...as you are about to see.


Practice Time

Let's imagine that your science teacher wants you to calculate the circumference (the distance around the equator) of each planet in the solar system. How would you go about writing a program to do that? If you apply the 5 step program development process, it should be pretty simple. So, let's give it a try.


Define the Problem

Here are the details we need to know.

  • The output of the program should be in a table format.
  • The title of the table (The Planets) should be centered over the columns.
  • The column headings should be Planet, Radius, and Circumference.
  • The value of pi, which will be needed to calculate the circumference, will be assigned as a constant.
  • The data required by the program will be the planet name (a string variable) and its diameter (a numeric variable).
  • We will have to calculate the radius and the distance around the equator of each planet (a numeric variable).
  • The information in the table will be printed out so that the numbers line up neatly in each column which will require the PRINT USING statement.
  • The foreground and background colors will be changed from the default conditions.

Define the Algorithm

Next, we need a flowchart. The flowchart should show reading in the data, any calculations, and what the output will be. Other details may be added as needed. Below is a flowchart depicting the algorithm for the program.



The "set" of data being read in this algorithm consists of:

the radius and the circumference
the diameter and circumference
the planet name and the diameter
pi and diameter


Notice the amount of detail shown in the flowchart in terms of the variable names, the calculations, and the print statements. Your flowcharts should also show this level of detail in future assignments.

The flowchart first shows that data will be read in as sets (the name and the diameter) for each planet.

Next, the radius of each planet is calculated.

After that, the value of pi is assigned as a constant.

Then, the circumference calculations are performed for each planet.

Next, the table heading and column headings are printed. Notice that the TAB values have been left blank. That's a detail that will have to be figured out once we start testing the program.

Finally, PRINT USING statements are used to print out the name of the planet, its radius, and its circumference. Notice that the spacing is pretty tight. It is hard to determine the spacing at this stage, so it is best to just indicate in general what will be output. The backslashes indicate a place for the planet name. The first set of number symbols (#) indicate the format of the radius (it will contain a comma). The last set of number symbols (#) indicate the format of the circumference (it will contain a comma and display two decimal places).

This is a concise, detailed flowchart that gives us a good "picture" of the algorithm for the program.


Code the Program

And now for the program. It is a little bit long because each READ statement only reads in one set of data from a DATA statement.

The program written from the flowchart is shown below. It has been color coded to enable you to see distinct parts. As usual, all of the QBasic keywords and functions are shown in red. The variables are shown in blue. The constant is shown in green. (Warning! Do not type this program in yet.)


REM The Planets
REM Penny Programmer
REM 02/20/2001
'
REM Input Data
READ planet1$, diameter1
READ planet2$, diameter2
READ planet3$, diameter3
READ planet4$, diameter4
READ planet5$, diameter5
READ planet6$, diameter6
READ planet7$, diameter7
READ planet8$, diameter8
'
REM Calculate Radius
radius1 = diameter1 / 2
radius2 = diameter2 / 2
radius3 = diameter3 / 2
radius4 = diameter4 / 2
radius5 = diameter5 / 2
radius6 = diameter6 / 2
radius7 = diameter7 / 2
radius8 = diameter8 / 2
'
REM Calculate Circumference
CONST pi = 3.14
circ1 = 2 * pi * radius1
circ2 = 2 * pi * radius2
circ3 = 2 * pi * radius3
circ4 = 2 * pi * radius4
circ5 = 2 * pi * radius5
circ6 = 2 * pi * radius6
circ7 = 2 * pi * radius7
circ8 = 2 * pi * radius8
'
REM Output
COLOR 2, 4
CLS
PRINT TAB(15); "The Planets"
PRINT "Planet"; TAB(15); "Radius (miles)"; TAB(35); "Circumference (miles)"
PRINT USING "\ \ ##,### ###,###.##"; planet1$; radius1; circ1
PRINT USING "\ \ ##,### ###,###.##"; planet2$; radius2; circ2
PRINT USING "\ \ ##,### ###,###.##"; planet3$; radius3; circ3
PRINT USING "\ \ ##,### ###,###.##"; planet4$; radius4; circ4
PRINT USING "\ \ ##,### ###,###.##"; planet5$; radius5; circ5
PRINT USING "\ \ ##,### ###,###.##"; planet6$; radius6; circ6
PRINT USING "\ \ ##,### ###,###.##"; planet7$; radius7; circ7
PRINT USING "\ \ ##,### ###,###.##"; planet8$; radius8; circ8
'
REM Data Statements
DATA Mercury,3032
DATA Venus,7521
DATA Earth,7926
DATA Mars,4213
DATA Jupiter,88732
DATA Saturn,74975
DATA Uranus,31763
DATA Neptune,30603
'
END

Notice that "good programming practice" has been followed in this example. Before leaving this example, be sure you recognize two important features.

  1. READ-DATA statements have been used to enter sets of data. Be sure you understand the syntax of these statements.
  2. PRINT USING statements have been used to control the horizontal positioning of data in the table and to format the alphanumeric and numeric variables that are being printed. Be sure that you understand the syntax of this function and how the appearance of the opt is controlled by the back slashes (\)and the number symbols (#).

Another important point is that not all of the information that was read in got printed out. Diameter was needed in order to do the calculations, but it did not appear in the output. It could have been included, but the programmer decided not to for some reason.


Test and Debug the Program

The program used in this example is available for you to download and study.

However, there is one catch...it doesn't work properly. There are a few mistakes in the program that you will need to find.

Several bugs are syntax errors and several are logic errors.

In order for you to gain some experience testing and debugging, download the file, run it, test it, compare it to the code provided above, and fix the mistakes. Re-save the program as planets.bas because you may need it again in the future!

 

Document the Program

As with previous examples, the main "documentation" is to include REM statements in the program to divide it up into easily identifiable sections. In this course, you probably will not have the need to write a documentation manual for any programs.


Check Your Understanding
DATA statements should never appear in flowcharts, but they always appear in programs anywhere above the END statement.

True
False


YOUR ASSIGNMENT

The program in this assignment is designed to give you experience in the following areas:

  • debugging a program (you've already done that)
  • modifying a program to do additional calculations
  • modifying a program to print additional results with PRINT USING

Details of this assignment are provided in the following pdf file which you should print out and carefully study.

Download and print this file now: 4_6_1.pdf

Submit your completed assignment "Assignment 04.06" to your instructor for a grade.


A QBasic Review

Before you move on to the next assignment, please make sure that you can do the following.

  1. Apply the correct syntax when using READ - DATA statements.
  2. Apply the correct syntax when using PRINT USING to control the format of variables.
  3. Interpret a flowchart algorithm that uses READ - DATA statements and PRINT USING.
  4. Debug a simple program using READ - DATA statements and PRINT USING.
  5. Modify and expand on a program that uses READ - DATA statements and PRINT USING.

READ - DATA statements give you some powerful techniques for getting sets of data efficiently into a program and PRINT USING allows you to format variables with great precision.


When you have completed this assignment in the Dropbox area, return to the Game Board. You are now ready for Assignment 4.07 Game Skills 2.S

previousnext