Archive for November, 2007

Standard Algorithm 2 – Linear Search

November 30, 2007

This algorithm asks the user for something to look for and then checks through a list of items to see if it can find it. If it does find it, the algorithm says in what position it found it. So for example from the following list:

“Stewart”, “Simon”, “Dara”, “Scott”, “Keith”, “Farooq”, “Gaetano”

User Input Algorithm Output

Stewart 1

Dara 3

Gaetano 7

In the same way as the last standard algorithm you need to be able to write the pseudocode in the exam!

1.1 ask for item being searched for

 

1.2 for each item in the array

 

1.3 if this item matches the item being searched for

1.4 store the position

 

1.5 end if

 

1.6 end loop

 

1.7 print out where the item was found

 

Powerpoint below:

linear-search.jpg

Standard Algorithm 1 – Counting Occurrences

November 29, 2007

This is the first standard algorithm (in no particular order) that you have to know, and be able to write a pseudo code version of in the exam.

Put simply this algorithm counts the number of times an item appears in a list. The item could be a name or a number, the list is normally an array.

So for example in the following list:

“A” “B” “B” “C” “B” “A” “A” “B” “C” “B”

The algorithm would ask the user what they are looking for and then should return the results below:

Being looked for                             Number of occurrences

A                                                                          3

B                                                                          5

C                                                                          2

So you need to be able to write the pseudo code that will do this. My answer is below, see the powerpoints at the bottom for more detail:

1.1 ask for item being searched for

1.2 for each item in the array

1.3 if this item matches the item being searched for

1.4 add one to the count

1.5 end if

1.6 end loop

1.7 print out the number of times the item was found

 

counting-occurrences.jpg


Confusion!

November 26, 2007

I think I have confused at least half the class with my explanation of parameter passing! Or at least maybe not my explanation but certainly my attempted explanation of the NAB type question.

Let me try and simplify it again:

Call by value means a copy of a variable is used in a sub-routine. If it changes the variable, only the copy is changed, not the original.

Call by reference uses the original variable (by referring to the address of the variable rather than a copy). If it changes, then the original is changed also.

If you understand that then you are probably fine! The NAB question was confusing as it lacked some important details.

The other thing we did to day was functions. The important thing about them is that they produce a value. For example random produces a random number.

functinos.jpg

Here is the powerpoint on parameter passing:

parameter-passing.jpg

Brazilians!

November 23, 2007

This was Mr Polland’s excellent program that I converted in to VB (and am still a bit too excited about).

It has an array of your names, an array of brazilian endings, and it concatenates a random ending onto a substring of your name.

The point of the lesson is to do with Functions. The random function is called with two parameters (low and high) andit then generates a random number between those two. In the program I use that random number to pick a brazilian ending and put it onto your name.

Here is a screenshot:

brazilian-program.jpg

Local and Global Variables

November 22, 2007

A local variable is declared (set up) and only available for use in the sub-routine it is made in. A global variable is declared at the start of your program (in VB anyway) and can be used anywhere.

It is easier to understand when you look at some code:
private sub get_marks

dim marks(10) as integer

for counter 1 to 10

marks(counter) = inputbox(“Type in your mark”)

next

end sub

In the code above the array (marks) is only available inside the sub-routine called get_marks. If you try to access it somewhere else, it won’t work i.e. the scope of the variable (array) is local

If you set up the variable (or array) at the start of the program before any sub-routines (like we normally do) then it will be available for use everywhere i.e. the scope of the variable is global

Notes below:

local-global-variables.jpg

String operations – Sub strings and Concatenation

November 20, 2007

We looked at what a string variable was last week e.g. “James”, “Eh12”. You need to know about two things that you can do to this type of data:

Substring – take a part of a string e.g. take “ame” from “James”

Concatenation – join two strings together e.g. join “Craig” and “mount” to make “Craigmount”

That is pretty much it, except you need to know how to make Visual Basic do this. Here are some examples:

Concatenation is done using the & symbol.

For example:

first = “j”

second = “whiteside”

username = first & second

The code above will make username equal to “jwhiteside“.

Substrings are done by using left, mid (or right).

For example:

day = “wednesday”

firstbit = left(day, 6)

middlebit = mid(day, 4, 3)

lastbit = right(day, 3)

This would make firstbit equal to “Wednes”, secondbit equal to “nes” and lastbit equal to “day”.

Click on the notes below to see the other examples and the questions again.

string-ops.jpgstring-op-questions.jpg

Sub routines

November 19, 2007

Once I eventually got my program to work in the way it was supposed to 😉 we looked at what a sub-routine is (also called a procedure). There were two sub routines in this program that took in pupil marks and then printed out an average, they are called:

  • fill_array
  • calculate_average

It is good to make use of sub-routines as they increase the maintainability of your program (i.e. other programmers can update your program easily).

These can be called (made to run) from any part of the program.  Click the notes below to see the program:

subroutines.jpg

Because this program makes use of subroutines so it is modular!

For Loops

November 18, 2007

We did some quick questions (here they are without answers constructs-quick-qus-wthout-answers.jpg in case you want to try them again, with answers at the bottom of the post so you can check them), then we had a look at how to make code repeat itself a set number of times. One way to do this is with a for loop.

In Visual Basic you need a counter (to keep track of how often the loop has gone round). This can then be used to do clever things, like change a particular element of an array. For example in the code below on the tenth go around of the loop, what the user types in will go into place 10 of the array (if you are starting from 1):

 

 

For counter := 1 to 15

 

name(counter) = InputBox(“Please type in another name”)

 

Next

If you still don’t get it, re-read the powerpoint below:

for-loop.jpg

 

And these are the answers to the quick questions:

constructs-quick-qus-with-answers.jpg

Language Constructs – Variables, Data Types and Arrays

November 16, 2007

Today was an explanation in formal terms of things that, mostly, you already knew. You already knew them because you have been writing programs in visual basc that use variables, data types and (in the future) arrays.

I ran an example program called agein2020 which had one variable called age of type integer (the picture below shows what the code turns into when run).

data-type.jpg

We then looked at the different types you could set up:

Integer – positive and negative numbers (e.g. -56, 100, 45)

Single – numbers with a point (e.g. 3.14, 1.4)

String – any combination of words and letters (e.g. “EH10”, “Jamie”)

An array is a set of variables of the same type e.g. dim names(5) as string will create an array with 5 spaces in it.

arrays.jpgvariables.jpg

Software Development Process Powerpoints

November 14, 2007

There were some excellent Software development Powerpoints. I have taken some screenshots of the best ones:

adam-sw-dev-screenshot.jpgabby-implment-sw-dev-screenshot.jpgscott-sw-dev-ppt-shot.jpgkelsey-sw-dev-ppt.jpgkaty-sw-dev-ppt-shot.jpgkirsty-sw-dev-ppt.jpg

If yours is featured I owe you a Merit refferal (see me if you want to collect it).