You are on page 1of 5

Answer the following questions and submit your work

(i)
(ii)

in the form of a MS Word document; and


by presenting your work in class during a demonstration.

Your programs have to be Visual Basic, written in modular form (procedures and functions). Comment
your codes generously. During demonstrations of your code you will be requested to give explanations
on them.
You do not need to write code for Question 1. Only give your answers in MS Word. Write a separate
program each for Questions 2 and 3.
Save the programs for demonstration, and at the same time have the codes copied and pasted in MS
Word so as to present your answer in the form of a report, which you will submit (through email or
otherwise). This document will also contain tables, diagrams and other elements, as requested in the
questions.
Marks will be given for the different programming conventions we have learned such as proper and
meaningful variable naming, indentation, comments, neatness etc.

Page 1 of 5

Question 1
Consider the function below.

Function Detobi(ByVal de As Integer) As String


Dim bit As Integer
Dim bi As String = ""
While de > 0
bit = de Mod 2 'Mod gives remainder
de = de \ 2 'Div (reject remainder)
bi = bit & bi
End While
Return bi
End Function
(a)

Draw a trace table and show the results when the following call is made:

Console.WriteLine(detobi(100))
[4]
(b)

It is not possible to write this function in Visual Basic using a FOR loop. Explain why.
[2]

(c)

Write the function in pseudocode using a REPEAT ... UNTIL loop.


[4]

Page 2 of 5

Question 2
This program will convert a binary number into denary.
(a)

Write a function called BinToDen that takes as parameter a string of binary digits (0 and 1) and
returns the equivalent denary value.
Example: BinToDen(10010011) returns 147
The algorithm (in English) is as follows:
Traverse the string from right to left and for each digit do
Multiply the integer value of the digit (0 or 1) by the weight of the bit
Add the result of this multiplication to the total denary value
Note: The weight of the bit is 2 raised to the power of the bits position (starting from 0 and
incrementing by one each time we move to the left). In the example above, we would
have something like (1*20) + (1*21) + (0*22) + ..
You will need to write another function called power that takes two integers and returns the value
of the first integer raised to the power of the second. Example: power(2,3) returns 8. You will
then use it in the above function.
Test your function with the following data and show your results:
(a)
(b)
(c)
(d)

101
100101
000000111
100000000
[10]

(b) Write another version of this function, this one called BinToDenNeg in order to make conversions in
2s complement. This will represent negative numbers.
Example: BinToDenNeg(10010011) returns -109
Test it with the same data from (a) above and show the results.
[5]

Page 3 of 5

Question 3
Our friend Alfred wants to encrypt some secret words and store them in a file. He devises a very simple
method that consists of replacing each letter in the word with the one following it. For example, a will
be replaced with b, o with p, and z with a (to make the list circular). Assume that Alfreds words
contain only letters of the alphabet.
(a)

You are to write a function called Scrumble to encrypt his words. The function takes the word as
a string and returns the encrypted version.
In the function:
-

Declare a string called cipher that will store the encrypted text.
Traverse the word from left to right and for each letter do
o Convert into ASCII value (Use the Asc function)
o Add 1 to that value and convert it back to the character (Use the Chr function)
Make an exception for z, which you will replace with a
o Append this new letter to cipher.

Test your function with the following data and show the results:
(i)
(ii)

alfred
zul
[10]

(b)

Write a function that does the reverse of Scrumble decrypt a string and return it in the original
form it was before. Call it Unscrumble. Test it with the data above.
[5]

(c)

Now Alfred wants to store his secret words in encrypted form in a file. Create a file with the
name MySecretWords.txt for that purpose in your folder.
Write a procedure calls AppendSecretWord that takes a string as parameter (the encrypted
secret word) and enters it as a new line in the file. Note that when you create the file,
StreamWriter takes another parameter along with the file path, called Append, which should be
set to true so that all other records are not overwritten.
[5]

(d)

Write a procedure that asks the user for a new word, encrypts it and enters it into the file. This
procedure is called EnterNewWord and principally calls the other functions and procedures you
have written previously to do the task.
[3]

Page 4 of 5

(e)

Write a function called IsAlreadyThere that takes a secret word as string and searches for it in
the file. It returns true if found. Note that each time you read a line when searching you need to
decrypt it before comparing. Test this function with some data based on your files contents.
[5]

(f)

Modify procedure EnterNew from (d) above such that whenever Alfred wants to enter a new
secret word, it checks whether it is already present first. If it is, it gives an error message and
does not enter it. Else it enters the new secret into the file normally. Use the above functions.
[2]

(g)

Write a procedure called ShowSecrets that displays all the files contents, in decrypted form.
[5]

(h)

Write a procedure called Menu, which display a menu of three items. It will resemble this:
1. Enter new word
2. Show all words
3. Exit
Enter your choice:

When it reads the users choice (which can only be 1, 2, or 3), it calls the appropriate function /
procedure to do the requested task.
The main procedure (Sub Main) should contain only a call to Menu, which in turns calls all the
other functions and procedures.
[5]
(i)

Draw a structure chart to represent this whole program.


[10]

Total Marks: 75

Page 5 of 5

You might also like