cg1.4: algorithms and programming programming · low-level programming languages have a very low...

9
AS Computing Booklet Serial II Bourne Grammar School by Mrs. X. Ellis All Rights© Reserved CG1.4: Algorithms and Programming Programming Coding Conventions In the world of software development, there are a number of important terms you need to be familiar with. Self-documenting identifiers are variable names which match the purpose they’re being used for. For example, x isn’t self identifying, whereas strFirstName is. Self documenting identifiers can be used for variable names, subroutine/sub names, function names. It is important to use self documenting identifiers because it is easy to read and follow the code for the original programmers as well as other programmers at a later time. Program layout refers to the use of indentation of IF statements, loops (FOR .... NEXT)and WITH blocks. This helps a developer to make sure they don’t miss any END IF or END WHILE statements when writing their code. Visual Basic handles this automatically, so you may not have noticed it. E.g. IF x>2 then DoThis() DoThat() END IF Annotation refers to the process of writing comments into application code. This is done to help the developer (or future developers) work out how the program works, which is especially helpful for the more complex functions of a program. Annotations in Visual Basic usually begin with a single quote and appear right above the lines of code the annotations are commenting on. Variables are ways of storing text, numbers and other items of data inside an application. The contents of a variable can be changed by the application code by referring it by its name- variable name. Constants are similar to variables, but the values assigned to them cannot be changed once entered. Pi could be stored as a constant, for instance.

Upload: ngohanh

Post on 29-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

CG1.4: Algorithms and Programming

Programming

Coding Conventions

In the world of software development, there are a number of important terms you need to be familiar with. Self-documenting identifiers are variable names which match the purpose they’re being used for. For example, x isn’t self identifying, whereas strFirstName is. Self documenting identifiers can be used for variable names, subroutine/sub names, function names. It is important to use self documenting identifiers because it is easy to read and follow the code for the original programmers as well as other programmers at a later time. Program layout refers to the use of indentation of IF statements, loops (FOR .... NEXT)and WITH blocks. This helps a developer to make sure they don’t miss any END IF or END WHILE statements when writing their code. Visual Basic handles this automatically, so you may not have noticed it. E.g. IF x>2 then DoThis() DoThat() END IF Annotation refers to the process of writing comments into application code. This is done to help the developer (or future developers) work out how the program works, which is especially helpful for the more complex functions of a program. Annotations in Visual Basic usually begin with a single quote and appear right above the lines of code the annotations are commenting on. Variables are ways of storing text, numbers and other items of data inside an application. The contents of a variable can be changed by the application code by referring it by its name- variable name. Constants are similar to variables, but the values assigned to them cannot be changed once entered. Pi could be stored as a constant, for instance.

Page 2: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

It is good programming practice to use constants in computer programs because if you want to change the value of the constant you only have to change it once at the top and its value will be updated throughout the whole program. Local variables are those which are created inside subroutines and functions. As soon as the function code has been run, the variable is “destroyed” so that the memory it occupied can be used by something else. This is the most efficient way to use variables in most cases. Global variables are declared at the top of an application and can be used by all the functions and subroutines in the application. These are used when a particular variable needs to be accessed throughout a program. Modules or standard modules are collections of functions and subroutines gathered into a single location which can be imported into numerous different programs the developer creates. These are helpful, as it saves the developer a large amount of time, and it means that the code used will be more reliable as it’ll be well tested and improved. Functions are small segments of code that perform a task, and return a value. Examples would be a piece of code that turns a string into upper case, or adds two numbers together. These are helpful whenever a particular process will be performed from several different locations in your code. A standard function is a function provided by a compiler or an interpreter which is so common that it is worth providing as part of the language. For example, the random number generator used in VB RND(). A language that had many standard functions available would be better as the programmer would not have to write so much code. Examples include standard mathematical operations such as Square Root, random number generators or standard I/O routines such as a message box in VB. Subroutines are pieces of code that perform a particular task. The difference between a function and a subroutine are that a subroutine doesn’t return a value to the user. Examples of subroutines are to hide all the controls on a form, or to print off an open document. The advantage of writing user-defined subroutines is that it is easier to write small pieces of code that perform specific tasks and then put them all together to solve the big problem, rather than try to write a large program in one go. If the programmers want to improve the performance of a program you can swap an existing subprogram with an updated version without having to re-write the rest of the code. Those subs can also be re-used by other programs. Selection occurs in an application by the use of IF statements. This allows actions to be taken based on choices.

