Module 4 - Game Skills 3
Assignment 4.08


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

  1. Create a program using READ - DATA statements to enter sets of data.
  2. Calculate the total of a set of data.
  3. Use LOCATE to control the horizontal and vertical print position of information on the screen.
  4. Use PRINT USING to control the horizontal placement of numeric and string data in a table.

YOUR LESSON

Introduction

Are you becoming more comfortable with READ - DATA and PRINT USING? When you need to store many sets of data within a program, READ - DATA is very efficient. When you need to precisely control the output to the screen, PRINT USING is the best solution.

In this lesson, we will add one more piece to the puzzle of placing data on the screen...LOCATE. When you need to control the horizontal and vertical position of output on the screen, LOCATE is the only efficient alternative. A statement like LOCATE 7, 12 indicates that printing will begin on the 7th row and in the 12th column. In this assignment, you will continue to focus on READ - DATA and PRINT USING, but we will add a simple use of LOCATE as well.

Several examples that have been used for illustration purposes and several programs that you have written have involved tables of data. Often such tables contain a final row that shows a total of one or more columns. Setting up assignment statements to calculate totals is very easy.

The program examples have been written in sections and are often color coded to help you see the program design better.


Positioning with LOCATE

There are some fairly sophisticated ways of using LOCATE to control the horizontal and vertical position of information on the screen; however, those are beyond the scope of this course. So, we will concentrate on a very simple use of LOCATE...to position a table in the center of the screen without using multiple blank PRINT statements to force the table down the screen.

This is easily and simply illustrated with the planets program previously used as an example. A part of the output section of that program is shown below, but this time one LOCATE statement has been added. (Notice that a remark was placed on the same line.)

REM Output
COLOR 2, 4
CLS
LOCATE 4, 32 'This positions
the following title at Row 4 and Column 32
PRINT "The Planets"
PRINT "Planet"; TAB(15); "Radius (miles)"; TAB(35); "Circumference (miles)"
PRINT USING "\ \ ##,### ###,###.##"; planet1$; radius1; circ1
PRINT USING "\ \ ##,### ###,###.##"; planet2$; radius2; circ2

This is about the simplest way to use a LOCATE statement. It's not fancy, but it is effective! We could have put in four blank PRINT statements and accomplished the same thing, but using LOCATE is better programming practice.


Calculating Column Totals

Tables of data often include totals at the bottom of one or more columns of data. Since you know how to use variables, setting up assignment statements to calculate totals is not difficult. Study the following example carefully.

Define the Problem

The Programming teachers had a four week bake sale. Mr. Putney's specialty is Chocolate Cake which sells for $8.25 each. Mr. Jordan's specialty is Key Lime Pie which sells for $7.75 each. Mrs. Atwill's specialty is Chocolate Chip Cookies which sells for $6.25 per dozen. Below are the sales results for each week.

Putney: 15, 13, 18, 17
Jordan: 17, 19, 13, 15
Atwill: 23, 20, 41, 16

We need to calculate how much money was made on the bake sale by each teacher for each week and calculate a total. The results should be printed in the center of the screen with appropriate table headings, column headings, the amount of money taken in each week, and the total for each teacher. The final output is shown below. Color could be added to make it more attractive.


  
Bake Sale
Mr. Putney
Mr. Jordan
Mrs. Atwill
Week 1
$123.75
$131.75
$143.75
Week 2
$107.25
$147.25
$125.00
Week 3
$148.50
$100.75
$256.25
Week 4
$140.25
$116.25
$100.00
Total
$519.75
$496.00
$626.00

We need to write a program to create this table from the data given.

Define the Algorithm

The general algorithm for the Bake Sale project is shown below. Examine this flowchart very carefully. Pay very close attention to how the totals for each teacher are calculated. The symbols have been color coded to help you visualize the design of the algorithm. A key is provided to the right of the flowchart.

Start and Stop indicate the beginning and end of the program.
The cost of each item is assigned to a variable. For example, the cost of a cake is $8.25.
The person's name and his/her weekly sales are read into a set of variables for each teacher. For example, Mr. Putney's name is read into tch1$ and his weekly sales numbers are read into P1, P2, P3, and P4.
The weekly sales in dollars is calculated for each teacher. For example, P1 * cake calculates the dollar amount for Mr. Putney for Week 1 and assigns that amount to Psales1.
The total for each teacher is found by adding the sales amount for each week. In the first case, Mr. Putney's total sales is found by adding up the weekly sales (Psales1+Psales2+Psales3+Psales4) and assigning that to the variable Ptotal.
The table heading, column headings, weekly sales amounts, and the column totals are printed out. Notice that the TAB positions and the PRINT USING formats have not been included. These are details that are not needed at this point. In addition, including those details at this stage would make the flowchart symbol really big!
 
