Jump to content

Fundamentals of Programming: Built-in functions

From Wikibooks, open books for an open world

PAPER 1 - ⇑ Fundamentals of programming ⇑

← Exception handling Built-in functions Random number generation →


You need to be familiar with several programming routines that come built into most common programming languages. These routines are very useful and should save you a lot of effort in writing code to perform common tasks. You might be asked to use them in the exam so learn them!

Arithmetic functions

[edit | edit source]

You'll have to be familiar with several

Round

[edit | edit source]

The round function is used to round numbers to a limited number of decimal places using the Math.Round() function

Math.Round(1.94, 1) 'Returns 1.9
Math.Round(1.95, 1) 'Returns 1.9 !0.5 rounds down!
Math.Round(1.96, 1) 'Returns 2.0
Math.Round(1.9445, 2) 'Returns 1.94
Math.Round(1.9545, 3) 'Returns 1.954
Math.Round(6.765, 2) 'Returns 6.76
Math.Round(1.9445) 'Returns 2 - the equivalent of saying round to 0 dp

Truncation

[edit | edit source]

The truncate function returns the integer part of a number, regardless of the decimal places.

Math.Truncate(19.45) 'Returns 19
Math.Truncate(19.9999) 'Returns 19

This is particularly useful when you are trying to perform DIV in modular arithmetic.

Extension: Random numbers

An essential part of most games is the ability to use random numbers. These might be used to randomly place gold coins on a map, or to calculate whether you hit a target with a rifle at some distance.

Dim rndGen As New Random()
Dim randomNumber As Integer
randomNumber = rndGen.Next()

The above code will give you a random number between 1 and 2,147,483,647. You might well require a number that is a little smaller. To get a random number between two set numbers, in this case 5 and 10 you can use the following:

randomNumber = rndGen.Next(5,10)

So how exactly can we use this? Take a look at the following game:

Dim rndGen As New Random()
Dim randomNumber As Integer
Dim guess as Integer
randomNumber = rndGen.Next(1,100)
console.writeline("Please guess the random number between 1 and 100")
Do 
  console.write("your guess:")
  guess = console.readline()
  if guess > randomNumber
    console.writeline("Too High")
  end if
  if guess < randomNumber
    console.writeline("Too Low")
  end if
Loop While guess <> randomNumber
console.writeline("Well done, you took x guesses to find it!")

Adjust the code above to tell the user how many guesses they took to find the random number. HINT: you'll need a variable

Answer:

    Sub Main()
        Dim rndGen As New Random()
        Dim randomNumber As Integer
        Dim guess As Integer
        Dim count As Integer = 1
        randomNumber = rndGen.Next(1, 100)


        Console.WriteLine("Please guess the random number between 1 and 100")
        Do
            Console.Write("your guess:")
            guess = Console.ReadLine()
            If guess > randomNumber Then
                Console.WriteLine("Too High")
            End If
            If guess < randomNumber Then
                Console.WriteLine("Too Low")
            End If
            If guess <> randomNumber Then
                count = count + 1
            End If
            If guess = randomNumber Then
                Console.WriteLine("Well done, you took " & count & " guesses to find it!")
            End If

        Loop
    End Sub
Exercise: Arithmetic function

What does the following code output:

dim num1 as single = 12.75
dim num2 as single = 12.499
dim total as single

num2 = Math.Round(num2, 1)
num1 = Math.Truncate(num1)
total = num1 + num2
console.writeline(Math.Round(total))

Answer:

   Code Output

24


Write some code to output the integer part of a number input by the user

Answer:

Math.Truncate(input)


Write code to output the integer and decimal parts of an input number:

   Code Output

Please insert a decimal number: 13.78
The whole number part of this number is: 13
The decimal part is: 0.78

Answer:

dim num as single
console.write("Please insert a decimal number: ")
num = console.readline()
console.writeline("The whole number part of this number is: " & Math.Truncate(num))
console.writeline("The decimal part is: " & num - Math.Truncate(num))

String handling functions

