Task 1 Task 2 Task 3
ducks

Task 1

An ornithologist collects and analyses data about ducks. She is writing a program which will allow her to determine if a duck is stressed.

Here is the program, written in PSEUDOCODE:

#ask for and get the duck's heartbeat

OUTPUT "Please enter a valid heartbeat rate between 32 and 81"

INPUT beat

WHILE beat <32 OR beat > 81 DO

OUTPUT "Invalid entry. Try again."

INPUT beat

END WHILE

#now we have a valid number, CALL the isStressed function, passing it the data and storing the returned result

result ← isStressed(beat)

#use the result to determine if the duck is stressed

IF result = TRUE THEN

OUTPUT "The duck is stressed."

ELSE

OUTPUT "The duck is in good condition."

END IF

The isStressed function returns TRUE if the parameter is greater than 65, otherwise it returns FALSE.

Implement the isStressed function and test it with the above program. You will have to translate it from pseudocode to program code.

The function header of isStressed in pseudocode is:

FUNCTION isStressed(heartbeat:INTEGER) RETURNS BOOLEAN

dreambig

Task 2

A language analyst is writing a program that will count the occurence of the number of times a number appears in an alphanumeric sentence. For example, if the sentence is 1 th1nk U R gr3at! The count of numbers is 3.

The word or sentence must have a length greater than 0 and less than 30.

Here is a program which calls a function countNumbers

#ask for and get a STRING

OUTPUT "Please enter a word or sentence!"

INPUT info

WHILE LENGTH(info) <1 OR LENGTH(info) > 29 DO

OUTPUT "Invalid entry. Try again."

INPUT info

END WHILE

#now we have a valid sentence, send it to the countNumbers function and get a result

result ← countNumbers(info)

OUTPUT "There are ", result, "numbers in the word/sentence."

Implement a function with a header:

FUNCTION countNumbers(sentence:String) RETURNS INTEGER

It will return the count of numbers in the alphanumeric String. Note, this could possibly be 0.

Implement this function test it with the above program. You will have to translate it from pseudocode to program code.

Remember to test your solution with different data to make sure it works as expected.

stock

Task 3

Stock traders are constantly updating data files with new data.

They require a system that will update a particular value found in an integer array with a new value.

For example, if the integer array in question stores [1003,1028,2635,6353,1028,1028], the value that is being searched for is 1028 and the new value is 1031, the the array will be updated to store [1003,1031,2635,6353,1031,1031]

Here is a program which calls a function updateKeyValue

#global variable, an array of INTEGERS

dataSet ← [1003,1028,2635,6353,1028,1028]

OUTPUT "Enter a search item:"

INPUT searchItem

#validate the search item

WHILE searchItem < 0 DO

OUTPUT "Cannot be negative! Try again."

INPUT searchItem

END WHILE

OUTPUT "Enter a replacement value:"

INPUT replacement

#validate the search item

WHILE replacement < 0 DO

OUTPUT "Cannot be negative! Try again."

INPUT replacement

END WHILE

#now we have a valid search item and replacement value, let's call our function to process the array

result ← updateKeyValue(dataSet, searchItem, replacement)

IF result = TRUE THEN

OUTPUT "Changes have been made"

ELSE

OUTPUT "No changes were made"

The function will have the header:

FUNCTION updateKeyValue(data:Array[1:n] OF INTEGER, searchValue: INTEGER, newValue: INTEGER) RETURNS BOOLEAN


Implement this function test it with the above program. You will have to translate it from pseudocode to program code.

Remember to test your solution with different data to make sure it works as expected.