Page 3: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

E.g. IF x>2 THEN msgbox “Hello” ELSE msgbox “Goodbye”

Repetition is a process by which steps can be taken over and over again until a certain condition is met. This is handy for looking through large amounts of data, or performing calculations, and is done by either a WHILE loop, or a FOR… NEXT loop. For example: For i = 0 to 5 Msgbox (“Greetings, earthling number “ & i) Next It is a good practice to organise a program into small and well documented sections. This is because:

• easier to read by the original programmers as well as other programmers • easier to debug

High-level programming languages have a higher level of separation of the language from the underlying computers (high level of abstraction). It is close to natural language in syntax (the way it is written). The code in some high level languages is translated to machine code using a compiler, whereas some languages use an interpreter or combinations of both. Features of a high-level language include: procedural (examples: C, FORTRAN, BASIC)), visual (examples: Kodu, Scratch), mark-up (examples: HTML, XML) and event driven (example: Visual Basic). Those high level languages provide the following features:

• insert hyperlinks • format text and graphics • include platform independent code • include/embed video and/or animation • include/embed sound • add buttons, menus, frames, etc.. to allow creation of easy to use interface • use colours, icons, images, etc… to create GUI • create or use events for buttons, menus, etc

Situations where a high-level programming language will be more suitable:

• Web applications where multimedia contents, menus, icons, formatted text, graphics, embedded links etc can be easily created.

• Appealing Graphic user interfaces: fonts, colours, formatted text, sounds, images., videos, events, menus, buttons, icons, etc to make user interface appealing, and can be tailored to client’s specific requirements.

Low-level programming languages have a very low level of separation of the language from the underlying hardware. It is normally referred to as machine code or assembly code. Low-level languages are difficult to use due to its many hardware specific details. But low-level languages can be made to run very quickly with little memory requirement.

Page 4: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

Situations where low-level programming languages are more appropriate:

• Operating systems because execution speed is critical and size of code needs to be small

• Embedded system because size of code needs to be small • Systems with primitive processors which have limited instruction set

Testing of an application happens in three stages:

Alpha – this is early testing undertaken by the development team to check the program does what it’s designed to. Beta – Once alpha testing is complete, the new software is given to end users to experiment with. They may find bugs that the developers overlooked, which they will report back for fixing.

Acceptance – Once the beta stage is complete, a final round of testing is performed by the client to ensure they’re happy to sign off the project as a complete application.

Techniques in problem solving and software development

Top-down approach in analysing a problem means the problem is broken into smaller sub problems and in turn those sub problems are broken into smaller problems that can be solved in just a few steps. This problem solving approach is also called “Divide and Conquer”. It is one of the most fundamental problem solving ideas. Professional programmers start solving a problem by starting naming the abstract modules at a high level, then sub divide those modules into sub modules. Modules here meant modular (individual) problems not the VB modules.

Page 5: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

Algorithms

An algorithm is the sequence of steps taken to solve a problem.

Another practice in problem solving and software development is to use pseudo-code to describe an algorithm. Pseudo-code is an artificial and informal language that helps programmers develop algorithms without worrying too much about the correct syntax. It consists of a series of commands that show the purpose of the program without bogging down with the actual chosen programming language. The programmers will need to convert the pseudo-code into high level code at a later date.

There are no Pseudo-code standards but some basic rules like all statements showing "dependency" are to be indented. The following is an example of a pseudo-code for assigning “Passed” or “Failed” for pupils’ test scores:

If student's grade is greater than or equal to 60

Print "Passed" else

Print "Failed"

This example shows the use of repetition in a pseudo-code: Repeat Loop Compare adjacent values, Swap if necessary End loop Until no more swaps are needed

There are many well defined and tested algorithms for some common problems. Programmers tend to use those algorithms in developing their solutions. Some well-known algorithms include searching and sorting algorithms. You should understand and describe the following sorting algorithms.

Shell Sort

The idea of Shell sort is the following:

a. arrange the data sequence in a two-dimensional array

This  statement  depends  on  the  If  statement,  so  it  is  indented.  

This  statement  depends  on  the  else  statement,  so  it  is  indented.  

Page 6: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

b. sort the columns of the array

For example: to sort the number sequence using Shell sort: 2,8,4,6,3,1

1) Choose a step size, in this case 3

