Home » qbasic » 02 Tutorial – Software Development Basic QBasic

02 Tutorial – Software Development Basic QBasic

INTRODUCTION Session 1

Learning Objectives

At the end of this tutorial  you will know about:

  • The basic nature and features of a procedural programming language
  • Variables –  naming conventions and data types: text; integer; floating point; byte; date; Boolean. The benefits of appropriate choice of data type eg additional validation and efficiency of storage
  • How and when to use assignment statements; input statements; output statements

SECTION 1 – VARIABLES

A variable, simply defined, is a name which can contain a value. Programming involves giving values to these names and presenting them in some form to the user. A variable has a type which is defined by the kind of value it holds. If the variable holds a number, it may be of integer, floating decimal, long integer, or imaginary. If the variable holds symbols or text, it may be a character variable or a string variable. These are terms you will become accustomed to as you continue programming.

Here are some examples of values a variable might contain:

  • STRING       “hello, this is a string”
  • INTEGER     5
  • LONG         92883
  • SINGLE       39.2932
  • DOUBLE       98334288.18
  • INTEGER A 16-bit signed integer variable.
  • LONG       32-bit signed integer variable.
  • SINGLE   single-precision 32-bit floating-point variable.
  • DOUBLE double-precision 64-bit floating-point variable.
  • STRING * n% fixed-length string variable n% bytes long.
  • STRING   variable-length string variable.
  • INTEGER (int)- The largest integer value is 215= ± 32768. The smallest is 1, if you exclude zero.
  • LONG (lnt) – The largest integer value is 231= ±2147483648. The smallest is 1, if you exclude zero.
  • SINGLE (sng) – Single-precision floating-point variables can represent a number up to seven digits in length. The decimal point can be anywhere within those digits. The largest number is 9,999,999 the smallest is .0000001 if you exclude zero.
  • DOUBLE (dbl) – Double-precision floating-point variables can represent a number up to 15 digits in length. The decimal point can be anywhere within those digits. The largest number is 99,999,999,999,999,999 the smallest is .000000000000001 if you exclude zero.

 

Variable types support by C++

 

Name Description Size* Range*
char Character or small integer. 1byte signed: -128 to 127
unsigned: 0 to 255
short int (short) Short Integer. 2bytes signed: -32768 to 32767
unsigned: 0 to 65535
int Integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
long int (long) Long integer. 4bytes signed: -2147483648 to 2147483647
unsigned: 0 to 4294967295
bool Boolean value. It can take one of two values: true or false. 1byte true or false
float Floating point number. 4bytes +/- 3.4e +/- 38 (~7 digits)
double Double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
long double Long double precision floating point number. 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t Wide character. 2 or 4 bytes 1 wide character

Declaration of variables

In order to use a variable in BASIC we must first declare it specifying which data type we want it to be. The syntax to declare a new variable is to write the specifier of the desired data type (like int, bool, float…) followed by a valid variable identifier. For example:
DIM intNumber AS INTEGER

Remember  the largest integer value you can store in here is is 215= ± 32768. The smallest is 1, if you exclude zero. It will use up two bytes of RAM in the memory used by the program.  It uses two bytes to store number

DIM lngNumber AS LONG

The largest integer value is now 231= ±2147483648. The smallest is 1, if you exclude zero. LONG uses four bytes to store a number.

Note

You choose which on the basis of speed and ram usage.

Think

This is done by using the DIM statement. Say you wanted to make a variable called intNumber which would contain an integer (whole number, no digits after the decimal point).

Then you would use that variable as an integer. The word DIM actually originates from the word Dimension, but you won’t see why until we discuss the topic of arrays.

SECTION 2 – INTERACTING WITH THE COMPUTER

You know what a variable is and how to control them, it’s time you learned some programming. QBasic (like all other languages) is set up using pre-defined statements according to the syntax specified for that statement. It may be helpful to look in the help index to learn a statement, although I’ve heard many complaint’s that the help index is too hard. Indeed it is too hard for new programmers, but as you learn more and more statements and their syntaxes, you’ll become accustomed to the index and use it as a casual reference. Lets make a program that prints some text on the screen. Type qbasic at the DOS prompt and enter the following program.

