UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Similar documents
UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

COP 3223 Introduction to Programming with C - Study Union - Spring 2018

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

COP 3223 Introduction to Programming with C - Study Union - Fall 2017

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Sample Examination. Family Name:... Other Names:... Signature:... Student Number:...

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

ESC101N: Fundamentals of Computing End-sem st semester

ECE264 Spring 2013 Final Exam, April 30, 2013

Final Intro to C Review

ECE264 Fall 2013 Exam 3, November 20, 2013

EECE.2160: ECE Application Programming Fall 2017 Exam 3 December 16, 2017

Computer Science Foundation Exam

This is CS50. Harvard University Fall Quiz 0 Answer Key

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Computer Science Foundation Exam

Do not start the test until instructed to do so!

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Fundamental of Programming (C)

ECE 264 Exam 2. 6:30-7:30PM, March 9, You must sign here. Otherwise you will receive a 1-point penalty.

Signature: ECE 551 Midterm Exam

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #06

Dynamic Data Structures (II)

Midterm Examination # 2 Wednesday, March 18, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

Question 1. [15 marks]

University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Final Exam

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Foundation Exam - Sample Problems for New Topics

Class Information ANNOUCEMENTS

Subject: Fundamental of Computer Programming 2068

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Computer Science Foundation Exam

MTH 307/417/515 Final Exam Solutions

CSCI-243 Exam 1 Review February 22, 2015 Presented by the RIT Computer Science Community

Arrays and Applications

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

Problem 2 Add the two 2 s complement signed 8-bit values given below, and express your answer in decimal.

Q1: Multiple choice / 20 Q2: Arrays / 40 Q3: Functions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

Principles of Programming. Chapter 6: Arrays

Computer Science Foundation Exam

Comp Sci 1MD3 Mid-Term II 2004 Dr. Jacques Carette

Ricardo Rocha. Department of Computer Science Faculty of Sciences University of Porto

CSE 333 Midterm Exam July 24, Name UW ID#

CS 0449 Sample Midterm

SOFTWARE Ph.D. Qualifying Exam Spring Consider the following C program which consists of two function definitions including the main function.

THE UNIVERSITY OF WESTERN ONTARIO. COMPUTER SCIENCE 211a FINAL EXAMINATION 17 DECEMBER HOURS

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

EE 312 Fall 2018 Midterm 1 Version A October 10, 2018

ECE551 Midterm Version 1

Make sure the version number is marked on your scantron sheet. This is Version 1

Algorithms & Programming. Midterm Examination # 2. Student Name: Student ID Number: Duration of examination:

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

CSE 303 Midterm Exam

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Questions Bank. 14) State any four advantages of using flow-chart

EE 312 Fall 2017 Midterm 1 October 12, 2017

Programming in C. Lecture 5: Aliasing, Graphs, and Deallocation. Dr Neel Krishnaswami. Michaelmas Term University of Cambridge

COP 3502 (Computer Science I) Final Exam 12/10/2015. Last Name:, First Name:

CS61, Fall 2012 Section 2 Notes

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Quiz 0 Review Session. October 13th, 2014

Programming in C++: Assignment Week 1

Computer Science Foundation Exam

Computer Science Foundation Exam

CS Introduction to Programming Midterm Exam #2 - Prof. Reed Fall 2015

C0MP1911 Final Exam 1337 Computing 1

CSC209H1S Day Midterm Solutions Winter 2010

C Programming Language: C ADTs, 2d Dynamic Allocation. Math 230 Assembly Language Programming (Computer Organization) Thursday Jan 31, 2008

'C' Programming Language

15 122: Principles of Imperative Computation. Practice Exam August 4, 2015

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

ECE551 Midterm Version 2

ECE 551D Spring 2018 Midterm Exam

ECE264 Fall 2013 Exam 1, September 24, 2013

Midterm Examination # 2 Wednesday, March 19, Duration of examination: 75 minutes STUDENT NAME: STUDENT ID NUMBER:

EECE.2160: ECE Application Programming Fall 2017

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Bubble sort starts with very first two elements, comparing them to check which one is greater.

EECE.2160: ECE Application Programming Spring 2018 Exam 3 May 10, 2018

Answer all questions. Write your answers only in the space provided. Full marks = 50

Q1: /20 Q2: /30 Q3: /24 Q4: /26. Total: /100

CS261: HOMEWORK 2 Due 04/13/2012, at 2pm

Here's how you declare a function that returns a pointer to a character:

CSE 333 Midterm Exam 2/14/14

United States Naval Academy Electrical and Computer Engineering Department EC310-6 Week Midterm Spring AY2017

UNIVERSITY OF SWAZILAND

High Performance Programming Programming in C part 1