2) Create a horizontal grid and place the elements such that you have 3 columns

2 8 4

6 3 1

3) Sort each column, and you now have a new sequence: 2,3,1,6,8,4

2 3 1

6 8 4

4) Reduce the step size by 1, and repeat steps 2-3.

2 3

1 6

8 4

Now you have another new sequence: 1,3,2,4,8,6

5) Repeat step 4. At this final step, only the number 2 and 3, and the number 6 and 8

need to be swapped.

1

3

2

4

8

6

Shell sort is much faster when sorting large amounts of data. When sorting smaller set of data, Bubble sort is more efficient.

Bubble Sort

Bubble sort works by comparing and swap two adjacent values. The process is repeated until there are no more swaps needed and the sequence thus in order.

Take the sequence for the above Shell sort for an example: 2,8,4,6,3,1

1 3

2 4

8 6

1

2

3

4

6

8

Page 7: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

1. First pass of the comparing and swapping:

1) Compare the 1st and the 2nd values and swap if not in order. In this case, no swap.

2) Compare the next 2 values and swap if needed. In this case, swap 8 and 4 since 4 is

smaller.

2, 4, 8, 6, 3, 1

3) Compare the next pair which is 8 and 6, swap.

2, 4, 6, 8, 3, 1 4) Repeat to the end of the sequence. Now the first pass is finished.

2, 4, 6, 3, 1, 8

2. Start the second pass of the comparing and swapping by repeating step 1-4 as illustrated above. Now the sequence is:

2, 4, 3, 1, 6, 8

3. After the third pass:

2, 3, 1, 4, 6, 8

4. After the fourth pass:

2, 1, 3, 4, 6, 8

5. After the fifth pass:

1, 2, 3, 4, 6, 8 Sorted!

There are two search algorithms you need to be able to explain and apply for your exam, the linear search and the binary search. Here is how they work:

Linear search is a search algorithm based on looking at each item in a data set one after the other. It is the simplest and slowest method of search. It is suitable for small data set that are not organised in any particular order.

Binary search or binary chop is a search algorithm to look through a data set that is in some sort of logical order. Binary search works by “chopping” the data set in half, then comparing the searched item with the end and the lower half and the beginning item of the upper half to decide in which half the searched item is in. Repeating this chopping to the half with the searched item in it until it finds the item. With each chopping, the algorithm reduces the number of items to look through by half. For a large set of data that are organised in a particular way (for example, sorted), binary search is very efficient. It will only take about 33 looks to find one person in the world!

Logical operations are important functions in computing. In Visual Basic programming, you are quite familiar with the logical functions of AND, OR, and NOT. There is another logical

Page 8: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

function XOR you may have not encountered. Those are not VB specific operations. In fact they are universal. The following summarises each of those functions:

AND compares two expressions, returns TRUE if both expressions are true, otherwise returns FALSE.

For example: (pseudo-code)

A is integer B is integer C is integer D is integer If A>B AND A>C then A is larger than both B and C endif OR compares two expressions, returns TRUE if any one of the two expressions is TRUE. For example: (pseudo-code)

A is integer B is integer C is integer D is integer If A>B OR A>C then A is larger than B or C endif

NOT works by negating the expression, returns TRUE if the expression is FALSE and returns FALSE if the expression is TRUE.

For example: (pseudo-code)

A is integer B is integer C is integer D is integer If NOT A>B then

A is smaller or equal to B endif XOR compares two expressions, returns FALSE if both of the two expressions are FALSE or TRUE, and returns TRUE ONLY IF only one of the expressions is TRUE.

Expression 1 Expression 2 XOR result TRUE TRUE FALSE TRUE FALSE TRUE

Page 9: CG1.4: Algorithms and Programming Programming · Low-level programming languages have a very low level of ... program does what it’s ... There are no Pseudo-code standards but some

AS  Computing  Booklet  Serial  II   Bourne  Grammar  School   by  Mrs.  X.  Ellis  

All  Rights©  Reserved  

FALSE TRUE TRUE FALSE FALSE FALSE

For example: (pseudo-code)

a = 10 b = 8 c = 6 firstResult, secondResult, thirdResult is Boolean firstResult = a > b XOR b > c secondResult = b > a XOR b > c thirdResult = b > a XOR c > b

In the above example, the results of the XOR operations are: firstResult = FALSE, secondResult = TRUE, and thirdResult = FALSE