[edit | edit source]

Very popular examination questions involve manipulating strings. These simple functions will help you with this task.

Length

[edit | edit source]

This function is used to find the length of any string you pass it, counting all the characters, including the spaces. In visual basic to find the length of a string we use the Len("some string") function that returns the integer length of the string that it has been passed:

someText = "Gary had a little lamb"
Console.writeline(Len(someText))
   Code Output

22


Position

[edit | edit source]

This function allows us to find the position of an item within a given string and returns the position's location. In visual basic this is performed by the following command: InStr([string], [item]) For example we might want to find the location of an end of a sentence by looking for a fullstop:

someText = "Gary had a little lamb. His fleece was white as snow."
Console.writeline(InStr(someText,"."))
   Code Output

23

We can also use this command to search for strings within strings. For example if we were to look for to see if a sentence contained a certain name:

someText = "Gary had a little lamb. Dave's fleece was white as snow."
Console.writeline(InStr(someText,"Dave"))
   Code Output

25

If the search item is not contained in the string then it will return 0

someText = "Gary had a little lamb. Dave's fleece was white as snow."
Console.writeline(InStr(someText,"Julie"))
   Code Output

0


Substring

[edit | edit source]

This function allows you to snip items out of a string and return a substring. Visual Basic uses the following command: [string].Substring([startPosition],[lengthOfReturnString]). For example we might want to find the local number from a landline phone number we have been given. We'll have to ignore the area code:

phone = "(01234)567890"
local = phone.Substring(7, 6)
console.writeline(local)
   Code Output

567890


Concatenation

[edit | edit source]

This function allows you to stick strings together (concatenate) so that you can start to build strings using variables. Visual Basic uses the following command: [stringA & stringB] For example we might have a users name stored in a variable dim name as string and a greeting that we would like to give them:

name = "Charles"
console.writeline("Hello " & name & ". How are you today?")
   Code Output

Hello Charles. How are you today?


String conversion functions

[edit | edit source]

When you declare a variable you give it a datatype. This datatype restricts the values that you can place into the variable. For example:

dim age as integer
would allow: age = 34
would NOT allow: age = "cabbages"

This seems to make sense, but what would happen when you try to place a real number into a integer:

dim age as integer
age = 34.3
console.writeline(age)
   Code Output

34

This might seem OK, but in other languages we might run into trouble. To perform this we would have to convert from one datatype to another:

dim age as decimal
age = 34.3
console.writeline(age)
age = CInt(34.3) 'converts the decimal into an integer
console.writeline(age)
   Code Output

34.3 34


Exercise: String functions