There is really only one thing new about this flowchart--the calculation of totals. In order to calculate the total for each teacher (in the purple rectangles), you must first know the amount sold for each of the four weeks.

Code the Program

The program has already been written so that you can study it carefully.

This program may look a bit intimidating, but it is really simple and straightforward. Believe it or not, there are only two things that are different from what you have seen in any other program up to this point. Let's dissect it to gain a better understanding of what is happening.


Section 1: Assigning Data

First, the price of each item is assigned to a variable. Then, three READ statements are used to store each teacher's name and how much they sold during week 1, week 2, week 3, and week 4. Notice that the DATA statements are at the bottom of the program so that they are out of the way.

REM Assign Data
LET cake = 8.25
LET pie = 7.75
LET cookies = 6.25
READ tch1$, P1, P2, P3, P4
READ tch2$, J1, J2, J3, J4
READ tch3$, A1, A2, A3, A4


What does the variable P2 represent in the first READ statement?

The number of pies Mr. Jordan sold the first week.
The number of cookies Mrs. Atwill sold the second week.
The number of cakes Mr. Putney sold the first week.
The number of cakes Mr. Putney sold the second week.

Section 2: Performing the Calculations

Notice that the calculations are divided into three groups, one group for each teacher. The first four calculations in each group determine the dollar total for each week. The fifth calculation in the group adds up the weekly sales for a teacher to find the total.

REM Calculations
' Results for Putney
LET Psales1 = P1 * cake
LET Psales2 = P2 * cake
LET Psales3 = P3 * cake
LET Psales4 = P4 * cake
LET Ptotal = Psales1 + Psales2 +Psales3 + Psales4
' Results for Jordan
LET Jsales1 = J1 * pie
LET Jsales2 = J2 * pie
LET Jsales3 = J3 *pie
LET Jsales4 = J4 * pie
LET Jtotal =Jsales1 + Jsales2 + Jsales3 + Jsales4
' Results for Atwill
LET Asales1 = A1 * cookies
LET Asales2 = A2 * cookies
LET Asales3 = A3 * cookies
LET Asales4 = A4 * cookies
LET Atotal = Asales1 + Asales2 + Asales3 + Asales4


Check
Your Understanding

The following statement represents the total of Mrs. Atwill's sales for all four weeks.

Atotal = Asales1 + Asales2 + Asales3 + Asales4


True
False

Section 3: Printing the Output in a Table

There are several important things to notice in this section.
1. Several rows of output have been positioned using the LOCATE statement.
2. TAB has been combined with the PRINT USING statement.
3. On each row, the Week has been included inside the format of the PRINT USING statement.


REM Output
COLOR 2, 0
CLS
LOCATE 5, 28
PRINT "Bake Sale"
LOCATE 7, 1
PRINT TAB(15); tch1$; TAB(30); tch2$; TAB(45); tch3$
COLOR 1, 0
PRINT TAB(6); USING "Week1 $###.## $###.## $###.##"; Psales1; Jsales1; Asales1
PRINT TAB(6); USING "Week2 $###.## $###.## $###.##"; Psales2; Jsales2; Asales2
PRINT TAB(6); USING "Week3 $###.## $###.## $###.##"; Psales3; Jsales3; Asales3
PRINT TAB(6); USING "Week4 $###.## $###.## $###.##"; Psales4; Jsales4; Asales4
COLOR 4, 0
LOCATE 13, 1
PRINT TAB(6); USING "Total $###.## $###.## $###.##"; Ptotal; Jtotal; Atotal

Section 4: The Data

Last but not least are the DATA statements.

DATA Mr. Putney,15,13,18,17
DATA Mr. Jordan,17,19,13,15
DATA Mrs. Atwill,23,20,41,16
END

 

There you have the program dissected. This is a strategy you should use when studying the design of a program. Divide and conquer!



YOUR ASSIGNMENT

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

  • using READ - DATA statements to enter sets of data
  • creating slightly more complicated calculations
  • controlling the appearance of output with PRINT USING statements
  • calculating totals

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_8_1.pdf

Submit your completed assignment "Assignment 04.08" to your instructor for a grade in the Dropbox area.



A QBasic Review

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

  1. Create a program using READ - DATA statements to enter sets of data.
  2. Calculate the total of a set of data.
  3. Use LOCATE to control the horizontal and vertical print position of information on the screen.
  4. Use PRINT USING to control the horizontal placement of numeric and string data in a table.

Calculating a total is an important concept. Be sure you understand how to do that before moving on.


After you have completed this assignment, return to the Game Board and continue with Assignment 4.09 Game Skill 4.

previousnext