Program Code – Type it in
CLSPRINT “This text will appear on the screen”

END

Test the program with this data
Input: NoneExpected out put:

This text will appear on the screen

Test results (Write results in the ox below)

 

Typing

QBasic allows to you define the ‘type’  of variable as number or characters like C++. It also allows loose typing, i.e where context tells you the type of data stored. Below is show how variables can be ‘typed,’ for loose typing all you need to do is give a variable a value:

name$ =” fred

age=3

pay = 3.45

This means spelling errors in variable names can cause problems. JavaScript is prone to this. QBasic provides good practice in dealing with this problem.

 

 

COLOUR

Below are the 16 colours QBASIC uses –

 

00 = black01 = dark blue

02 = dark green

03 = dark cyan

04 = dark red

05 = dark purple

06 = orange brown

07 = gray

08 = dark gray09 = light blue

10 = light green

11 = light cyan

12 = light red

13 = magenta

14 = yellow

15 = bright white

 

 softwareexampledoit   CLS

COLOR 12

PRINT “I’m light red text!”

COLOR 14

PRINT “I’m yellow text!”

END

Sets the screen display colours.

COLOR [foreground%] [,[background%] [,border%]]   Screen mode 0 (text only)

COLOR [background%] [,palette%]                   Screen mode 1

COLOR [foreground%]                               Screen modes 4, 12, 13

COLOR [foreground%] [,background&]                 Screen modes 7-10

foreground%   A number that sets the foreground screen colour.

background%   A number that sets the background screen colour.

border%       A colour attribute that sets the screen border colour.

palette%       A number (0 or 1) that specifies which of two sets of colour attributes to use:

The available colour attributes and values depend on your graphics adapter and the screen mode set by the most recent SCREEN statement

LET and INPUT

You have learned how to   use the PRINT and CLS commands. In this chapter, you will learn about   variables and the INPUT command.

What are variables? Variables are “boxes” in the computer’s memory to store a value – be it a number, name, decimal, currency amount, or what have you. There are two main types of variables – numbers and “strings,”   which are text variables.

The “numbers” category is further broken down into two areas.

Integer Numbers

Integers, they can be in the range of -32767 to 32767.

Long integers, they have a range of -2 billion to 2 billion. “Why not make all numbers long integers?” The memory of the computer, especially in QBasic, is limited – you should save as much space as possible. Only use long integers where they are necessary.

 

Floating-point” numbers.

These are decimal variables that can have very long decimal spaces.

Single types

Double types

You still may be in the dark as to how variables are used.

Variables are assigned a value using the LET command. For example:

LET intNumber = 123

This would assign a value of 123 to the short integer variable   ” intNumber.” You can use maths functions while assigning variables, too.   “LET intNumber = 4 * 12″ would make ” intNumber ” equal 48. You can increment variables like this:

LET intNumber = intNumber + 1

Or, you can make it ” intNumber = intNumber + 2, intNumber = number – 1,” and   so on. You can also add two variables together using the same syntax, or sequence of commands. You need a way to output these variables to the screen if you   want them to have any meaning to the user of the program.

You can use the PRINT command for this task easily:

PRINT intNumber

This would output to the screen the value assigned to “intNumber.”   If you want to include text before the number, you must use this format:

 softwareexampledoit    

Dim IntNumber AS INTEGER

CLS

LET intNumber = 100

PRINT “The number is”; intNumber

END

This would output, when run:

The number is 100

“How do I ask the user for something?”

Like strings, You do this by using the INPUT command. :

 softwareexampledoit   DIM IntNumber AS INTEGER

CLS

INPUT intNumber

PRINT “The number is”; intNumber

END

 

This would give the user a prompt for a short integer to assign to “number” and then print it out. You can also supply a prompt for INPUT   to use – like this:

 softwareexampledoit   DIM IntNumber AS INTEGER