Write a short program to tell someone how many letters they have in their name (just in case they don't know!), for example:

   Code Output

Input: Fremlin
Hello Fremlin, you have 7 letters in your name.

Answer:

  Dim name As String
  console.write("Input: ")
  name = console.readline()
  console.writeline("Hello " & name & " you have " & Len(name) & " letters in your name.")


Some people have stupidly typed their firstname and their surname into a database, write some code to display the first name, then their surname

Dim name as string = "Elizabeth Sheerin"
   Code Output

Input: Elizabeth Sheerin
Firstname: Elizabeth
Surname: Sheerin

Answer:

        Dim name As String = "Elizabeth Sheerin"
        Dim firstname, secondname As String
        Dim space, textlength As Integer

        space = InStr(name, " ")
        textlength = Len(name)

        firstname = name.Substring(0, space)
        secondname = name.Substring(space, textlength - space)

        Console.WriteLine("first name is: " & firstname)

        Console.WriteLine("second name is: " & secondname)


A telephone number has been typed into a computer as a string: (01234)567890

dim phonenum as string = "(01234)567890"

Write some code to output the number without brackets:

   Code Output

Input: (01234)567890
Output: 01234567890

Answer:

        Dim phonenum As String = "(01234)567890"
        Dim firstbracket, secondbracket As String
        Dim textlength, arealength As Integer

        firstbracket = InStr(phonenum, "(")
        secondbracket = InStr(phonenum, ")")
        textlength = Len(phonenum)
        arealength = secondbracket - firstbracket

        Console.Write(phonenum.Substring(firstbracket, arealength - 1) & phonenum.Substring(secondbracket, textlength - secondbracket))


A similar question to the one above, telephone numbers are currently stored in a very unreadable format: 01234567890, completely missing off the area code. Can you convert them to display the first 5 figures are the area code:

dim phonenum as string = "01234567890"

This should then be output as:

   Code Output

Input: 01234567890
Output: (01234)567890

Answer:

        Dim phonenum As String = "01234567890"
        Console.Write("(" & phonenum.Substring(0, 5) & ")" & phonenum.Substring(6, 5))
        Console.ReadLine()


A palindrome is a word, phrase or number that may be read the same way in either direction. For example 1234321, RACECAR, TOOT and NUN. You need to write a program that checks to see if any input given is a palindrome and let the user know:

   Code Output

Input: NUN
That is a palindrome!
Input: nune
That is NOT a palindrome

Answer:

  Dim name As String
  Dim length As Integer
  Dim Pal As Boolean = TRUE
  console.write("Input: ")
  name = console.readline()
  length = Len(name)
  For x = 0 to (length / 2)
    If name.Substring(x, 1) != name.Substring(length - x, 1) then
       Pal = FALSE
    End If
  Next
  
  If Pal then
    console.writeline("That is a palindrome!")
  Else
    console.writeline("That is NOT a palindrome!")
  End If


Extension: REGEX

You will often want to check the format of a string being input and if it is incorrect you will want it to be submitted again. For example you might want someone to input the name of their best friend, meaning that they shouldn't be inputting any numbers or spaces, and it should start with a capital letter:

   Code Output

Name of best friend: Beanie(CORRECT)
Name of best friend: jonny5(STOP THIS)

To do this we can match the input string against some rules, regular expressions or regex, in this case we only want characters from the alphabet:

[A-Z][a-z]+

Breaking apart the rule:

  • [A-Z] - start exactly one instance of a capital letter
  • [a-z]+ - followed by as many lower case letters as you like (that's what the + means)

Another example might be checking for the correct spelling of a famous composer:

"Handel", "Händel", and "Haendel"

We can check this using the pattern H(ä|ae?)ndel. Let's take a look at what this means:

  • H - start with an H
  • (ä|ae?) - includes an ä or (the | symbol) an a followed by an optional e (e? means the e is optional)

Most regular expression tools provide the following operations to construct expressions.

Boolean "or"

A vertical bar separates alternatives. For example, gray|grey can match "gray" or "grey".

Grouping

Parentheses are used to define the scope and precedence of the operators (among other uses). For example, gray|grey and gr(a|e)y are equivalent patterns which both describe the set of "gray" and "grey".

Quantification

A quantifier after a token (such as a character) or group specifies how often that preceding element is allowed to occur.

  • ? The question mark indicates there is zero or one of the preceding element. For example, colou?r matches both "color" and "colour".
  • * The asterisk indicates there is zero or more of the preceding element. For example, ab*c matches "ac", "abc", "abbc", "abbbc", and so on.
  • + The plus sign indicates there is one or more of the preceding element. For example, ab+c matches "abc", "abbc", "abbbc", and so on, but not "ac".

Most programming languages have regular expression functions. In VB.NET we can use regular expressions by using the Regex routine:

' this code enforces the name rule from earlier
Dim name As String
Console.Write("Name of best friend: ")
name = Console.Readline()

' match the string against a regular expression
Dim m As Match = Regex.Match(name, "[A-Z][a-z]+")

If (m.Success) Then
    Console.WriteLine("You have input the name correctly")
Else
    Console.WriteLine("Incorrect format!")
End If

A common use for regular expressions is in checking that you have a correctly typed email address. A rule for that is this: ^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$.

You can find out more about Regular expression on wikipedia and you will cover regular expressions in more detail in A2.

to/from integer, real, date/time.