BEGIN [u]FindSpell()[/u]
OPEN “Spellbook” for INPUT
Get target from user input
FoundFlag = False
READ element from Spellbook
IF element == target THEN
FoundFlag = True
READ use
PRINT “Spell: “, element
PRINT “Use: “, use
ELSE
READ use
READ element
ENDIF
WHILE NOT FoundFlag AND element != “ZZZ” DO
READ use
IF element == target THEN
FoundFlag = True
PRINT “Spell: “, element
PRINT “Use: “, use
ENDIF
READ element
ENDWHILE
IF NOT FoundFlag THEN
PRINT “Spell not found”
ENDIF
CLOSE “Spellbook”
END [u]FindSpell[/u]
//Challenge
BEGIN [u]UpdateSpells()[/u]
Get name from user input
Get use from user input
OPEN “Spellbook” for APPEND
READ element
WHILE NOT EOF DO
READ element
ENDWHILE
WRITE name, use TO “Spellbook”
CLOSE “Spellbook”
END [u]UpdateSpells[/u]
<< PrevNext >>ALCON PROBLEM 4:
STRAK ENTERPRISES
After Problem 2's fantastic 5 entries, Problem 3 was a bit more disappointing with only 2 entries. Let's try to bring that up next time! ;D
Week 3 asked you to write a program that allows Hairy Bottler and Hermit Grunger to manage and update their spellbook by looping through a sequential file.
There were two entries for this problem - anomalous and cthulu did well with a fantastic 6/7 and 4/7 respectively. If you missed this problem, you can always do them in your spare time and get feedback in your code.
Sample answer:
(note [u ][/u] tags mean that the code was underlined.)Code: [Select]BEGIN [u]FindSpell()[/u]
OPEN “Spellbook” for INPUT
Get target from user input
FoundFlag = False
READ element from Spellbook
IF element == target THEN
FoundFlag = True
READ use
PRINT “Spell: “, element
PRINT “Use: “, use
ELSE
READ use
READ element
ENDIF
WHILE NOT FoundFlag AND element != “ZZZ” DO
READ use
IF element == target THEN
FoundFlag = True
PRINT “Spell: “, element
PRINT “Use: “, use
ENDIF
READ element
ENDWHILE
IF NOT FoundFlag THEN
PRINT “Spell not found”
ENDIF
CLOSE “Spellbook”
END [u]FindSpell[/u]
//Challenge
BEGIN [u]UpdateSpells()[/u]
Get name from user input
Get use from user input
OPEN “Spellbook” for APPEND
READ element
WHILE NOT EOF DO
READ element
ENDWHILE
WRITE name, use TO “Spellbook”
CLOSE “Spellbook”
END [u]UpdateSpells[/u]
Please make sure you've read the rules here.
Week 4 question:
You’re the new intern at Strak Enterprises, and Toby Strak has tasked you with keeping track of all the Nickel Man suits in the facility. Currently, there’s a relative file containing the code name and location of each suit, in no particular order.
Mr Strak has decided he wants the file moved into a multidimensional array instead, and alphabetised by code name. Pick an appropriate sorting method and write a program to carry out the requirements.Optional challenge add-on (+2 marks)Jarvis has compiled an alphabetised 2d array (100 x 2 elements) with the code name and the unique nickname Mr Strak comes up with for each suit. E.g. “MK1906” is called “Mark”. Write an additional program that takes in the nickname and looks through the array to output the code name for the suit.
Because Mr Strak talks quickly, this searching process must be done as quickly as possible.
You’ll be marked on correctness, efficiency, and defensive programming.
Learning objectives for the week:
-> Search and sort arrays.
-> Open and read relative files.
-> Manage multidimensional arrays.Marking guidelines(https://i.imgur.com/eABiWFZ.png)
Don't forget to login or register an account to submit your answer!
Good luck, and as always, happy algorithming!
Actually makes me wish I could code!Why wait? (:
Why wait? (:
BEGIN [u]StrakEnterprises[/u]
OPEN “Nickelmen” for INPUT
SET curIndex = 0
READ codeName from Nickelmen
READ location from Nickelmen
WHILE curIndex < sentinel value of Nickelmen DO
nickelmenArray(curIndex).codeName = codename
nickelmenArray(curIndex).location = location
INCREMENT curIndex by 1
END WHILE
SET pass to 1
WHILE pass < length of nickelmenArray DO
SET count to pass + 1
SET min to pass
WHILE count <= length of nickelmenArray DO
IF nickelmenArray(count) < nickelmenArray(min) THEN
min = count
END IF
INCREMENT count by 1
END WHILE
SWAP nickelmenArray(min) with nickelmenArray(count)
INCREMENT pass by 1
END [u]StrakEnterprises[/u]
BEGIN [u]StrakChallenge[/u]
GET userInput
SET newString to userInput[0] + userInput[length of userInput]
SET count to 0
SET found to false
WHILE count < length of nickelmenArray and found = false DO
SET codeNameString to nickelmenArray(count).codename
SET newCodeNameString to codeNameString[0] + codeNameString[length of codeNameString]
IF newString == newCodeNameString THEN
DISPLAY nickelmenArray(count).codename
found = true
ELSE
False
END IF
INCREMENT count by 1
END WHILE
END [u]StrakChallenge[/u]
BEGIN SystemTransfer()
Suits(100,2) is a 2d array of type string
Open SuitsFile for relative access
Move read/write head to start of file
i = 1
WHILE NOT EOF DO
GET name, location from SuitsFile
Suits[i,1] = name
Suits[i,2] = location
INCREMENT i
ENDWHILE
[u]SortArray(Suits)[/u]
END [u]SystemTransfer()[/u]
BEGIN SortArray(array)
//Using selection sort, but insertion or bubble can also be used
pass = 1
FOR pass TO array.length() - 1
Min_index = pass
Count = pass + 1
FOR test TO array.length() - 1
IF array[test] < array[min_index] THEN
Min_index = count
ENDIF
NEXT test
Swap(array, min_index, pass)
NEXT pass
END [u]SortArray[/u]()
** Where Swap() swaps the values of the elements in the array given.
BEGIN [u]Find_Nickname[/u](suits, target_name)
start = 1
end = suits.length()
foundFlag = False
WHILE end >= start AND NOT foundFlag DO
middle = INT((start + end)/2)
IF target = suits[middle,0] THEN
foundFlag = True
ELSE
IF target < suits[middle,0] THEN
end = middle - 1
ELSE
start = middle + 1
ENDIF
ENDIF
ENDWHILE
IF foundflag THEN
RETURN suits[middle,1]
ELSE
DISPLAY “Not found”
ENDIF
End [u]Find_Nickname[/u]