Category Archives: 2. Software Development
An excercise in fustration
I thought long and hard about today’s task, I set out to make a task that appeared easy to complete but without the correct design would prove to be frustrating.
The task below has been broken down so you need to follow it from start to finish and constantly revisit “finished” parts of the program, this is the iterative nature of programming.
1. Design and create a program that
a. Stores 20 class marks before displaying them and displaying the average to one decimal place.
b. Change the design and program to validate the marks (between 1 & 100) using the supplied function.
c. Change the design and program to also store the names of the students and then display the output in a two column report.
I’ll post the (1c) solution up tomorrow after class, the code for the function is after the break.
Arrays
We use Arrays to store lists of similar items, like class names or class marks. The allows us to reference all the items with the same name and therefore simplify coding by using loops.
Visual basic uses the DIM command to set up an array.
DIM StrName(5) as String ‘ creates a Array with 6 elements of type string Continue reading Arrays
Dice Menu Program
Write a program using the skills you have learned so far to ask the user to select between a 6,12 or 20 sided dice and then to display 5 rolls of that dice. The program should only stop asking when the user enters zero.
Solution after the break
DIM
DIM is used to dimensionalise a variable, that is set memory aside for the variable to be stored. It is used thus
- DIM Number1 as Integer
- DIM Number2 as Integer
However this can be shortened to
- DIM Number1, Number2 as Integer
In this case VB(Express Edition) sets number1 & number2 to integers. A more complex example can be seen below
- DIM StrName as String
- DIM Number1, Number2 as Integer
Becomes
- DIM StrName as String, Number1, Number2 as Integer
You need to be careful that you get your syntax correct. It might be easier at the start to stick with the traditional(top) method.
Select Case
The Select..Case statement is a special instance of the IF structure. To implement multi-way decisions the statement provides a more concise and elegant representation than multiple and statements, which can get very difficult to follow. Probably if you have more than three conditional statements you should consider using the Select..Case statement instead.
Here is an example algorithm
- Ask user to input a number between 1 and 7
- select case number
- Case is = 1 Display “Sunday”
- Case is = 2 Display “Monday”
- Case is = 3 Display “Tuesday”
- Case is = 4 Display “Wednesday”
- Case is = 5 Display “Thursday”
- Case is = 6 Display “Friday”
- Case is = 7 Display “Saturday”
- Case else Display “Error”
- End Select
Homework for this Friday
Write the pseudocode to solve the following problems
- Calculate the area of a square
- Calculate the perimeter of a rectangle
- Calculate the volume of a square bottomed pyramid.
Strings and branches (so far)
Right for the last couple of weeks we have been using MS Visual Basic Express 2008. Here is the state of play so far.
String Handling
Description and exemplification of the following constructs in pseudocode and an appropriate high level language: string operations (concatenation and substrings)
String and string handling are the bread and butter of all programs. Today we looked at how the programmer can cut (substring) and paste (concatenation) strings together.
- Len – Output is the length of the string
- Ucase – Output is the string in upper case
- Lcase – Output is the string in lower case
- Asc – Output is the ASCII value of the character
- Chr – Output is the ASCII character of the value
- Mid$ – outputs the characters from anypoint in the string.
- Mid$(String,First,HowMany)
- Strings.Left – Outputs the leftmost characters in a string.
- Strings.Left(String,HowMany)
- Strings.Right – Outputs the rightmost characters in a string.
- Strings.Right(String,HowMany)
Concatenation is a scary-looking word, but it simply means “joining together”. String concatenation means joining two (or more) strings together to form one new string: String concatenation in VB is very simple – you don’t need a special function – you just use the + sign.
For example,
- if string1 = “Fred”,
- and string2 = “McSporran”,
- then string1 + string2 = “FredMcSporran”.
Today’s “Impossible Task”
Create a program that displays the ASCii codes of all the characters of a string that is entered by the user, solution after the jump.
Simple Conditional Statements (IF)
A simple conditional statement is checks one condition. We use IF in VB, it is written as
- IF <Condition> then
- Do something
- ELSE
- Do something else
- END IF
The Condition has to resolve to true or false and uses comparison operators (Comparitors). Try these tasks
- Says hello to people named Bob
- Tells you if a number is above 10
- Tells you which of two numbers is the biggest
- Tells you if a number is odd or even