CLS

INPUT “Enter a Number”; intNumber

PRINT “You typed “; intNumber; “.”

END

Exercise 1

Write a program to input the user’s name (STRING) and age (INTEGER) and output them using the PRINT command.

The first statement — CLS — stands for “clear screen.” It erases whatever was on the screen before it was executed. PRINT simply displays its argument to the screen at the current text cursor location. The argument in this case is the text enclosed in quotes. PRINT displays text within quotes directly, or it can display the value of a variable, like this:

Program Code – Type it in
DIM intA, intB AS INTEGERCLS

LET intA = 50

LET intB = 100

PRINT “The value of a is “; intA; ” and the value of b is “; intB

END

Test the program with this data
Input: NoneExpected out put:

The value of a is 50 and the value of b is 100.

Test results (Write results in the box below)

This will yield the output; The value of a is 50 and the value of b is 100. The semicolons indicate that the next time something is printed, it will be right after where the last PRINT statement left off. Remember that PRINT prints literally what is inside quotes, and the value of the variable which is not in quotes. intA and intB are integers containing values in this example, and their values are printed using the PRINT statement.

Say you want to interact with the user now. You’ll need to learn a statement called INPUT. INPUT displays a prompt (the first argument) and assigns what the user types in to a variable (the second argument)

 

Program Code – Type it in
DIM strName as StringDIM intAge as Integer

CLS

INPUT “What is your name? “, strName

INPUT “How old are you? “, intAge

PRINT “So, “; strName; “, you are “; intAge; ” years old. That’s interesting.”

END

Test the program with this data
Test 1 Input: Fred, 12

Expected out put:

So, Fred             you are 12 years old. That’s interesting.

Test 2 (Write in your own Test data and expected results)

Test results (Write results in the box below)
Test 1Test 2

This firsts asks the user for their name and assigns it to the string variable strName. Then the age is requested, and the result is printed in a sentence. Try it out! So what happens if you input I DON’T KNOW for the age prompt? You’ll get a weird message that says REDO FROM START. Why? The program is trying to assign a string (text) to an integer (number) type, and this makes no sense so the user is asked to do it over again.

Another cornerstone of programming is the conditional test. Basically, the program tests if a condition is true, and if it is, it does something. It looks like English so it’s not as hard as it sounds.

 softwareexampledoit   DIM intSelection as INTEGER

CLS

PRINT “1. Say hello”      ‘ option 1

PRINT “2. Say nice tie”         ‘ option 2

PRINT “Enter your selection ”

INPUT intSelection

IF intSelection = 1 THEN

PRINT “hello”

ENDIF

IF intSelection = 2 THEN

PRINT “nice tie”

ENDIF

END

The user is given a set of options, and then they input a value which is assigned to the variable selection%. The value of selection% is then tested, and code is executed based on the value. If the user pressed 1, it prints hello, but if they pressed 2, it prints nice tie. Also notice the text after the ‘ in the code. These are remark statements. Anything printed after a ‘ on a line does not affect the outcome of the program. Back to the actual code — but what if the user doesn’t input 1 or 2? What if they input 328? This must be taken into account as part of programming. You usually can’t assume that the user has half a brain, so if they do something wrong, you can’t screw up the program. So the ELSE statement comes into play. The logic goes like this:

 

IF the condition is true, THEN do something, but if the condition is anything ELSE, then do something else. The ELSE statement is used with IF…THEN to test if a condition is anything else.

 softwareexampledoit DIM intNumber as INTEGERCLS

INPUT “Press 1 if you want some pizza.”, intNumber

IF intNumber = 1 THEN

PRINT “Here’s your pizza”

ELSE

PRINT “You don’t get pizza”

ENDIF

END

 

Revision Session 2

Solving simple mathematical problems is one of the easiest tasks that you could do in BASIC. If you are more keen at writing a game or making graphics, you will have to be patient.:) We’ll deal about that later.

