COS1511 May/Jun 2016 exam paper — questions
The paper, as written
Scanned page 1 of 9 Scanned page 2 of 9 Scanned page 3 of 9 Scanned page 4 of 9 Scanned page 5 of 9 Scanned page 6 of 9 Scanned page 7 of 9 Scanned page 8 of 9 Scanned page 9 of 9
Every question, by topic
Question 1.1 · Selection: if/else and switch statements · 2 marks
Given the C++ code segment below, determine what value the variable newval will hold once the code has finished executing: int var1 = 4; int var2 = 10; int newval = 0; if (var1 * 2 >= var2) newval = 5 + 2 * var2; else if (var1 < var2) newval = var2 - var1 * 2; else newval = var1;Show the full question
Question 1.2 · Selection: if/else and switch statements · 2 marks
If the value 5 is entered as input for the variable a, work out what a will equal after the following C++ code has executed: int a; cin >> a; switch (a) { case 1: a += 3; case 3: a = a * 3; break; case 5: a = ++a + 10; case 6: a /= 2; default: a = a + 1; }Show the full question
Question 1.3 · Selection: if/else and switch statements · 2 marks
Study the C++ code segment below: int findValue(int numberP) { int count = 0; int value = 20; while (count < numberP) { value += count; count++; } return value; } Determine what will be printed to the screen when the following statement is executed inside the main function: cout << findValue(3);Show the full question
Question 2.1 · Code tracing and variable diagrams · 2 marks
Questions 2.1 and 2.2 each require you to write down, in words, what the purpose of the given segment of code is. Before answering, consider this example: given the code 'int a,b,c; cin >> a >> b >> c; cout << c + b + a;', the purpose of this code segment is to input three integer values and display their sum. Assuming that s and n have been declared as integers, explain in words what the purpose is of the following code segment: int s = 0; int n = 0; while (n <= 5) { s = s + n; n++; }Show the full question
Question 2.2 · Code tracing and variable diagrams · 2 marks
Questions 2.1 and 2.2 each require you to write down, in words, what the purpose of the given segment of code is. Before answering, consider this example: given the code 'int a,b,c; cin >> a >> b >> c; cout << c + b + a;', the purpose of this code segment is to input three integer values and display their sum. Explain in words what the purpose is of the following code segment: int numbers[] = {11, 0, 15, 0, 16, 23}; int c = 0; for (int i = 0; i <= 5; i++) if (numbers[i] != 0) c += 1;Show the full question
Question 3 · Code tracing and variable diagrams · 8 marks
Examine the following C++ function: line 1: int result(int valueP) line 2: { line 3: int a = 2; line 4: int count = 0; line 5: while (count < valueP) line 6: { line 7: a += count + a / 2; line 8: count += 2; line 9: } line 10: return a; line 11: } Show, by means of a variable diagram that traces the execution of the code line by line, what the output of this program will be when valueP is equal to 6. Your variable diagram must illustrate what the code does at each step.Show the full question
Question 4.1 · Selection: if/else and switch statements · 2 marks
Karlton Learning wants a program that displays the amount of money a company owes for a seminar. The fee charged per person depends on how many people the company registers: for 1 through 4 registrants the fee is R100,00 per person; for 5 through 10 registrants the fee is R80,00 per person; and for 11 or more registrants the fee is R60,00 per person. For example, if a company registers 7 people, the amount owed is R560,00. If the user enters a number of registrants that is less than or equal to 0, the program must display an appropriate error message. You are given the skeleton of a C++ program starting with '#include <iostream>', 'using namespace std;' and 'int main() { ... return 0; }', and must complete the missing statements as indicated by the comments for each sub-question. Inside the given program skeleton, declare a variable to hold the number of registrants and a variable to hold the amount a company owes for the seminar.Show the full question
Question 4.2 · Selection: if/else and switch statements · 2 marks
Karlton Learning wants a program that displays the amount of money a company owes for a seminar. The fee charged per person depends on how many people the company registers: for 1 through 4 registrants the fee is R100,00 per person; for 5 through 10 registrants the fee is R80,00 per person; and for 11 or more registrants the fee is R60,00 per person. For example, if a company registers 7 people, the amount owed is R560,00. If the user enters a number of registrants that is less than or equal to 0, the program must display an appropriate error message. You are given the skeleton of a C++ program starting with '#include <iostream>', 'using namespace std;' and 'int main() { ... return 0; }', and must complete the missing statements as indicated by the comments for each sub-question. Write the statements needed to input the number of registrants. If the user enters a number that is less than or equal to 0, the program should display an appropriate error message.Show the full question
Question 4.3 · Selection: if/else and switch statements · 4 marks
Karlton Learning wants a program that displays the amount of money a company owes for a seminar. The fee charged per person depends on how many people the company registers: for 1 through 4 registrants the fee is R100,00 per person; for 5 through 10 registrants the fee is R80,00 per person; and for 11 or more registrants the fee is R60,00 per person. For example, if a company registers 7 people, the amount owed is R560,00. If the user enters a number of registrants that is less than or equal to 0, the program must display an appropriate error message. You are given the skeleton of a C++ program starting with '#include <iostream>', 'using namespace std;' and 'int main() { ... return 0; }', and must complete the missing statements as indicated by the comments for each sub-question. Write the statements needed to calculate and display the amount of money a company owes for the seminar, based on the number of registrants entered and the fee structure described (R100,00 per person for 1 through 4 registrants, R80,00 per person for 5 through 10 registrants, and R60,00 per person for 11 or more registrants).Show the full question
Question 5 · Selection: if/else and switch statements · 5 marks
The Department of Road Works wants to plan road maintenance by determining road usage, which is done by counting the number of vehicles using a road that have two wheels, four wheels, or more than four wheels respectively. Using the variables int nrWheels (the number of wheels of a vehicle using the road) and int countTwo, countFour, countMore (the counters for vehicles with two, four, and more than four wheels respectively), write down ONLY the necessary switch statement to count the number of vehicles with two wheels, four wheels and more than four wheels. Do NOT write a complete program. You may assume that countTwo, countFour and countMore have already been initialised, and that a value has already been input and validated for nrWheels.Show the full question
Question 6.1 · Loops: for, while and do-while · 2 marks
Write a for-statement that displays the numbers 1 through 10 on the screen.Show the full question
Question 6.2 · Loops: for, while and do-while · 3 marks
Write a while-statement that displays the numbers 1 through 10 on the screen.Show the full question
Question 6.3 · Loops: for, while and do-while · 2 marks
The following code is intended to display the numbers 1, 2, 3 and 4 on the screen, but it is not working correctly. Correct the errors in the code: line 1 reads 'int num = 1,'; line 2 reads 'while (num < 4)'; line 3 reads 'cout << num << endl,'; line 4 reads '// endwhile'.Show the full question
Question 6.4 · Loops: for, while and do-while · 2 marks
The following code is intended to display each salesperson's commission, calculated by multiplying the salesperson's sales by 10%, but it is not working correctly. Correct the errors in the code: line 1 reads 'float sales = 0 0,'; line 2 reads 'float commission = 0 0,'; line 3 reads 'cout << "Enter a sales amount ",'; line 4 reads 'cin >> sales,'; line 5 reads 'while (sales > 0 0)'; line 6 reads '{'; line 7 reads 'Commission = sales * 0.1,'; line 8 reads 'cout << commission << endl;'; line 9 reads '}'; line 10 reads '// endwhile'.Show the full question
Question 7 · Arrays (one- and two-dimensional) · 6 marks
The Fruit Packers keep a record of the number of crates of fruit packed each day for a whole year (365 days). These 365 values are stored in an int array called crates. Write a complete function, called calcAverage, that determines the average number of crates packed per day for the year. Assume that a global constant has been declared as const int NUM_DAYS = 365; (the number of days per year), and that the main function contains the two declarations int crates[NUM_DAYS]; (the array of days) and int average; (the average number of crates). Assume further that values have already been assigned to all the elements of the array, and that the function is called in the main function as follows: average = calcAverage(crates); Write down ONLY the complete function calcAverage.Show the full question
Question 8.1 · Functions and parameters · 1 mark
Write a function header for a function named check that takes two parameters: the first parameter must be an integer value and the second parameter must be a floating point value. The function should return no value.Show the full question
Question 8.2 · Functions and parameters · 1 mark
Write a function header for a function named mult that takes two floating point numbers as parameters and returns the result of multiplying them.Show the full question
Question 8.3 · Functions and parameters · 1 mark
Write a function header for a function named time that inputs values for seconds, minutes and hours, and returns these three values as parameters back to the function that calls it.Show the full question
Question 8.4 · Functions and parameters · 1 mark
Write a function header for a function named countLets that returns the number of occurrences of a given character within a given string, where both the character and the string are supplied to the function as parameters.Show the full question
Question 8.5 · Functions and parameters · 2 marks
Suppose the main function of a C++ program contains the following declarations: an integer variable number, and floating point variables cost, markup and discount. Also suppose the main function contains the following calling statement: amount = calcFinal(67.50, markup, discount, 5); Write the correct function header for the function calcFinal.Show the full question
Question 8.6 · Functions and parameters · 2 marks
Suppose the main function of a C++ program contains the following declarations: a string variable dayOfWeek, integer variables productCode and number, and a floating point variable discount. Given that the function header is: void calcDiscount(float & discountP, int productCodeP, int numberP, string dayOfWeekP), write the correct calling statement for the function calcDiscount within the main function.Show the full question
Question 9.1 · Arrays (one- and two-dimensional) · 2 marks
Bookworm bookstores ran a competition over four weeks for its three branches, with the branch recording the highest sales for a given week receiving a surprise for that week. As the programmer, you must write a program that keeps record of the number of books sold per week at each of the three branches over the four-week period, storing the figures in a two-dimensional array called sales with four rows and three columns, and then displays, for each week, the highest number of books sold. Example input data for the program is: Week 1 - Bookworm South 100, Bookworm West 120, Bookworm North 103; Week 2 - Bookworm South 96, Bookworm West 122, Bookworm North 111; Week 3 - Bookworm South 110, Bookworm West 101, Bookworm North 119; Week 4 - Bookworm South 106, Bookworm West 99, Bookworm North 102. The corresponding output must read: 'The highest sales in week 1 were 120 books.', 'The highest sales in week 2 were 122 books.', 'The highest sales in week 3 were 119 books.', 'The highest sales in week 4 were 106 books.' You are given the following incomplete program and must use its declarations to complete the tasks below: #include <iostream>; using namespace std; const int NUM_WEEKS = 4; const int NAME = 3; int main() { int highest; // array sales should be declared here (part 9.1) // Assume statements to input the array here // Do not write these statements // Your statements to determine and display the highest // number of sales per week (part 9.2 of the question) return 0; } Declare the two-dimensional array sales, using the NUM_WEEKS and NAME constants already defined, so that it can hold the number of books sold for four weeks across three branches.Show the full question
Question 9.2 · Arrays (one- and two-dimensional) · 6 marks
Bookworm bookstores ran a competition over four weeks for its three branches, with the branch recording the highest sales for a given week receiving a surprise for that week. As the programmer, you must write a program that keeps record of the number of books sold per week at each of the three branches over the four-week period, storing the figures in a two-dimensional array called sales with four rows and three columns, and then displays, for each week, the highest number of books sold. Example input data for the program is: Week 1 - Bookworm South 100, Bookworm West 120, Bookworm North 103; Week 2 - Bookworm South 96, Bookworm West 122, Bookworm North 111; Week 3 - Bookworm South 110, Bookworm West 101, Bookworm North 119; Week 4 - Bookworm South 106, Bookworm West 99, Bookworm North 102. The corresponding output must read: 'The highest sales in week 1 were 120 books.', 'The highest sales in week 2 were 122 books.', 'The highest sales in week 3 were 119 books.', 'The highest sales in week 4 were 106 books.' You are given the following incomplete program and must use its declarations to complete the tasks below: #include <iostream>; using namespace std; const int NUM_WEEKS = 4; const int NAME = 3; int main() { int highest; // array sales should be declared here (part 9.1) // Assume statements to input the array here // Do not write these statements // Your statements to determine and display the highest // number of sales per week (part 9.2 of the question) return 0; } Assuming the sales array has already been filled with values, write a program fragment that determines and displays, for each of the four weeks, the highest number of books sold among the three Bookworm branches, producing output in the form 'The highest sales in week 1 were 120 books.' for each week.Show the full question
Question 10.1 · Structures (structs) · 2 marks
Consider the following struct definition used throughout this question: struct Product contains the fields name (of type String), weight (of type float) and price (of type float). Given the Product struct definition (with fields name, weight and price), suppose the declaration 'Product p1;' appears in the main function of a C++ program. Write a cout statement that will display all the fields of the Product p1.Show the full question
Question 10.2 · Structures (structs) · 4 marks
Consider the following struct definition used throughout this question: struct Product contains the fields name (of type String), weight (of type float) and price (of type float). Write a function called read_Product_Record which accepts a reference parameter named new_Product of type Product. The function read_Product_Record must fill new_Product with values entered from the keyboard.Show the full question
Question 11 · Strings and string functions · 7 marks
Write a function that accepts as input a person's first names and surname, and then outputs only the initials formed from those names. For example, given the input John Peter Doe, the function must display the initials JPD. A table of string class member functions you may use to manipulate string objects is supplied: int size() returns the length of a string object; string substr(int,int) returns a substring of a string object, where the first parameter gives the starting position and the second parameter gives how many characters to copy, with the second parameter optional (if omitted, the substring runs from the starting position to the end of the string); int find(string,int) returns the position of a given string within a string object, with an optional second parameter specifying where to start the search (if omitted, the search starts at the beginning), returning -1 if the sought string is not found; void insert(int,string) inserts a given string into a string object at a specified position; void erase(int,int) erases a substring from a string object starting at a given position for a given number of characters; and void replace(int,int,string) replaces characters of a string object, determined by a start position and a count, with another given string.Show the full question