Software Development Topic 3 High Level Language Constructs.

Post on 03-Jan-2016

222 views 0 download

Transcript of Software Development Topic 3 High Level Language Constructs.

Software Development Topic 3

High Level Language Constructs

Resources

Theory for the unit is covered in pages of How to Pass Higher Computing

Throughout this topic you will be asked to complete practical tasks from the Software Development Using Visual Basic booklet

What you need to know

Description and exemplification of a number of programming construct

Features of high level languages Outline in pseudocode of high level language

constructs

Re-cap – What you already know about data types

A variable is used to store data in a program Variables can be of different types and must be

declared at the start of the program String Real Integer Boolean

Each data type has different memory requirements Declaring the data type allows a translator to

allocate the correct amount of memory

Variable types used in Visual Basic

Integer – Whole numbers from about -32,000 - +32,000

Long – Whole numbers from about -2,000,000,000 - +2,000,000,000

Single – Fractional numbers Double – Fractional numbers with more accuracy String – Text or words Boolean – Can have only 2 values T/F, Y/N, M/F

Practical Activity – 3 blocks

Complete the practical activities for Topic 1 from the practical booklet. This is a revision topic and should be completed quickly

Practical Software Development Topic 2

Strings and Branches

What we will learn

Enforced variable declarations String Functions

Enforced Variable Declarations

You can set up visual basic to make sure you declare your variables in every program.

If you do not declare your variables your program will not work correctly

Tools, Options, Editor, Required Variable Declaration

String Functions

LEN Ucase Lcase Asc Chr$

Each of these functions performs a task on a string. You will work out what each function does by creating a test program

Practical Activity – 1 block

Beginning at Topic 2 read pages 1 – 5 Create the function test programs detailed on

pages 6-7 – section 2.5

Report back by typing up a table of testing for each function. You should then write a sentence explaining what each function does. (bottom of page 7)

Parts of strings are extracted and used elsewhere in the program

Extracts letters from the middle of a string

Word = Computing, is, funKeyword = Mid$(word, 7,5)PRINT Keyword

What would the output of this program be?

String Operations - Substrings

Word = Computing is funMid$(word, 7,5)PRINT Mid

The code Mid$(word, 7,5) extracts 5 characters from the string variable called word, starting with the 7th character. That means the output would be

ing i

Remember a space counts as a character

String Operations - Concatenation

Concatenations means adding two strings together

LET part1 = Man

LET part2 = Chester

LET whole = part1 & part2

PRINT whole

What would be the output of this program?

Practical Activity

Beginning on page 9 of Unit 2 in your practical software development book work through the practical task 2.7 – Devising User IDs

Analysis and Design has been done for you Implementation has been party done You are wholly responsible for testing and

evaluation Submit printouts for implementation, testing

and evaluation

Practical Assessment

Well done you are now ready to complete your first practical assessment.

This will be done under assessment conditions.

You may refer to previous programs you have created or to your programming manual

You will be observed throughout the task First Practical Assessment

Past paper questions on string operations

A string variable mystring contains the word “elephant”. Using code from a programming environment with which you are familiar, show how you would extract the substring “ant” from mystring? 2006 paper, Q 4(b)

3 marks Describe what is meant by the string operation

concatenation. 2006 paper, Q 4(a)

1 mark

Suggested Solutions

Mid$(mystring, 6,3) extracts 3 characters from the string variable called mystring, starting with the 6th character. That means the output from mystring “elephant” would be “ant”

1 mark for Mid$ function1 mark for starting point (6)1 mark for 3 characters (3)

Concatenation is the process of joining strings together

1 mark

Formatting of Input and Output

You should be familiar with writing programs using different input and output formats. Visual basic has a range of input and output formats Text boxes List boxes Radio buttons Image boxes

It is not necessary for you to be able to use all of these but you should be aware of them

Multiple Outcome Selection Selection is used when a choice has to be made in a

program. The simplest form of selection uses an IF statement

IF MARK>=70 thenPRINT “A”ELSE IF MARK >=60 THENPRINT “B”ELSE IF MARK >= 50 THENPRINT “C”ELSE IF MARK >=45 THENPRINT”D”ELSEPRINT “No Award”END IF

CASE STATEMENT

CASE mark

>= 70 PRINT “A”

>=60 PRINT “B”

>=50 PRINT “C”

>=45 PRINT “D”

CASE ELSE PRINT “No Award”

Repetition in Programs

Fixed Loops – for ……….. Next Do …… Loop Until Do Until …….. Loop Do …… Loop While ……. Do While ……. Loop

Practical Activity – 1 block max

To ensure your understanding of loops (repetition) and selection (IF and CASE) you should chose 2 programs from 2.8 – 2.11 which you should implement and test.

Submit a structured listing for each program you implement

Topic 3

Loops (Revision)

Revision

Remember we have already covered loops during Standard Grade so the following programs should be done as quickly as possible.

Practical Activity – 2 blocks max

Revise counters by doing example 3.6.1 & 3.6.3 Revise for …. next loops by doing example 3.7 Revise Do …. Loop Until by doing example 3.12 Consolidate your learning by doing example 3.12.2

For each program you should submit a structured listing

For one program you should also submit analysis, design, test table with screen shots and produce a user guide, technical guide and evaluation

Homework Exercise

Complete homework exercise revising points covered so far

Topic 4

1 Dimensional Arrays

Arrays

The same type of data grouped together is called an array

The array is given a meaningful name Each part of the array is called an element Each element is given a unique number

Declaring arrays

You must declare the name and the number of elements in an array

DIM name(9) as stringDIM mark(9) as integer

This tells the program the dimension of the array – ie the number of elements

Using Arrays in Visual Basic

Visual basic array’s always start with element number 0

To set up an array to hold 5 names you would declare as follows

DIM pupil_name(4) As String

This instructs visual basic to set aside 5 elements to store 5 names

(Filling an array with 6 numbers entered by the user)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Number(0) Number(1) Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

0

Number(0) Number(1) Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

0

Numbers(0) = inputbox(“ Please enter a number”)

Number(0) Number(1) Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

0

Number(0)

8

Number(1) Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

0

Number(0)

8

Number(1) Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

1

Number(0)

8

Number(1) Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

1

Numbers(1) = inputbox(“ Please enter a number”)

Number(0)

8

Number(1) Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

1

Number(0)

8

Number(1)

12

Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

1

Number(0)

8

Number(1)

12

Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

2

Number(0)

8

Number(1)

12

Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

2

Numbers(2) = inputbox(“ Please enter a number”)

Number(0)

8

Number(1)

12

Number(2) Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

2

Number(0)

8

Number(1)

12

Number(2)

1

Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

2

Number(0)

8

Number(1)

12

Number(2)

1

Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

3

Number(0)

8

Number(1)

12

Number(2)

1

Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

3

Numbers(3) = inputbox(“ Please enter a number”)

Number(0)

8

Number(1)

12

Number(2)

1

Number(3) Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

3

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

3

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

4

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

4

Numbers(4) = inputbox(“ Please enter a number”)

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4) Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

4

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4)

17

Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

4

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4)

17

Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

5

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4)

17

Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

5

Numbers(5) = inputbox(“ Please enter a number”)

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4)

17

Number(5)

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

5

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4)

17

Number(5)

5

Dim Numbers(5) as integer For counter = 0 to 5

Numbers(counter) = inputbox(“ Please enter a number”)

Next counter

Counter

5

Number(0)

8

Number(1)

12

Number(2)

1

Number(3)

21

Number(4)

17

Number(5)

5

Glossary Definitions

At the back of your jotter write a glossary definition of

Array Element

Practical Activity

You should now complete Topic 4 from the practical booklet

Modularity

Code should be broken into manageable sections.

Visual basic used two ways of breaking up a program Subroutines Functions

Subroutines

Sections of code which do a specific task Can be called during the running of the

program

Functions

A function is a sub-program which returns a result

Functions can be pre-defined or user-defined Pre-defined functions we have already met:

LEN RND UCASE LCASE

User defined functions

You must know The name of the function The type of result it will return What input data is needed (type)

Benefits of User defined functions

Keeps main code simpler and more readable Same function can be reused elsewhere in

the code Can be saved in a module library

Functions

Like a subroutine but has a value which can be assigned to a variable

These variables can be Local variables Global variables

Practical Activity Complete the practical activities for Topic 5 this is

straight forward topic and should be completed quickly

5.5.1 5.5.3 Either 5.5.4 or 5.5.5 5.5.6 – full analysis, design, implementation, testing

and evaluation (all but implementation and testing can be done at home)

If you cannot complete all of this within class time you must catch up at home

Written Questions

Answer questions 23 – 26 on pages 57 and 58 of How to pass Higher Computing

Complete Homework Revision Ex 2

Section 6

Modular Programming

Real life programming

Examples so far have been short, simple programs

Higher level programming requires the use of modules and parameters

Using variables in Higher programming

You need to be familiar with and be able to use the following types of variables Local variable Global variable Variables (parameters) passed by reference Variables (parameters) passed by value

Local Variables

We have already used local variables in the functions we created earlier

A local variable is defined for use in one part of a program

It can only be assigned a value inside a procedure, sub-routine or function

Using local variables reduces the chances of a variable being changed accidentally if the same variable is used in other parts of a program

Global Variables

Can be assigned a value anywhere in a program

Should only be used for data that needs to be shared between different procedures

Should be used with care due to the fact that they have the ability to change a value throughout a whole program

Glossary Definition

At the back of your jotter write a glossary definition for Variable Local Variable Global Variable

Data Flow

Variables which represent data can be used in many different parts of a program

Programs are divided into sub-routines (also called modules)

The movement of data between subroutines is controlled by using Parameters

Parameters

A parameter is a variable or value that is passed into or out of a subroutine.

Parameters are used to control data flow in a program

When a subroutine is used within a program, the calling program must pass parameters to it. This is called Parameter Passing

Practical Task

Create the program on pages 5 – 7 of your practical booklet (Topic 6) 6.5.1

Parameter Passing

Parameters can be passed (called):

Called/Passed by Reference or

Called/Passed by Value

Passed by Value (in)

The current value is passed into the subroutine

Any changes made do not affect the program Used when the variable content does not

need passed out.

The actual variable is passed into the subroutine

The variable may be updated and the new value passed out.

Used when the variable content needs to be passed out.

Pass by Reference (out)

Diagram: Data Flow

Area of Rectangle

Enter Length

Enter Breadth

Calculate Area

Display Area

OUT: breadth OUT: areaIN: length

length

breadth

area

PARAMETERS

OUT: length IN: breadth IN: area

Algorithm showing Data Flow

1. Get length of rectangle (OUT: length)

2. Get breadth of rectangle (OUT: breadth)

3. Calculate the area (IN: length,breadth OUT: area)

4. Display the area (IN: area)

Glossary definitions

At the back of your jotter, write a glossary definition of

Passing a parameter by reference Passing a parameter by value

Using a structured chart to show data flow

Your teacher will take you through the data flow diagram on pages 8 – 10

An alternative method is shown on pages 10 - 11

6.5.2.2

You should now add the parameters to the code you created earlier

Exemplar 1 (Parameter Passing)

Software Specification

Design, implement and test a program that will ask the user to enter the radius and height of a cylinder. The volume will then be calculated and displayed.You should use a structured chart to show the data flow before you do the algorithm in pseudocode

Diagram: Data Flow

Volume of Cylinder

Get Input Calculate Volume