Before proceeding, you’ll have to understand some Basic concepts:

What is a program?

A program is a set of instructions that makes the computer work.

In most programs you will have to write you will have to think as follows:

What data do I need to give to the computer?

What operations will the computer have to perform with the data given?

What results will the program display

There are three parts in every task that we accomplish

Input –> Process –> Output

Input: What is needed

Process: What you need to do, calculate with the input

Output: The result obtained when the process has been done

 

Before beginning to code your program, it is important to write algorithms or pseudo codes in plain English that will outline what you want to do.

Algorithms

 An algorithm is a set of precise instructions for solving a problem.  Algorithms can be represented in plain English or in form of flowcharts

Here’s a simple algorithm for calculating the sum of 2 numbers

Get the first numberGet the second number

Calculate the sum

Display the sum

Let’s illustrate this concept using some mathematical examples:

We’ll be using colours to differentiate between the Input, Process and Output parts of the algorithm and program code.

Example 1: Finding the sum of 2 numbers
Write a program that will display the sum of numbers 8 and 12
Algorithm Program Code
Get the first number

Get the second number

Calculate the sum

Write down the result

DIM intNumber1, intNumber2 as INTEGERDIM intSum as INTEGER

intNumber1 = 8

intNumber2 = 12

intSum = intNumber1 + intNumber2

PRINT intSum

Output on Screen
20

You will have noticed that this program is not very explicit. The following code makes the presentation more understandable:

Enhanced Example 1: Finding the sum of 2 numbers
Program Code
DIM intNumber1, intNumber2, intSum AS INTEGERCLS

intNumber1 = 8

intNumber2 = 12

PRINT “This program finds the sum of two numbers”

PRINT “The first number is:”

PRINT intNumber1

PRINT “The second number is:”

PRINT intNumber2

intSum = intNumber1 + intNumber2

PRINT “The sum is:” ;

PRINT intSum

END

Output on Screen
This program finds the sum of two numbers”The first number is:

8

The second number is:

12

The sum is:

20

 

 

 

 

 

The use of semi-colons in the PRINT statement tells BASIC to keep the cursor on the same line.

 

Enhancing Program Presentation

It’s important for you to enhance you program by:

  • Documenting your program
  • Using meaningful variables
  • Saying what is the purpose of the program
  • Documenting your program

As you will notice later, programs may become huge, thus difficult to understand. The REM statement allows you to put remarks in your program. Any line starting with the REM statement is ignored at run-time. You can replace the REM statement by a single-quote ( ) symbol.

The two lines below are similar:

REM Program written by H.B.
‘ Program written by H.B.

The single-quote form of REM can also be placed at the end of a line

PRINT “Welcome everybody” ‘Welcome message

Using meaningful variables

The use single-letter variables may be easy to type especially if your typewriting speed is slow. However they may cause your program to be difficult to understand in the long-run. The use of meaningful variables makes your program easier to understand and maintain.  Here is an illustration:

ix = 2000y = 10

z = x * (100 + y)/100

Could you guess the code above? Now see how it is easy when meaningful variables are used:

LET intOldSalary = 2000LET intPercentageIncrease = 10

LET sngNewSalary  = intOldSalary  * (100 + intPercentageIncrease )/100

Saying what is the purpose of the program Many novice programmers learning how to program, often neglect to say what is the purpose of their program.  Example of an undocumented program:

LET a = 20LET b = 30

LET c = a * b

PRINT “The product is “; c

 

 

As we said above, use the REM statement to put remarks, use meaningful variables and, at run-time, say what your program is doing.

REM This program calculates the product of 2 numbers: 20 and 30.
LET intnumber1 = 20LET intNumber2 = 30LET intProduct = intNumber1 * intNumber2PRINT “This program calculates the product of 2 numbers”PRINT “The first number is”; intNmber1PRINT “The second number is”; intNumber2PRINT “The product is “; intProduct

 

Getting user input at run-time

Two commands or functions that allow you to get user input at run-time are:

The INPUT statement

The INKEY$ function

The INPUT statement

INPUT [variable]

Example:

Example 1: Asking the user for his nameFinding the sum of 2 numbers
Program Code
DIM strYourname AS STRINGPRINT “Enter your name:”;

INPUT strYourname

PRINT “Hello”; strYourname; “. Have a nice day!”

Output on Screen
Enter your name: LoulouHello Loulou. Have a nice day!

 

 

 

The lines:

PRINT “Enter your name:”;

INPUT strYourname

can also be written as:

INPUT “Enter your name:”; strYourname

Personally I prefer to use the first option because it is easier to understand and because it is the standard procedure in most programming languages.

 

Example 2: Asking the user for his score in English and Math and calculating the sum
Program Code
DIM intEnglish AS INTEGERDIM intMath AS INTEGER

DIM intSum AS INTEGER

 

PRINT “Enter your score in English:”;

INPUT intEnglish

PRINT “Enter your score in Math:”;

INPUT intMath

LET intSum = intEnglish + intMath

PRINT “The sum is:”; intSum

Output on Screen
Enter your score in English: 65Enter your score in Math: 59

The sum is: 124

 

 

Program Code – Type it in
DIM sngPi, sngRadius, sngArea as SINGLECLS

LET sngPi = 3.1415

INPUT “What is the radius of the circle? “, sngRadius

LET sngArea = sngPi * sngRadius ^ 2

PRINT “The area of the circle is “, sngArea

END

Test data
Radius Answer
2 12.568
3.2 32.17408
34.567 3754.305
2.345 17.27794

First, we’re defining the variable pi. It’s a single number, which means that it can be a fairly large number with some decimal places. The exclamation mark tells QBasic that sngPi is of the single type. Next, the user is prompted for the radius of their circle. Then the area is calculated. The * means “times,” and the ^ (carrot) means “to the power of.” sngRadius ^ 2 means “radius squared.” This could also be written as:

sngPi! * sngRadius * sngRadius.

 

Worked Examples

 

IPO TABLE For Block problem

Input Process Output
Height (H) Calculate volume by multiplying H X W X L Volume
Width       (W)
Length (L)
Input controls/Variables Calculations Output Controls/variables
sngHeight dblVolume       = sngHeight * sngWidth * sngLength dblVolume
sngWidth
sngLength

 

Structure Chart For Block problem

 

 

Program Code – Type it in
‘Program: Concrete Calculator’Author Michael Fabbro

‘Date 7 December 2003

‘This program will work out costings for quotes

DIM sngPi, sngRadius, sngArea as SINGLE

Dim sngHeight, sngWidth, sngLength As Single

Dim dblVolume As Double

Dim intStartLine As Integer

‘Main section

‘Set up input screen

intStartLine = 2

Color 10, 1

Cls

LOCATE 2, 25: Print “Concrete Calculator”

LOCATE intStartLine + 5, 5: Print “Height”

LOCATE intStartLine + 7, 5: Print “Width”

LOCATE intStartLine + 9, 5: Print “Length”

LOCATE intStartLine + 15, 15: Print “Volume”

LOCATE intStartLine + 5, 12: INPUT ” “, sngHeight

LOCATE intStartLine + 7, 12: INPUT ” “, sngWidth

LOCATE intStartLine + 9, 12: INPUT ” “, sngLength

‘Calculate Volume

dblVolume = sngHeight * sngWidth * sngLength

LOCATE intStartLine + 15, 24: Print USING “#####.##”; dblVolume;

END

Test data
Write in some test data here

Expected Results


  Exercises

  • Create the test data and state the output the program should produce for the problems below.
  • At least five different tests for each.
  • Write the QBASIC code to solve the problem – USE MEANINGFUL VARIABLE NAMES AND COMMENTS.
  • Print out copies of the program and the test results, keep them in a safe place as they are to be handed in.
  1. Add two numbers input from the keyboard together and output the answer.
  1. Subtract     two numbers input from the keyboard together and output the answer.
  1. Divide two numbers input from the keyboard together and output the answer.
  1. Add three numbers input from the keyboard together and then divide the sum by 3.Output the answer to this calculation as the average. (Do the problem     as two separate calculations not one)
  1. Rewrite exercise using just one calculation.
  1. Input the cost of an item and add 30% of its cost as profit to give the retail price. Output the retail price.

 