CS 108 Computing Fundamentals. October/November Array Bootcamp

Advanced Pointer Topics

Transcription:

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Final Examination December 14, 2012 2:00 p.m. 4:30 p.m. (150 minutes) Examiners: J. Anderson, B. Li, M. Sadoghi, D. Sengupta, G. Steffan Exam Type A: This is a closed book examination; no aids are permitted. Calculator Type 4: No calculators or other electronic devices are allowed. All questions are to be answered on the examination paper. If the space provided for a question is insufficient, you may use the last page to complete your answer. If you use the last page, please direct the marker to that page and indicate clearly on that page which question(s) you are answering there. You must use the C programming language to answer programming questions. You are not required to write #include directives in your solutions. You may use any math function that you have learned, as necessary. The examination has 22 pages, including this one. Circle your lecture section (one mark deduction if you do not correctly indicate your section): L01 L02 L03 L04 L05 L06 Li Li Anderson Steffan Sengupta Sadoghi Monday 2 PM Monday 9 AM Monday 11 AM Monday 11 AM Monday 4 PM Monday 4 PM Full Name: Student Number: UTORID: MARKS 2-3 4 5-6 7 8 9 10 11 12 14 15 16 18 20 Total /6 /6 /8 /4 /4 /8 /8 /8 /8 /8 /8 /8 /8 /8 /100

Question 1 [3 Marks] How would the following sorting algorithms perform when sorting the example arrays given? In particular, for each algorithm, count the number of times that two numbers from the array would be compared, and give that number as an answer. Array 1: 5 4 3 2 1 Algorithm Answer Selection sort 10 Insertion sort 10 Array 2: 1 4 2 5 3 Algorithm Answer Selection sort 10 Insertion sort 7 Array 3: 1 2 3 4 5 Algorithm Answer Selection sort 10 Insertion sort 4 2

Question 2 [3 Marks] Consider the following C program: #include <stdio.h> void visitgrid(int x, int y) if (x < 10 && y < 10) visitgrid(x + 1, y); visitgrid(x, y + 1); printf("%d,%d\n", x, y); int main(void) visitgrid(0, 0); return 0; In the space below, write only the first three lines that would be printed by its execution. 9,9 9,8 9,7 3

Question 3 [3 Marks] Write a single C statement that declares a int type variable named r and initializes it to a random number between 50 and 1500 inclusive, where the random number is divisible by 10. int r = (rand() % 146 + 5) * 10; Question 4 [3 Marks] Circle and correct the errors in the following complete C program. You should cross out any lines with errors and rewrite them. #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) char *str = pass the exam ; char *str2 = (char)malloc(strlen(str)*sizeof(char)); strcpy(str2, str); printf("%s %s\n", str, str2); return 0; A correct execution of the program should print: pass the exam pass the exam 1) malloc result should be cast to char * 2) we need to malloc strlen(str)+1 [for the NULL character] 3) should be " " around the string instead of 4

Question 5 [4 Marks] What output does the following C program produce? #include <stdio.h> int foo(int *num) int result = *num * 2; num = &result; (*num)++; return *num + result; int main(void) int num = 3; printf("num = %d, foo(num) = %d, num = %d\n", num, foo(&num), num); return 0; num = 3, foo(num) = 14, num = 3 5

Question 6 [4 Marks] What output does the following program produce? #include <stdio.h> #include <string.h> void foo(char *s, char *d) if(s!= d) foo(s + 1, d - 1); printf("%c %c\n", *s, *d); int main(void) char *hello = "Hello World"; foo(hello, hello + strlen(hello) - 1); return 0; o W l o l r e l H d 6

Question 7 [4 Marks] What is the output of the following program? #include <stdio.h> void quicksort(int list[], int low, int high) printf("%d,%d\n", low, high); if (low <= high) double pivot = list[low]; int left = low; int right = high; while (left < right) while (list[right] >= pivot && left < right) right--; list[left] = list[right]; while (list[left] <= pivot && left < right) left++; list[right] = list[left]; list[left] = pivot; quicksort(list, low, left - 1); quicksort(list, right + 1, high); int main(void) int list[] = 5, 7, 2; quicksort(list, 0, 2); 0,2 0,0 0,-1 1,0 2,2 2,1 3,2 7

Question 8 [4 Marks] Given the following C function: void thenineagesofman() char words[5][8] = "not", "old", "enough", "to know", "better"; for (int i = 0; i < 9; i++) int start = i % 2; int stop = 5 - i/2; for (int j = start; j < stop; j++) printf("%s ", words[j]); printf("\n"); In the space below, write the output that would be printed if this function were called (a poem by Emerson Andrews). not old enough to know better old enough to know better not old enough to know old enough to know not old enough old enough not old old not 8

Question 9 [8 Marks] Write a C function called bordersum, the prototype of which is given below, that returns the sum of the values in the border of a n x n array containing integers. The sum should include values in the top and bottom rows, and the leftmost and rightmost columns, but include the corner values in the sum only once. For example, if the input array a is: 8 6 9 5 1 5 2 4 9 12 16 8 13 10 11 16 with n = 4, the function bordersum(4, a) should return 100. Assume that the value of N is greater than 0. Hint: Consider three different cases of n: n = 1, n = 2 and n > 2. int bordersum(int n, int **a) int sum = 0, col, row; if(n == 1) sum = a[0][0]; else if (n == 2) sum = a[0][0] + a[0][1] + a[1][0] + a[1][1]; else for (col = 0; col < n; col++) sum += a[0][col] + a[n-1][col]; for (row = 1; row < n-1; row++) sum += a[row][0] + a[row][n-1]; return sum; 9

Question 10 [8 Marks] Write a complete C function named findlongestsequence, the prototype of which is given below, that receives two parameters: a non-empty array list containing unique positive integers (without duplicates), and the size of the array size. The function returns the length of the longest sequence of increasing numbers in the array. For example if the input array list is: 12 14 16 8 9 10 11 21 6 The function findlongestsequence(list, 9) should return 5, since the longest sequence of increasing numbers is 8, 9, 10, 11, 21. Important: You are not allowed to use any additional array within the function. int findlongestsequence(int list[], int size) int start = 0, end = 0, length = 0; for (int i = 1; i < size; i++) if (list[i] > list[i-1]) end++; else if (end - start + 1 > length) length = end - start + 1; start = end = i; if (end - start + 1 > length) length = end - start + 1; return length; 10

Question 11 [8 Marks] Write a complete C function called producerandomnumbers, the prototype of which is given below, that receives an array list and the size of the array length as its parameters. The function fills all the elements in the array with randomly generated integer values in the range between 1 and length * 2 inclusive, with the requirement that all integer values in the array must be unique, i.e., there are no duplicates. void producerandomnumbers(int list[], int length) int *status = (int *) malloc(sizeof(int) * (length * 2 + 1)); int i; for (i = 1; i <= length * 2; i ++) status[i] = 0; for (i = 0; i < length; i ++) do list[i] = rand() % (length * 2) + 1; while (status[list[i]]); status[list[i]] = 1; free(status); 11

Question 12 [8 Marks] Write a C function called checkplagiarism, the prototype of which is given below, that returns true if two suspected input codes (code1) and (code2) have high similarity. High similarity is defined as matching exactly, but ignoring any spaces ( ) or return characters ( \n ). For example, the function checkplagiarism() returns true when comparing the example strings c1 and c2 below. Do not use recursion in your solution. You can assume that code1 and code2 are null-terminated strings. Hint: Your code should return false as soon as it finds evidence of a mis-match. #include <stdio.h> #include <stdbool.h> bool checkplagiarism(char *code1, char *code2); int main(void) char c1[] = "int main(void)\n int x = 10;\n int z = x + 5;\n return 0;\n\n"; char c2[] = "int main(void)\n int x=10;\n int z=x+5;\n\n\n return 0;\n\n"; printf("%d\n", checkplagiarism(c1, c2)); bool checkplagiarism(char *code1, char *code2) while (*code1 && *code2) if (*code1 == *code2) code1++; code2++; else if (*code1 == \n *code1 == ) code1++; else if (*code2 == \n *code2 == ) code2++; else return false; if (!*code1 &&!*code2) return true; 12

else return false; 13

Question 13 [8 Marks] Given an unsorted array of size length, insertion sort builds the sorted array by visiting each element from index 1 forward to the end of the array (index length - 1). A variation of insertion sort visits each element in the unsorted array from index length - 2 backward to the start of the array. At any given iteration, if the index of the visited element is i then one has to find the correct position of the element in the already sorted list starting from index i + 1 to length - 1. For example, if the input unsorted array is: 9 16 7 2 5 4 3 then the content of the array after each iteration is shown below: 9 16 7 2 5 3 4 9 16 7 2 3 4 5 9 16 7 2 3 4 5 9 16 2 3 4 5 7 9 2 3 4 5 7 16 2 3 4 5 7 9 16 The sorted sublist after each iteration is shown in bold font. Note that the array is to be sorted in ascending order. Write a C function called newinsertionsort, the prototype of which is given below, that receives a non-empty array list containing positive integers and the size of the array length as its parameters and sorts the elements in the array in ascending order using the above technique. Important: You are not allowed to use any additional arrays within the function. Solutions using additional arrays within the function will receive 0 marks. void newinsertionsort(int list[], int length) int top, i; int item; for (top = length - 2; top >= 0; top--) item = list[top]; i = top; while (i < length - 1 && item > list[i+1]) list[i] = list[i+1]; i++; 14

list[i] = item; 15

Question 14 [8 Marks] The following sentences are examples of palindromic sentences because, when spaces and punctuation are removed, the remaining words are palindromes (palindromes read the same in both directions). never odd or even. a nut for a jar of tuna. no lemon, no melon. Write a recursive C function called ispalindromicsentence that determines if a string (passed in as a parameter) is a palindromic sentence. You may assume the string contains only lower-case characters, and that the only punctuation marks used are. and,. You may write a helper function (and have that helper function be recursive rather than the ispalindromicsentence function). You may only use the strlen function from the string.h library, and may not use any other functions from string.h. Solutions that do not use recursion will receive 0 marks. You are not allowed to allocate any new arrays in your solution. bool ispalindromicsentencehelper(char *start, char *end) while ((start <= end) && (*start ==. *start ==, *start == )) start++; while ((end >= start) && (*end ==. *end ==, *end == )) end--; if (end <= start) return true; if (*end!= *start) return false; return ispalindromicsentencehelper(start + 1, end - 1); bool ispalindromicsentence(char *str) return ispalindromicsentencehelper(str, str + strlen(str) - 1); 16

Question 15 [8 Marks] The Node structure in a linked list has been defined as follows: typedef struct node int info; struct node *link; Node; Write a C function called deletefirstlast, the prototype of which is given below, that deletes the first and the last node in a linked list pointed to by head, and returns a pointer to the head of the linked list after deletion. If the linked list is empty, the function returns NULL. If the linked list has only one node, the function deletes that node and returns NULL. Node * deletefirstlast(node *head) if (head == NULL) return NULL; if (head->link == NULL) free(head); return NULL; if (head->link->link == NULL) free(head->link); free(head); return NULL; // Initialize current and newhead to point to the 2nd node Node *current = head->link; Node *newhead = head->link; // Delete the first node free(head); while (current->link->link!= NULL) current = current->link; free(current->link); current->link = NULL; return newhead; 17

Question 16 [8 Marks] Write a recursive C function called gettwohighest, the prototype of which is given below, that finds the two largest elements in an array (passed in as a parameter) and stores them in two integer variables, pointers to which are passed in as parameters. An example illustrating the use of this function is given below. In this example, the output 13 12 is printed on a line (since these are the largest two elements in the array list1), followed by the output 7 7 (since these are the largest two elements in the array list2). You may assume the function is called for an array with at least 2 elements. Solutions that do not use recursion will receive 0 marks. You are allowed to use a helper function if you wish. int main(void) int a, b; int list1[] = 5, 7, 13, 10, 12; gettwohighest(list1, 5, &a, &b); printf("%d %d\n", a, b); int list2[] = 7, 6, 7, 2; gettwohighest(list2, 4, &a, &b); printf("%d %d\n", a, b); return 0; void gettwohighest(int list[], int n, int *first, int *second) if (n > 2) gettwohighest(list + 1, n - 1, first, second); if (list[0] >= *first) *second = *first; *first = list[0]; else if (list[0] >= *second) *second = list[0]; else // n == 2 if (list[0] > list[1]) *first = list[0]; 18

*second = list[1]; else *first = list[1]; *second = list[0]; 19

This page has been left blank intentionally. You may use it to answer Question 16. 20

Question 17 [8 Marks] The Node structure in a linked list has been defined as follows: typedef struct node int info; struct node *link; Node; Write a C function called splitswap, the prototype of which is given below, that finds the node with info equal to the parameter item. This node is used for splitting the linked list into a left and a right list. The left list contains all the nodes to the left of the splitting node, and the right list contains all the nodes to the right of the splitting node. The splitting node itself is included in the left list. The splitswap function returns the modified linked list by swapping the left and right lists. If the input linked list is 1->9->13->4->5->6->17->8->NULL then after calling the splitswap function with item = 5, the modified linked list returned by the function is 6->17->8->1->9->13->4->5->NULL Assume all info values are unique in the linked list. Also, if the item is not found, then the original linked list is simply returned without any modification. 21

Node * splitswap(node *head, int item) Node *current = head, *split; // Handle the empty list if (!current) return head; // Find the item for splitting the list while (current->link && current->info!= item) current = current->link; if (current->link) //Save the address of splitting node split = current; //Find the last element in the list while (current->link) current = current->link; //Swap the list at position split current->link = head; head = split->link; split->link = NULL; return head; 22

This page has been left blank intentionally. You may use it for answers to any questions. 23