: 2000/000752/07
MAIN CAMPUS
292 SMITH STREET
DURBAN
4000
nd
2 YEARS
ASSIGNMENT BOOKLET
Page 1 of 21
Registered with the Department of Education as a Private Higher Education Institution under the Higher
Education Act, 1997. Registration Certification No. 2000/HE07/008
GUIDELINES
3. The length of the entire assignment must have minimum of 5 pages. Preferably typed with font
size 12
3.1 The quality of work submitted is more important than the number of assigned pages.
4. Copying is a serious offence which attracts a severe penalty and must be avoided at all costs. If
any learner transgresses this rule, the lecturer will retain the assignments and ask the affected
learners to resubmit a new assignment which will be capped at 50%.
5. Use the Harvard referencing method.
Page 2 of 21
ASSIGNMENTS DUE DATE
Moderator’s Comments:
Page 15 of 21
QUESTION ONE (25 MARKS)
1.1 Compare and contrast the if/elseif control structure with the switch control
structured and provide coded examples to sustain your answer. (10)
1.2 Use loops and control structures create a program that grades the following list of
students given the grade table below:
For example:
Sauer Jeppe scored a Distinction. (15)
2.1 Using multidimensional arrays; create a program that stores 3 arrays, with each
array storing 3 elements.
For example:
3.1 Write a function countWords($str) that takes any string of characters and finds the
number of times each word occurs. You should ignore the distinction between
capital and lowercase letters, and do not have to worry about dealing with
characters that are not letters. Hint: Create an associative array mapping word
keys to the number of times they occur. You will need to look at PHP's string
functions to split a sentence into words.
Page 16 of 21
Hint 2: The print_r($array_name) function is useful for examining the contents of an array.
3.2 In the same file, write a form consisting of a single text field and a submit button. The
“action” attribute to the form should be the same page that the form is on (don’t hard
code, use $_SERVER*‘PHP_SELF’+). The form should send the contents of the text field
via GET.
Upon submitting the form, you should be redirected to the same page, but the URL
should contain the string from the text field as a GET request normally behaves.
3.3 Check for the existence of a GET request. If a GET request exists with the name of the
input field that you made in Part 2, run the contents of the request through your
countWords($str) function. Take the output array of the function, and display an HTML
table of words and the number of times they occur. Make the table sorted by
decreasing number of occurrences.
4.1Write a PHP script named states.php that creates a variable $states with the value”Mis-
sissippi Alabama Texas Massachusetts Kansas”. The script should perform the following
tasks:
Search for a word in $states that end in xas. Store this word in element 0 of an array
named $statesArray.
Search for a word in $states that begin with k and ends in s. Perform case-insensitive
comparison. Store this word in element 1 of $statesArray.
Search for a word in $states that begin with M and ends in s. Store this element in
element 2 of the array.
Search for a word in $states that ends in a. Store this word in element 3 of the array.
Search for a word in $states at the beginning of the string that starts with M. Store this
word in element 4 of the array.
Output the array $statesArray to the screen.
[20 marks]
4.2Write a PHP script that tests whether an e-mail address is input correctly. Verify that the
input begins with the series of characters, followed by the @ character, another series of
characters, a period(.) and a final series of characters. Test your program, using both
valid and invalid e-mail addresses.
[10 marks]
4.3Write a PHP script to calculate and display average marks, five lowest and highest marks.
Recorded marks : 78, 60, 62, 68, 71, 68, 73, 85, 66, 64, 76, 63, 75, 76, 73, 68, 62, 73, 72,
65, 74, 62, 62, 65, 64, 68, 73, 75, 79, 77, 89, 90, 52, 65, 75, 80.
Example :
[10 marks]