COS1511-2017-6-E-1.pdf - UNIVERSITY EXAMINATIONS...

Info icon This preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 2
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
Image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 6
Image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
Image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
Image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UNIVERSITY EXAMINATIONS UNIVERSITEITSEKSAMENS UNISA‘E‘ univaslty of south afnu C03151 1 Maleune 2017 INTRODUCTION TO PROGRAMMING I Duratlon 2 Hours 100 Marks EXAMINERS FIRST MRS P LE ROUX SECOND MRS A MATHEW Closed book examination This examination question paper remains the property of the Unwerstty of South Africa and may not be removed from the examination venue Thls paper conSIsts of 12 pages INSTRUCTIONS Answer all the questions In the answer book Do all rough work In the answer book as well Number your answers and label your rough work clearly Section A consusts of MCQS In Section B marks are awarded for part of an answer, so do whatever you are abIe to In each question U'I-D-UJNH ALL THE BEST' 2 COSlSlI MAY/JUN E2017 SECTION A 20 MARKS Please answer thlS question in the answer book Choose one optlon for every question Mark your answers clearly if, for example you choose option 2 as the correct answer for Questloo 1, and option 4 as the correct answer for Question 2, please answer In the book as follows 1 2 2 4 etc QUESTION 1 2 marks Suppose the followmg declarations appear In the main functron of a C++ program strlng name, course, char sex, 1nt age, float cost, bool approved, If the followrng function header Is given bool approveAppllcatlonI1nt ageP, Char sexP, strlng nameP, strlng courseP, float costP) which of the options below IS a correct calling statement of the function approveAppl mat 1011 In the maln function? 1 approveApplchatlon (30, m, Peter Slbande, HIV, 5499 00), approved = approveAppllcatlon (30, ‘m', ‘Peter Slbande', ‘HIV', 5499 00), 3 approved = approveAppllcat1onI1nt age, char sex, strlng name, strlng course, float cost), 4 1f (approveAppllcatlon (30, ‘m', “Peter Slbande”, “HIV", 5499 00)) cout << “Yes”, QUESTION 2 2 marks Suppose the followmg declarations appear In the menu function of a C++ program strlng name. quallflcat1on, char sex, 1nt age, yearsExperlence, N If the fo|l0wmg function header IS given v01d f1ndCand1ciate(char sexP, 1nt 8: ageP, strlng 5: name-P, 1nt yearsExperlenceP, strlng quallflcatlonP) Wthh of the options below IS a correct calling statement of the function flndCandldate In the menu function? [TURN oven] J 3 COSlSll MAY/JUNE2017 f1ndCanE11datet ‘m', 30, nameP, 7, “B Curr") , f1ndCand1date(sexP, ageP, nameP, yearsExper1enceP, qual1f1cat10nP), f1ndCand1date(‘m', 30, name, 7, qua11f1cat10n), f1ndCand1date(‘m‘, age, name,7, “B Curr"), QUESTION 3 2 marks ' Suppose the followung declarations appear In the ma 1n function of a C++ program waH str1ng name, course, date, float cost, bool approved, Suppose the followmg calling statement appears In the menu function approveFunds(“Memory enhancement”, “Ellen Sublsa", date, 595 00, approved), Wthh of the optlons below Is a correct function header of the function approveFunds In the menu function? 1 v01d approveFunds(str1ng courseP, strlng nameP, strlng & dateP, float costP, bool & approvedP) , 2 v01d approveFunds{str1ng & courseP, str1ng & nameP, str1ng dateP, float & costP: bool approvedP) 3 vo1d approveFunds(course, name, date, cost, approved) 4 vo1d approveFunds(“Memory enhancement”, “Ellen Sublsa", date, cost, approved) QUESTION 4 2 marks Whrch of the options below expresses the logic that neither of the followmg two statements are true? 0 1 Is less than 4 or i as greater than 10, o and 1 times j IS greater than 54 1 ('((:L < 4 || 1 > 10) 8:5; (1*) > 54))} 2 ('((:L < 4 H 1 > 10) H (1*) > 54))) 3 ((1<4 |I1>10) I} (1*]>54)) 4 ('((1 < 4 8:5: 1 > 10) 6:8: (l*j > 54))) [TURN oven] 4 | ‘ COSlSll MAY/JUNE2017 QUESTION 5 2 marks Whlch of the options below expresses the logic that a person :5 ready If they are erther not wearing Jeans or they are wearing a hat and bronx shoes? 1 ready = Clothlng"= “jeans" || hat =5 ‘y' && shoes == “bronx”, 2 ready = cloth1ng 1: “jeans" && hat == ‘y’ [I shoes == “bronx”, 3 ready = clothlng I= “jeans” || hat z; ‘y' || ehoes == “bronx”, 4 ready = clothlng ‘2 “jeans" && (hat == ‘y' && shoes == “bronx"). \ QUESTION 6 2 marks f Consrder the followung C++ code segment 1f (1 == 3) ~ cout << "1", ‘ else 1f ((1 % J) < 3) cout << "2", else 1f (1 < (3-1)) cout << "3", else ~ cout << "4“, ; cout << "5", « If the value of 1 I5 5 and the value of j as 6, Wthh of the optcons below gives the correct output? 25 * 35 ‘ 15 45 QUESTION 7 - 2 marks ConSIder the followung C++ code segment #WNH for (1n: 1 0, 1 < n, ++i) { for (1:11: 3 = 0, j < m, ++]) . { 1f (1 I= j} dout << "0“, else cout << "1", } } UURNOVEM 5 c051511 MAY/JUNE2017 Wthh of the optlons below gives the correct output If the value of n :s 2 and the value of m IS 3? 1 100010 2 011101 3 100100 4 010001 Questions 8 to 10 are based on the followmg C++ program Note that the conventions as explamed In the Study Guude are used ”lthe varlable duagrarns 1 #1nclude <1ostream> 2 flinclude <5tr1ng> 3 USlng namespace std, 4 float getAmount(float & prlceP, strlng 1temP, 1nt & howmanyP) 5 { 6 float total = prlceP, 7 1f (1temP 1: "book” && prlceP > 200) 8 1temP = “book", 9 else if (1temP == "birthday card" && howmanyP >= 10) 10 prlceP = prlceP + 10 / 5, 11 total += ptlceP, 12 1f {prlceP <= 12) 13 1temP = "penc11", 14 howmanyP = 50, 15 return total, 16 } 17 1nt ma1n( ) 15 { 19 strlng 1tem, 20 float prlce : 10, fee, 21 1nt howmany : 40, 22 fee = 20, 23 1tem : "blrthday card", 24 fee = getAmountlprlce, 1tem, howmany), 25 1f (item == "book") 26 fee += 100, 27 else 1f (1tem == “penc11") 28 fee —= 6, 29 else fee += 15, 30 cout << "Total cost for 1tems bought 15 R" << fee << endl, 31 return 0, 32 } {TURN OVER] 6 COSlSll MAY/JUNEZUl? QUESTION 8 2 marks Which of the options below correctly reflects the Situation after Lune 10 has been executed? [price] _[pr1ceP {learn} |1cemP {howmany} [howmanyp [fee] total [prlce] IprlceP [item] [howmany] |howmanyP [fee] 1tem}? total Option [prlce] lprlceP [1tem] [howmany] lhomnanyP [fee] itemP total [price] IprlceP [item] |1temP [howmany] prlceP {fee} howmany? total QUESTION 9 2 marks Which of the OpthnS below correctly reflects the Situation after Lme 14 has been executed? [prlce] IprlceP [item] |1temP [howmany3 IhowmanyP [fee] total [TURN OVER} 7 C051511 MAY/JUN E2017 [prlce] IprlceP [1tem] [hcwmany] lhowmanyP [fee] 1temP total [price] [Item] IltemP [howmany] "mummy cad prlceP [fee] howmanyP total [prlce] [prlceP [item] [howmany] —|howmanyP [fee] 1temP total QUESTION 10 2 marks Wthh of the options below correctiy reflects the sutuatlon after Lme 29 has been executed? m- Price Item howmany fee pm howmany fee m- prlce 1tem hCWmany — fee was new howmany [TURN OVER} 8 C05 1511 MAY/JUN E2017 n- "pencm fee 16 SECTION B 80 MARKS QUESTION 1 12 marks Parents of the pupils of the Park Primary School must pay an amount for outfits for the annual play All pupils take part m the play, except the Grade 0 pupils The amount that the parents have to pay IS calculated as follows - The cost of the OUtfltS for Grade 1 and 2 pupils l5 R45 0 The cost of the outfits for Grades 3 to 5 IS R65 ‘ Grade 6 and 7 puplls may play one or two roles If they play a leadrole, they may only play one role The cost of the outfits IS R70 If they play one role If thlS role Is a leadrole, the cost IS R100 If they play two roles, the cost IS R130 Use a switch statement and write down ONLY the necessary C++ statements to calculate and display the amount to he paid or display an appropriate error message If requrred Do NOT wnte a complete program Use the followrng variables 1n: grade, 1nt fee, bool leadrole, // true 1f a (fluid plays a leadrole bool rolesz, // true 1f a chlld plays 2 roles Assume that values have been a55Igned to these variables already QU ESTlON 2 10 marks A constructton company pays Its temporary workers R100 per day If they work on a Monday to Saturday and R200 if they work on a Sunday They only work when the company contacts them, so they do not work the whole month Complete the program below that asks the user to enter the days that the worker has worked The character '1' Is entered 1f the day IS Monday to Saturday and ‘2' If it IS a Sunday A total IS also updated to count the number of Sundays that was worked The program must then calculate the total amount that the worker must be pazd at the end of the month When all days have been entered, the character ‘x' IS entered to terminate the loop Use an appropriate loop structure Do not lntroduce any additional variables Write down ONLY the mussung statements [TURN OVER] 9 0351511 fidAY/MJNE2017 #1nclude <1ostream> u51ng namespace std, lnt malnl) { const float SUNDAY = 200, // salary per day for sundays const float OTHER = 100, // salary per day for other days char whlchDay, // '2’ for Sundays, ‘1‘ for other days float totalAmount, // total salary 1nt anundays, // number of Sundays worked cout << “Enter first day ‘2' for Sunday. ‘1' for other days” << endl, // YOUR STATEMENTS SHOULD COME IN HERE cout << “Total salary " << totalAmount << endl, cout << “number of Sundays worked " << anundays << endl, return 0, } QUESTION 3 13 marks In this question you have to write a complete function MyMedia Publishers uses two parallel arrays to keep track of the number of subscriptions for each of the” 50 publications Array publlcatlons holds the names of the magazmes and newspapers published and array subscrlptlons holds the number of Subscriptions for each corresponding magazine or newspaper You have to write a VOld function, called flndMOStSubS to determine which publication has the most subscribers Function flndMostSubs has to return the name of the publication as well as the number of subscribers to that publication Assume the followmg - a declaration of a global constant const 1nt NUM_PUBS = 50, //number of publlcatlons - four declaration statements in the main function strlng publicatlons[NUM_PUBS], // tltles of the publlcatlons 1nt subscrlptlons [NUMWPUBS] , // number of subscrlptlons for the // corresponding publlcatlons 1nt nrMostSubscrlptlons, // number of subscrlptlons for // publlcatlon w1th most subscrlptlons strlng mostSubscrlptlons, // tltle of publlcatlon w1th most // subscrlptlons 0 values have been aSSIgned already to all the elements of the arrays 0 the function 15 called in the main program as follows flndMDStSubS (publlcat lons. subscrlptlons, mostSubscrlptlons, nrMOStSubscrlptlons), [TURN OVER] 10 COSlSll MAY/JUNE2017 erte down ONLY the complete function flndMostSubs QUESTION 4 16 marks 4 1 Declare two Integer constants ROWSIZE equal to 4 and COLSIZE equal to 4 (4) 4 2 Declare two 1nt two-dlmenSIonal arrays, namely {4) 0 playl With ROWSIZE number of rows and COLSIZE number of columns, and I play2 Wlth ROWSIZE number of rows and COLSIZE number of columns, 4 3 Assume that values have been assIgned to all the elements of playl and play2 Also assume that an 1111; varIable trley has been declared and IntIaIIsed to 0 Use nested for loops and write down the necessary C++ statements to do the followmg - Each row of playl l5 compared element by element to the correspondmg row of pl ay2 o If the elements are equal, add the value of one element to trlcky - If the elements are not equal, add the sum of the two elements to the value of trlcky For example, prlayl and play2 are InItIalIsed as follows 1 5 3 '7 4 6 3 '7 6 8 5 8 and 5 O 7 8 3 3 3 5 8 7 6 5 '7 8 l 5 '7 3 4 5 respectIvelv, then the value of tricky wnll be 5+1l+3+7+ll+8+12+8+ll+l0+9+5+7+ll+5+5=128 Dlsplav the value of trlcky Do NOT wrlte a complete program or any functlons Wnte down ONLY the reqmred statements (8} QUESTION 5 14 marks A bathroom warehouse keeps the followmg Information for the Items In stock 0 descrlptlon (a strung, for example "bath") a code to dlStlngUISh between Items wrth the same descrIptIon and color but of a dIfferent deSIgn (a strIng to store 6 dIgIts, for example ”123456") 0 color (a strIng, for example "thte") 0 number In stock (an Integer, for example 49} o prlce (a floatmg ponnt number, e g 349 95) 5 1 Wnte down the declaratlon for a struct for stonng the InformatIon assomated WIth one kInd of Item In stock Gave the name Item to the struct (6) [TURN oven] 11 COSlSll MAY/JUNE2017 5 2 Assume that an array Item stock [50] has been declared and that mformatlon for 50 Items In stock has been stored In the array The program fragment below prints the codes for all the white baths and determines the total number ’ of whlte baths In stock Now write down ONLY the necessary C++ Instructions for line numbers 2, 4, 6, 8, 9 and 11 to complete thlS program fragment Write down only the lrne number and the iflStl’UCthfl that should appear next to the lune number (8) 1 Hit total, 2 f/lnltlallse total //search through array to find all whlte baths for ( ) // examlne all 1tems { //‘cest 1f 1tem is a whlte bath { // prlnt codes for whlte baths // determlne the total number of whlte baths 111 stock l—‘l—lLDm-JIONUIbbLU 0 l 1 // dlsplay total number of whlte baths 111 stock QUESTION 6 15 marks ln this question you have to write a C++ program to convert a date from one format to another You have to write a complete program consrstmg of a ma1n() function and a function called convertDate () The function recewes a string of characters representing a date in Amerlcan format, for example December 29 , 1953 The function has to convert thIS date to the International format For example If the string December 29, 1953 IS FECENEd, the string that the function should return must be 29 December 1953 Use the followmg C++ skeletOn 1nclude <1ostream> #1nclude <str1ng> u51ng namespace std, strlng convertDate( ) l // Add the code for the function here [TURN OVER] 12 C0515 11 MAY/JUNEZOl? 1m: ma1n() { strlng amerlcanDate, // Add the code for the mam functton here return 0, } The main” function shoutd prompt the user to enter the string, then read the strung and call function convertDate () to convert It to the requured form Hint 1 Below the question we Inst 3 number of string member functions you may need Hmt 2 Start the convertDate H function by determining the posmon of the first space character In the 1nput A number of string member funcuons to help you StrlngObject Slze( ) SCrlngObjeCt substr(startPos, length} StrlngObject f1nd(substr1ng) StrlngObject f1nd(substr1ng, startPos} Strlngobject 1nsert(1nsertPos, substrlng) , S’crlngobject erase(startPos, length), Strlngobject replace(startPos, length, substrlng), where startPos, length and 1nsertPos are of type 1111:, and substrlng IS of type strlng ...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern