aps105f_2006_exam - UNIVERSITY OF TORONTO FACULTY OF...

Info iconThis preview shows pages 1–15. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

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

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

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

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

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

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

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

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

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

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

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

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13

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

View Full DocumentRight Arrow Icon
Background image of page 14
Background image of page 15
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APSIO5F —— Computer Fundamentals Final Examination —— December, 2006 Examiners: Tom Fairgrieve, W. James MacLean Duration: 2.5 hours o Examination Type A: This is a “closed book” examination; no aids are permitted. o Calculator Type 4: No electronic or mechanical computing devices are permitted. o Write your answers in the spaces provided. Please answer using a pen or dark pencil. o If more space is required, blank pages are provided at the back of the examination. DO NOT detach any pages from the exam paper. 0 Rough work, if necessary, can be done on the backs of the pages. 0 This examination has 15 pages (including the cover page). a Read all instructions and write your name, student number, and ECF login on the first page before you start. 0 You must use the C++ programming language to answer programming questions. 0 While you are. not required to comment any code you write in this exam, useful comments may improve your mark if it helps the marker better understand what you are attempting to do. 0 Except for the true/ false questions, we will award partial marks where possible. Attempt answers to all questions. 0 Enjoy the holiday season! First Name Last Name ____________._..__.—— Student Number _’___..__.___———-_ ECF Login MARKS Question 1 2 a 4 6 “I Value 14 16 10 10 10 10 m| 1. (14 Marks, 1 mark each) For (a) through (m), circle the correct answer for each of the following: (a) (b) (c) (d) (f) (g) (h) (i) (k) (l) (m) True or False: True or False: True or False: True or False: True or False: True or False: True or False: True or False: True or False: True or False: True or False: True or False: True or False: Au sorted linked list can be searched as quickly as a sorted array. When applied on an unsorted array, the insertion sort algorithm performs more element. moves than selection sort. It is always safe to dereference a pointer variable. If we allocate a dynamic array and store the array’s address in arr, then the statement delete arr; de-allocates the array. Adding a node at the head of a linked list, as described in class, is generally faster than adding it at the tail. Recursive algorithms may have more than one base case. Mergesort is faster than selection sort for large arrays. Structures may not be returned by functions. Structures may not have members that are themselves structures. The UNIX I/O redirection operator > appends to the end of a file, if it already exists. Absolute pathnames in UNIX may begin with any letter. A C++ function may return a variable of type pointer to char. When a C++ program ends, all of its dynamically allocated mem- ory is returned to the operating system. If i is a variable and p points to i, which of the following expressions are aliases for i? (1) *p (5) &p (2) *&p (3) *i (4) *&i (6) Sup (7) &i (8) &*i Circle all expressions that you think are aliases for i. 2. (16 hlarks) (a) [5 Marks] Write statements to count the number of characters in a file named someData.txt. Indicate any include directives your statements will require. (b) [3 Marks] W hat. is the output from the following program when executed with the input: IFee Fi Fo Fuml #include (iostream> using namespace std; int main() { char uord[253. *data[4]; for ( int i=0; i<4; i++ ) cin >> word; data[i] = new char[25]; data[i] = word; } for ( int i=0; i<4; i++ ) { cout << dataii} << " ”; } cout << endl; return 0; (c) i. [2 Marks] State one reason for choosing to use an array instead of a linked list. Explain. ii. [2 Marks] State one reason for choosing to use a linked list instead of an array. Explain. (d) [4 Marks] Write the function body for the function having declaration: int *find_midd1e( int a , int n ); When passed an array int a of length n, the function will return a pointer to the army’s middle element. If n is even, choose the middle element with the larger index; for example, if n — 4, consider the middle element to be a[2], not a[1] . 3. (10 Marks) Write a C++ function named findAll to find all occurences of a value in an array of integers. The function will take, as parameters, the array, its size, and the value to be found. You will return a pointer to a linked list node of type struct IndexNode { int index ; IndexNode *next ; }; If the array does not contain the value searched for, return NULL. Otherwise, return a pointer to the head node of a linked list. The list contains, in increasing order, the indices of all occurences of the value sought. Note: In order to get. full marks, your list must contain the indices in increasing order. However, if you cannot achieve this, you may return them in reverse order at a penalty of 1 mark. 4. (10 Marks) Write a function, having the header void reverse(ListNode *khead), that reverses the order of the nodes in the linked list pointed to by the actual parameter corresponding to head. Do not make copies of the list, any of its nodes, or any of the data values. Assume ListNode is defined as struct ListNode { int data ; ListNode *next ; }; Hint: Think carefully before you start. There is a relatively short solution. 5. (10 Marks) A magic square is an arrangement of the numbers from 1 to 112 in an n X n array, with each number occurring exactly once, and such that. the sum of the entries of any row, any column, and either diagonal are the same. It is not hard to Show that. this sum must be "02“). The number n is called the order of the magic square. - For example, the 3 x 3 array 6 7 2 l 5 9 8 3 4 is a magic square because it satisfies the prOperties listed above. (a) [‘2 Marks] Assume there exists a library named MatrixTrivia that contains a function named isMagicSquare. isMagicSquare returns type bool. This function determines whether or not a dynamically allocated n x n array of int is a magic square. Write what you believe the function declaration should be for the isMagicSquare function. You are not required to write the body of the isMagicSquare function. (h) {8 Marks} Write an efficient and complete C++ program that: 0 Prompts the user to enter an order n. o Allocates the amount of memory required to hold an n x n array of int variables. 0 Reads an n X n array of integers into the allocated memory. 0 Prints a message that indicates whether or not the input array is a magic square. Your program should use the isMagicSquare function discussed above. You are not required to write the body of the isMagicSquare function. 0 Returns the allocated memory to the operating system. You may assume that the user will enter a positive order n. Continue your solution for Question 5 on this page. 6. (10 Marks) Recursion (a) [5 Marks] What is the output of the printInOrder function, shown below, if it is called as int arr = {5, 2, 3,-6, 2, 7}; printInOrder(arr,6); You may find that it assists you in tracing the code if you draw a sketch (on the back of the previous page) showing the function calls and their parameters. (Answer goes here) void printBetueen(const int b, const int length, const int index, const int low, const int high) { if (index != length) { if (b[index] > low && b[index] < high) { cout << "# ” ; // extra debugging/tracing output printBetueen(b,1ength,index+1,low,b[index]); cout << b[index] << " " ; printBetueen(b,1ength,index+1,b[index], high); } else { cout << "* ” ; // extra debugging/tracing output printBetueen(b,length,index+1,low,high); } } } void printInUrder(const int al], const int length) { const int smallestInt = ~2147483647; const int largestInt = 2147483647; printBetueen(a,1ength,0, smallestInt,largestInt); cout << endl ; (b) [1 Mark] If the purpose of the function is to print the elements of an array in non-decreasing order (even if it is not already sorted, and ignoring the ’*’ and ’3’ characters), is it working properly? If not, what is wrong? You may assume the input array will never contain the values specified by smallestlnt and largestlnt. (c) [4 Marks} Modify the code to print the list in reverse (non-increasing) order. If you indicated in (b) that the code was not, working properly, fix it) as well. You do not need to rewrite the code in full if you can clearly explain what changes are required ...feel free to mark your changes directly on the code, which has been reproduced below (without the extra debugging/tracing output). void printBetween(const int bl] , const int length, const int index, const int low, const int high) { if (index != length) { if (b{index] > low && b[index] < high) { printBetueen(b,length,index+1,low,b[index]); cout << b[index] << " " ; printBetween(b,length,index+1,b[index], high); } else { printBetween(b,length,index+1,lou,high); } } } void printInUrder(const int all, const int length) { const int smallestInt = -2147483647; const int largestInt = 2147483647; printBetueen(a,length,0, smallestlnt,largestlnt); cout << endl ; 10 7. (10 Marks) In this question you are to write a complete and efficient C++ function named findInString. ete occurrence of a given C—string s2 (excluding The findInString function locates the first compl st. The index of the start of the string 52 in $1 the terminating null character) in a given C—string is returned by f indInString. The value -1 is returned if the string 32 is not. found in the string s1. If s2 is the empty string, "", the findInString function returns the value 0. For example, findInString("splatter" , "latte“) would return ‘2, while findInString("James" , "Tom") would return -1. You may call strlen, but no other functions, from your implementation of findInString. 11 8. (10 Marks) An array of statements were executed to set up such a list: structure variables could be used to keep track of student records. Suppose the following struct Student { char *familyName; char *firstName; int id; }; Student listl400]; a sorted alphabetical order by non—decreasing familyName. e” should appear in the list before the record for e the same familyName, the sorting can be based on dents have the same familyName and firstblame, Lists of this nature are usually stored in For example, the record for student “Fairgriev student “MacLean”. If two or more students hav non-decreasing firstName. And if two or more stu the sorting can be based on increasing id number. (a) [5 Marks] Write an efficient. C++ function named isSorted that determines both whether or not a given array of Student structure variables is in the order specified above. 12 (b) [5 Marks] Write an emcient C++ function that uses the sequential search algorithm to determine the first and last index of a given familyName in a scrted array of Student structures. If the given familyName does not appear in the array, both indices should be set to —1- 13 Continue answers to questions here if necessary. Clearly indicate which question the answer belongs to. 14 Continue answers to questions here if necessary. Clearly indicate which question the answer belongs to. 15 ...
View Full Document

This note was uploaded on 10/16/2010 for the course ECE APS105 taught by Professor Maclean during the Fall '08 term at University of Toronto- Toronto.

Page1 / 15

aps105f_2006_exam - UNIVERSITY OF TORONTO FACULTY OF...

This preview shows document pages 1 - 15. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online