Display Volume

OUT: height OUT: VolumeIN: radius

radius

height

volume

PARAMETERS

OUT: radiusIN: height

IN: Volume

Exemplar 1Algorithm (With Data Flow)

1. Declare Variables2. Get Input Out: radius, height

2.1 Get and store Radius2.2 Get and store Height

3. Calculate Volume in: radius, height

out: Volume

3.1 Volume = 3.14*radius*radius*height

4. Display Volume in: Volume

4.1 Display Volume of the cylinder

Implementation – Main Program

Private Sub CmdRun_Click()

!Declare the variables – Step 1. of Algorithm

Dim radius As Single, height As Single, Volume As Single

!Main part of the program – Steps 2. 3. & 4.of Algorithm

Call Get_input (radius, height)

Call Calculate_Volume (radius, height, Volume)

Call Display_Volume (Volume)

End

Sub Routines – refinements of Algorithm

Private Sub Get_input (ByRef radius As single, height As single)

radius = InputBox ("Enter the radius") (step 2.1 of algorithm)

height = InputBox ("Enter the Height") (step 2.2 of algorithm)

End Sub

Private Sub Calculate_Volume(ByVal radius as Single, height As Single, ByRef Volume as Single)

Volume = 3.14 * radius * radius * height (step 3.1 of algorithm)

End Sub

Private Sub Display_Volume(ByVal Volume As Single)

PicDisplay.Print Volume (step 4.1 of algorithm)

End Sub

Exemplar 2 (Parameter Passing)

Using the previous example complete the following task

Design implement and test a program that will ask the user to enter the distance travelled and the time taken for a journey. The speed will then be calculated and displayed.

Exemplar 3 (Parameter Passing)

Software Specification

Design, implement and test a program that will ask the user to enter the name of a pupil followed by four exam marks (out of 100). The average mark will then be calculated and the pupil name and average mark should be displayed.

Exemplar 3 – Suggested Solution

Data flow is important to ensure

correct data types are used data does not get mixed up in a

program data only gets changed if desired the correct data is used in the correct

sub-routine (module)

Homework Exercise 3

Practical Activity

To ensure you understand what happens in a program when a program is passed by value or passed by reference you should complete practical task 6.6.1 and 6.6.2

Actual and Formal Parameters

Past paper questions on variables

A program uses global variables and local variables. Explain each of these terms. (2006 Past Paper, Q5)

2 marks

Suggested answer

Past paper question (2002, Q18)A program is used to analyse the number of road

traffic accidents in six areas. The names of the areas and the number of accidents are stored in 2 separate arrays. Here is a possible algorithm for the part of the program which finds the area with highest number of accidents

2. Find area with highest number of accidents2.1 find position of maximum number of

accidents in the array2.2 print name from corresponding position in

area array

Part (a) of question

Step 1 requires 2 parameters. Identify these parameters and state their data types and methods of parameter passing

Break this question down before attempting to answer it

identify the parameters

state their data types

state methods of parameter passing

Suggested solution part (a)

A variable to store the position of the maximum number of accidents will be needed (Position)

As this variable will store the position of the variable maximum the variable must be a whole number therefore the data type must be an integer

It is presumed the position must be passed to other parts of the program (eg display) so therefore this variable should be passed by reference

Written Task

Answer questions 17 – 22 on page 57 of How to Pass Higher Computing

End of Topic Activities

Complete end of topic evaluation sheet Complete end of topic ActiVote Assessment

Learning Outcomes - High level programming language constructs

Description and exemplification of the following constructs in pseudocode and an appropriate high level language: string operations (concatenation and substrings), CASE (or equivalent multiple outcome selection)

Description and exemplification of real, integer and boolean variables; and 1-D arrays

Description and exemplification of procedures/subroutines/subprograms, user-defined functions,

modularity, parameter passing (in, out, in/out), call by reference/value, local and global variables, scope of a variable