Print Using revisited

PRINT [#filenumber%,] USING formatstring$; expressionlist [{; | ,}]

LPRINT USING formatstring$; expressionlist [{; | ,}]

The PRINT with no arguments prints a blanks line so we can separate our answers.

Say you want to print something in a certain pre-defined format. Say you want to print a series of digits with only 2 places after the decimal point and a dollar sign before the first digit. To do this requires the PRINT USING statement, which is very handy in applications for businesses. The PRINT USING statement accepts two types of arguments. The first is a string which has already been defined. This is a special type of string, in that it contains format specifiers, which specify the format of the variables passed as the other arguments. Confused? You won’t be. Here’s a quick list of the most common format specifiers:

###         digits

&              Prints an entire string

\       \       Prints a string fit within the backslashes. Any thing longer is truncated

$$              Puts a dollar sign to the left of a number

.                Prints a decimal point

,                Prints a comma every third digit to the left of the decimal point.

  • And these can be combined in a format string to make a user defined way to print something.
  • So $$#,###.## will print a number with a dollar sign to the left of it. If the number has more than two decimal places, it is truncated to two. If it is more than four digits long to the left of the decimal place, it is also truncated to fit.
  • To use a PRINT USING statement, you must first define the format string containing the format specifiers. Then you use PRINT USING, then the name of the format string, and variable values to fill the places defined in the format string.

Here’s a code example:

 softwareexampledoit DIM intA AS INTEGERDIM strA AS STRING

intA = 123.4567

PRINT USING “###.##”; intA

PRINT USING “+###.####”; intA

strA = “ABCDEFG”

PRINT USING “!”; strA

PRINT USING “\ \”; strA

Program Code – Type it in
DIM strItemName AS STRINGDIM strFormat AS STRING

DIM intNumItems AS INTEGER

DIM sngItemCost, sngTotalCost AS SINGLE

CLS

‘ get user input

INPUT “Enter item name: “, strItemName

INPUT “How many items?: “, intNumItems

INPUT “What does one cost?: “, sngItemCost

CLS

‘ display inputs

strFormat = “\          \         #,###     $$#,###.##         $$#,###,###.##”

PRINT “Item Name             Quantity   Cost               Total Cost   ”

PRINT “————–         ——–   ———-   ————–”

sngTotalCost = intNumItems * sngItemCost

PRINT USING strFormat; strItemName ; intNumItems ; sngItemCost; sngTotalCost

END

Test the program with this data

First, we get the item name, number of items, and cost per item from the user. Then we clear the screen and define the format string to be used. It contains a static length string (text that will be truncated if it is too long), up to 4 digits for the quantity, 4 digits and two decimals for the item cost, and 7 digits and two decimals for the total cost. Then we print out some column headers so we know what each value will represent, and some nice lines to go under the column headers. Then the total cost is calculated by multiplying the number of items by the item cost. Finally, the four variable’s values are displayed under the column headers using the PRINT USING statement.

Volume of a Cylinder

Volume of a cylinder is the area of the a circle making up the clynder times the height of the cylinder.

Area = pi X radius X radius

Volume = Area X height

Or

Volume = pi X radius X radius X height

Or

Volume = pi X radius2  X height

IPO TABLE For Cylinder problem

Input Process Output
Radius (R) Calculate volume by multiplying pi * radius * radius * Length Volume
Length (L)
Input controls/Variables Calculations Output Controls/variables
sngHeight dblVolume       = 3.142 * sngRadius * sngRadius * sngLength dblVolume
sngRadius

Structure Chart For Cylinder problem

 

A Data Dictionary

 

Variable Name Description/Use Type Picture
sngHeight Height of Cylinder Single 9999.99
sngRadius Radius of Cylinder Single 9999.99
dblVolume Volumeof Cylinder Double 999999.99

 

Program Code – Type it in
‘Program: Concrete Calculator’Author Michael Fabbro

‘Date 7 December 2003

‘This program will work out costings for quotes

Dim sngLength, sngRadius As SINGLE

Dim dblVolume, As DOUBLE

Dim intStartLine AS INTEGER

Const conPi = 3.142

‘Procedure to calculate volume of a cylinder

Color 10, 1

CLS

‘draw input screen

LOCATE 2, 25: Print “Concrete Calculator”

LOCATE intStartLine + 5, 5: Print “Radius”

LOCATE intStartLine + 7, 5: Print “Length”

LOCATE intStartLine + 15, 15: Print “Volume”

LOCATE intStartLine + 5, 12: INPUT ” “, sngRadius

LOCATE intStartLine + 7, 12: INPUT ” “, sngLength

‘Calculate Volume

LET dblVolume = conPi * sngRadius ^ 2 * sngLength

LOCATE intStartLine + 15, 24: Print USING “#####.##”; dblVolume;

END

Test the program with this data
Pi Radius Volume
3.142 2 12.568
3.142 343 2155.412
3.142 5435435 34156274
3.142 0.000005 3.14E-05
3.142 -453 -2846.652
3.142 45.435 285.5135
3.142 boo #ERROR!

 

Enter the following programs and test they are working correctly

 

Exercise 3

Payroll Problem

 

Write an IPO table to process payroll. To calculate Pay multiply hours work time rate of pay.

Input Process Output
Input controls/Variables Calculations Output Controls/variables

 

 

Now draw a Sructure Chart for the problem

Program Code – Type it in
‘Program: Payroll program’Author Michael Fabbro

‘Date 7 December 2003

 

DIM sngRate, shgHours,sngWage AS SINGLE

DIM strName As STRING

DIM intPayNum, intStartLine As Integer

‘Main section

‘Set up input screen

intStartLine = 2

Color 10, 1

CLS

LOCATE 2, 25: Print “Concrete Calculator”

LOCATE intStartLine + 5, 5: Print “Name”

LOCATE intStartLine + 7, 5: Print “Payroll Number”

LOCATE intStartLine + 9, 5: Print “Pay Rate”

LOCATE intStartLine + 15, 15: Print “Hours”

LOCATE intStartLine + 5, 12: INPUT ” “, strName

LOCATE intStartLine + 7, 12: INPUT ” “, intPAyNum

LOCATE intStartLine + 9, 12: INPUT ” “, sngRate

LOCATE intStartLine + 11, 12: INPUT ” “, sngHours

‘Calculate Volume

LET sngWage = sngHours * sngRate

LOCATE intStartLine + 15, 24: Print USING   “#####.##”; sngPay;

END

Test the program with this data

Expected Results

Enter the following programs and test they are working correctly using the supplied test data. Note some of these programs may not be working correctly. If the program is not working properly state on the listing you produced what was wrong with it and what you did to correct it.

Exercise 4

Using this sheet annotate the listing below describing what each line does.

‘Program: Payroll program

‘Author Michael Fabbro

‘Date 7 December 2003

DIM sngRate, shgHours,sngWage AS SINGLE

DIM strName As STRING

DIM intPayNum, intStartLine As Integer

‘Main section

‘Set up input screen

intStartLine = 2

Color 10, 1

CLS

LOCATE 2, 25: Print “Concrete Calculator”

LOCATE intStartLine + 5, 5: Print “Name”

LOCATE intStartLine + 7, 5: Print “Payroll Number”

LOCATE intStartLine + 9, 5: Print “Pay Rate”

LOCATE intStartLine + 15, 15: Print “Hours”

LOCATE intStartLine + 5, 12: INPUT ” “, strName

LOCATE intStartLine + 7, 12: INPUT ” “, intPAyNum

LOCATE intStartLine + 9, 12: INPUT ” “, sngRate

‘Calculate Volume

sngWage = sngHours * sngRate

LOCATE intStartLine + 15, 24: Print USING “#####.##”; sngPay;

END
Glossary of Basic found in this Chapter.

PRINT

Writes data to the screen or to a file. LPRINT prints data on the printer LPT1.

Print [#filenumber%,]; [expressionlist]; [{; | ,}]

LPRINT [expressionlist] [{; | ,}]

filenumber%       The number of an open file. If you don’t specify a file number, PRINT writes to the screen.

expressionlist   A list of one or more numeric or string expressions to print.

{; | ,}          Determines where the next output begins:

; means print immediately after the last value.

, means print at the start of the next print zone.

Print zones are 14 characters wide.

See Also PRINT USING, LPRINT USING,   WIDTH,   WRITE

TAB

Moves the text cursor to a specified print position.

TAB(column%)

column%   The column number of the new print position.

Example:

Print Tab(25); “Text”

SPC

Skips a specified number of spaces in a PRINT or LPRINT statement.

SPC(n%)

n%   The number of spaces to skip; a value in the range

o through 32, 767#

Example:   Print “Text1”; Spc(10); “Text2”

CLS

Clears the screen.

Cls [{0 | 1 | 2}]

CLS     Clears either the text or graphics viewport. If a graphics viewport has been set using VIEW), clears only the graphics viewport. Otherwise, clears the text viewport or entire screen.

CLS 0   Clears the screen of all text and graphics.

CLS 1   Clears the graphics viewport or the entire screen if no graphics viewport has been set.

CLS 2   Clears the text viewport.

See Also VIEW     VIEW PRINT   WINDOW

REM

Allows explanatory remarks to be inserted in a program.

Rem remark

‘ remark

remark   Any text.

Remarks are ignored when the program runs unless they contain

metacommands. A remark can be inserted on a line after an executable statement if it is preceded by the single-quote (‘) form of REM or if REM is preceded by a colon (:).

Example:

   Rem   This is a comment.

    ‘     This is also a comment.

    Print “Test1”       ‘This is a comment after a PRINT statement.

    Print “Test2”:   Rem This is also a comment after a PRINT statement.


LET

 

Assigns the value of an expression to a variable.

[LET] variable=expression

■ variable     Any variable. Variable names can consist of up to 40                     characters and must begin with a letter. Valid characters are A-Z, 0-9, and period (.).

■ expression   Any expression that provides a value to assign.

■ Use of the optional LET keyword is not recommended. The

variable=expression assignment statement performs the same action

with or without LET.

 

LOCATE

Moves the cursor to a specified position on the screen.

CSRLIN returns the current row position of the cursor.

POS returns the current column position of the cursor.

LOCATE [row%] [,[column%] [,[cursor%] [,start% [,stop%]]]]

CSRLIN

POS(expression)

■ row% and column%   The number of the row and column to which the

cursor moves.

■ cursor%             Specifies whether the cursor is visible:

0 = invisible, 1 = visible

■ start% and stop%   Integer expressions in the range 0 through 31

that specify the first and last cursor scan lines.

You can change the cursor size by changing the

cursor scan lines.

■ expression         Any expression.

Task – Data Types
 

Task 1

Data types include string/ text; integer; floating point; byte; date; Boolean other eg long, double single. Using examples from Microsoft Qbasic and other languages, describe the benefits of the appropriate choice of data types available to the programmer, eg additional validation, efficiency of storage, strong typing.

Task 2

Write the following program for a mail order company. Input the purchase cost of an item add 30% to give the retail then add a delivery charge of 10% of the retail cost.

Ensure your  programs are documented with comments. You should submit

 

a) Structure chart

b) Test data and the results expected

c) A listing of the program’s code with comments

d) A screen grab of you program’s results with the test data.

e) A Data dictionary of variables.

 

 


Support