UNIVERSITY OF PRETORIA FACULTY OF ENGINEERING, BUILT ENVIRONMENT AND INFORMATION TECHNOLOGY Student Number: Name: Surname: Email: Contact Number: Signature: Subject: MPR212: Programming and Date Processing STUDENT INFORMATION TEST INFORMATION Test: Date: 22 June 2011 Pages: 16 Internal: Mr. Vogel and Dr. Wilke Questions: 6 Examination External: Prof. Holly Moore Time: 180min SPECIAL INSTRUCTIONS You can use any open space on the paper for roughwork. Question 1: Multichoice questions should be answered on Side 1 Question 2: If your program won t run save everything and restart Octave. Question 3: The reexam is at 13:30 1 July 2011 Question 4: Question 5: Question 6: RESULTS EXAMINATION RULES AND REGULATIONS No candidate may enter the examination hall later than half an hour after the examination session has commenced and no candidate may lease the examination hall earlier than half an hour after the examination session has commenced. Condidates are obliged to execute immediately all instructions given by the invigilator. Candidates may not take into the examination hall or have in their possession any unauthorised apparatus, books, notes or paper of whatever nature or size. Once the invigilator has announced that the examination has commenced, all conversation or any other form of communication between candidates must cease. During the course of the examination there may be no communication of any nature between No candidates may assist or attempt to assist another candidate, or obtain assistance or attempt to obtain assistance from another candidate with regard to any information. Candidates shall not act dishonestly in any respect. Writing on any paper other that that supplied for examination purposes is strictly prohibited. Candidates may also not write on the blitting paper or the examination paper except if, in the case of the latter, they are required to fill in answers or to respond to multiple-choice questions. Rough work should be done in the examination answer book and then crossed out. Pages may not be removed from the answer book. Smoking is not permitted in the examination hall and candidates may not leave the examination hall during the examination in order to smoke outside. Only in exceptional circumstances will a candidate be given permission to leave the examination hall temporarily, and then only under the supervision of an invigilator.
Question 1 1.1 (OpenOffice) What do I have to press along with <Ctrl> to switch between formula and value view in OpenOffice.org? Wat moet ek saam met <Ctrl> druk om te verander van formule na waarde vertoning in OpenOffice.org?! V <F3> <Shift> ~ [2] 1.2 (OpenOffice) Which of the following graph types CAN T be made using OpenOffice.org? Watter van die volgende tipes grafieke kan NIE met OpenOffice.org gemaak nie? Pie Chart Bubble Chart Scatter Chart Polar Chart Stock Chart [2] 1.3 (OpenOffice) What does the DAYS function do? Wat doen die DAYS funksie? Calculates the number of days in the current year. Calculates the number of days since the beginning of the year. Calculates the number of days in the current month. Calcualtes the number of days until a certain date. Calculates the number of days between two dates. [2] 1.4 (OpenOffice) Which of the following is NOT a valid use of the detective function? Watter een van die volgende is NIE a geldige gebruik van die detective funksie in OpenOffice.org NIE Trace precedents of cells. Mark cells with invalid data. Trace dependents of cells. Trace errors in cells. Marked named cells. [2] Page 2 of 16
1.5 (OpenOffice) What is the order of the inputs for the FV() function? Wat is die order van die insette vir die FV() funksie? FV(NPER, PMT, PV, Rate, Type) FV(Rate, PMT, NPER, PV, Type) FV(NPER, Rate, PV, PMT, Type) FV(PV, NPER, PMT, Rate, Type) FV(Rate, NPER, PMT, PV, Type) [2] 1.6 (OpenOffice) When setting up a what-if scenario, one must type in a comment in the scenario dialog. apply conditional formatting for the cells used. have selected atleast two cells. use a named ranges in the scenario. type in the values for all scenarios before hand. [2] 1.7 (OpenOffice) Use the goal seek tool in OpenOffice.org to solve the following equation: Gebruik die goal seek tool in OpenOffice.org om die volgende vergelyking: tan (x) x = 1 the fourth digit after the decimal point is then die vierdie syfer na die desimale punt is dan 5 3 1 7 9 [3] 1.8 (OpenOffice) Consider the following experimental data, Beskou die volgende experimentele data, Page 3 of 16
X Y 0.018 1.672 1.251 4.433 2.510 9.276 3.706 10.541 4.962 14.132 If we create a graph of the data in OpenOffice.org and fit a linear regression line through the data the gradient of said regression line will be: As ons n grafiek van die data in OpenOffice.org maak en n lineêre regressie lyn deur die data pas sal die helling van die regressie lyn wees: 2.7137 3.9341 2.2108 3.2136 1.8921 [4] 1.9 Consider the following function, Beskou die volgende vergelyking, f(x) = (A + B) 2 Where, Waar, A = sin(x) + cos(x) 1 x 2 B = e (x2 3x+A) ln(x 2 + 1); Use OpenOffice.org s goal seek tool to determine for what value of x the function will be equal to 2. Gebruik OpenOffice.org se goal seek funksie om te bepaal vir watter waarde van x die funksie gelyk is aan 2. x = 2.840?82... 0 4 8 2 6 [4] 1.10 (OpenOffice.org) A lumber mill saws both finish-grade and constructiongrade boards from the logs that it receives and sells them in bundles. Suppose that it takes 2 hours to rough-saw a bundle of the finsh-grade boards and 5 hours to plane a bundle of these boards. Suppose also that it takes 2 hours to roughsaw a bundle of the construction-grade boards, but it takes only 3 hours to plane a bundle of these boards. n Saag meulary saag beide afrondings en konstruksie graad planke van hout wat dit ontvang en verkoop dit in bondles. Veronderstel dit neem 2 ure om n bondel afrondings-graad planke rof te saag en 5 ure om n bondle van hierdie te planke deur die dikte skaaf te sit. Veronderstel ook dat dit 2 ure neem om n bondel konstruksie planke rof te saag en 3 ure neem om n bondle van hierdie planke deur n dikte skaaf te sit. Page 4 of 16
The saw is available 8 hours per day, and the plane is available 15 hours per day. If the profit on a bundle of the finsh-grade boards is R120 and the profit on a bundle of the construction-grade boards is R100, how many bundles of each board type of lumber should be sawed to maximize the proft? How many bundles of construction grade boards should be manufactured? Die saag is vir 8 ure per dag beskikbaar en die dikte skaaf is vir 15 ure per dag beskikbaar. As die wins op n bondel van die afrondingsgraad plank R120 is en die wins op n bondel konstruksiegraad planke R100 is, hoeveel bondels van van elke soort plank moet vervaardig word om die wins te maximeer? Hoeveel bondels van konstruksie graad planke moet vervaardig word? 5 3 7 9 1 [5] 1.11 (Octave) What is value of x 2 in the solution to the following system of nonlinear equations. Wat is die waarde van x 2 in die oplossing van die voglende stelsel nie-lineêre vergelykings x 2 1 + 2x 2 2 x 2 2x 3 = 0 x 2 1 8x 2 2 + 10x 3 = 0 x 2 1 7x 2 x 3 1 = 0 0.2 0.0 0.6 0.4 0.8 [4] 1.12 (Octave) If the following differential equation is solved using lsode, As die volgende differentiaal vergelyking opgelos word met lsode, ẋ = x3 + x 3x 2 t + t with x t=0 = 0 and 1 t 2. As x t=0 = 0 en 1 t 2. What will the final value of x be at t = 2? Wat sal die finale waarde vir x wees by t = 2? 0.64866 1.3788 0.50000 0.68233 0.57885 [4] Page 5 of 16
1.13 (Octave) If we fit a third order polynomial through the following experimental data, As ons n derde graadse polinoom deur die volgende experimentele data pas, X Y 0.00000 0.95300 0.20000 0.40035 0.40000 0.02769 0.60000 0.34741 0.80000 0.71190 1.00000 1.08306 the third digit after the decimal point on the quadratic term will then be die derde syfer na die desimale punt op die kwadratiese term sal dan wees 6 2 8 0 4 [4] [40] Page 6 of 16
Question 2 (10) Write a function that takes two inputs namely, a vector a and an integer x. The function must then count how many times the integer x occurs in vector a. The number of occurences must be returned as output. Skryf n funksie wat twee insette neem, n vektor a en n heelgetal x. Die funksie moet dan tel hoeveelkeer die heelgetal x in die vektor a voorkom. Die hoeveelheid keer moet as uitset gegee word. Rest of page for rough work. Page 7 of 16
Question 2 Answer [10] Page 8 of 16
Question 3 (14) Write a function that takes a rectangular matrix A as input. The entries of A may only contain postive or negative integer values that is no zeros are allowed. The function may assume each entry is an integer. The function must however ensure that no zeros are present. When no zeros are present the function must display a histogram of the number of times each integer present in the matrix occurs. When zeros are present in the matrix the function must display the text Not valid as zeros are present in matrix. in the middle of the figure. Hint: See help hist Skryf n funksie wat n reghoekige matriks A as inset neem. Die inskrywings in A mag slegs postiewe of negatiewe heelgetalle bevat dit is geen nulle nie. Jy mag aanneem dat elke getal n heelgetal is. Die funksie moet egter seker maak dat geen nulle voorkom nie. Indien geen nulle voorkom nie moet die funksie n histogram vertoon van die hoeveelheid keer wat elke heelgetal wat in die matriks is voorkom. Indien nulle in die matriks voorkom moet die funksie n figuur vertoon met die teks Ongeldig nulle kom voor in matriks in die middel van die figuur. Wenk: Sien help hist Rest of page for rough work. Page 9 of 16
Question 3 Answer [14] Page 10 of 16
Question 4 (13) Sudoku is a japanese number placement puzzle. The objective is to fill a 9 9 grid with numbers so that each column and each row contain all of the numbers 1 to 9 exactly once. In addition to this each of the 3 3 subgrids must also contain the numbers 1 to 9 exactly once. An example of a valid sudoku solution is shown below. Sudoku is n japanese nommer plaas kopkrapper. Die doel is om n 9 9 matriks te vul met nommers so dat elke kolom en elke ry al die nommers 1 tot 9 slegs eenkeer bevat. Daarby moet elk van die 3 3 sub-matrikse die nommers 1 tot 9 ook slegs eenkeer bevat. n Voorbeeld van n geldige sudoku oplossing word hier onder vertoon. 5 3 4 6 7 8 9 1 2 6 7 2 1 9 5 3 4 8 1 9 8 3 4 2 5 6 7 8 5 9 7 6 1 4 2 3 4 2 6 8 5 3 7 9 1 7 1 3 9 2 4 8 5 6 9 6 1 5 3 7 2 8 4 2 8 7 4 1 9 6 3 5 3 4 5 2 8 6 1 7 9 Write a function validsudoku that will take a 9 9 matrix and verify whether or not it is a valid sudoku solution. Your function must only return either a true or a false value. Skryf n funksie validsudoku wat n 9 9 matriks sal vat as inset en bepaal of dit n geldige sudoku oplossing is. Jou funksie moet slegs n waar of n false as uitset gee. Rest of page for rough work. Page 11 of 16
Question 4 Answer [13] Page 12 of 16
Question 5 (13) The Lochness Monster, also known as Nessy, has eluded detection for decades. It is rumored that Nessy might be hiding in the deapest darkest corner of the Lochness Lake. Three boats have been dispatched to the Lochness lake to find the deapest spot in the lake. The boats measure the depth of the lake at any point by dropping a weight attached to a cable. We can assume that the depth of the lake at a given position (x, y) is given by the following equation, Die Lochness Monser, ook bekend as Nessy, word al dekades lank gesoek. Dit is berig dat Nessy in die donkerste diepste hoek van die Lochness meer wegkruip. Drie bote is na die meer gestuur om die diepste plek in die meer te soek. Die bote kan die diepte van die meer op enige punt bepaal deur n kabel met n gewig te laat sak en die diepte te meet. Dit kan aanvaar word dat die diepte van die meer op enige punt (x, y) deur die volgende vergelyking gegee word, depth(x, y) = (100 (x 5) 2 (y 4) 2 ) (0.226/100) where the units are in kilometers. waar die eenhede in kilometer is. Initially the boats start in a random position in the lake and you can assume the lake to be a 10km 10km square. The boats are in radio contact with each other and can let each other know where they have found the deapest spot in the lake thus far. After a boat has measured the depth at a given location it must move to a new location. The new location of the boat is determined from the following equation, Die bote begin op n willekeurige punt in die meer en dit kan aanvaar word dat die meer n 10km 10km vierkant is. Die bote is in radio kontak met mekaar en kan mekaar laat weet waar hulle die diepste punt tot so vêr gekry het. Na n boot die diepte gemeet het by n gegewe posisie moet dit aan beweeg tot by n volgende posisie. Die nuwe posisie word gegee deur die volgende vergelyking, y new = y old + rand() (y my_best y old ) + rand() (y best_all_boats y old ) x new = x old + rand() (x my_best x old ) + rand() (x best_all_boats x old ) where y my_best is the location of the current boats deapest recorded depth and y best_all_boats is the location of the deapest recorded depth overall. Similarly with the x values. Each of the three boats continue to move around in this fashion. At a given location they measure the depth and then move to a new location using the given equations. The process stops when the boats have tried a 100 positions. Write a program that will look for the deapest place in the lake using the process with the three boats outlined above. Your program must print out x best_all_boats and y best_all_boats in the end with a suitable message. waar y my_best die posisie is waar die meer die diepste is wat die huidige boot al gekry het en y best_all_boats is die posisie wat die diepste is wat alle bote tot dusver al gekry het. Soort gelyk met die x waardes. Elk van die drie bote herhaal die proses. By enige posisie meet hulle die diepte en beweeg dan aan na n nuwe posisie wat bepaal is deur die gegewe vergelykings. Die proses stop wanneer die bote 100 posisies probeer het. Skryf n program wat die diepste plek in die meer gaan soek deur die proses met die drie bote soos beskryf hierbo. You program moet slegs aan die einde x best_all_boats en y best_all_boats vertoon met n geskikte boodskap. Rest of page for rough work. Page 13 of 16
Question 5 Answer [13] Page 14 of 16
Question 6 (10) Write a recursive function that print the moves to solve the Tower of Hanoi problem. The towers of Hanoi problem: You have three towers, Tower1, Tower2 and Tower3. Initially, Tower1 has n discs of varying size from the biggest at the bottom to the smallest at the top. The aim of the game is to get all the dics from Tower 1 to Tower3 in the same order as what they were on Tower1, by moving only 1 disc at a time and without putting a larger disc on top of a smaller disc. The following recursive formula can be used to move n discs from Tower1 to Tower3: Skryf n rekursiewe funksie wat die skywe vertoon om die Toring van Hanoi probleem mee op te los. Die torings van Hanoi probleem: Jy het drie torings, Toring1, Toring2 en Toring3. In die begin het Toring1 n skywe van variërende groote met die grootste onder end die kleinste bo. Die doel is om al die skywe van Toring1 na Toring3 te beweeg sodat die volgorde dieselfde is as wat hulle op Toring1 was, deur slegs 1 skyf op n slag te beweeg en sonder om n groter skyf op n kleiner skyf te plaas. Die volgende rekursiewe formule kan gebruik word om n skywe van Toring 1 na Toring 3 te verplaas: If n > 1 1. use this procedure to move n 1 smaller discs from Tower1 to Tower2. 2. move largest disc from Tower1 to Tower3. 3. use this procedure to move the n 1 smaller discs from Tower2 to Tower3. If n == 1 move disk from Tower 1 to Tower 3 Indien n > 1 1. gebruik hierdie prosedure om die n 1 kleiner skywe van Toring1 na Toring2 te beweeg. 2. grootste skyf kan van Toring1 na Toring3 beweeg word. 3. gebruik hierdie prosedure om die n 1 kleiner skywe van Toring2 na Toring3 te beweeg. Indien n == 1 beweeg skyf van Toring 1 na Toring 3 Hint: The recursive function should have four inputs. Namely, the number of discs to be moved, Namely, the number of the tower to move from, the number of tower to which to move and the number of the remaining tower. The output for n=3 should look as follows: Wenk: Die rekursiewe funksie moet vier insette hê. Naamlik die aantal skywe wat beweeg moet word, die nommer van die toring waarvan beweeg word, die nommer van die toring waarnatoe beweeg word en die nommer van die oorblywende toring. Die uitset vir n=3 met moet soos volg lyk: move from Tower1 to Tower3 move from Tower1 to Tower2 move from Tower3 to Tower2 move from Tower1 to Tower3 move from Tower2 to Tower1 move from Tower2 to Tower3 move from Tower1 to Tower3 Rest of page for rough work. Page 15 of 16
Question 6 Answer [10] Page 16 of 16