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

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

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

CSE 142 Wi01 Midterm 2 page 1 of 6

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

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

CSC 270 Survey of Programming Languages. What is a Pointer?

Q1: Multiple choice / 20 Q2: C input/output; operators / 40 Q3: Conditional statements / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

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

BSM540 Basics of C Language

COS 126 General Computer Science Fall Midterm 1

University of Maryland College Park Dept of Computer Science CMSC106 Fall 2013 Midterm I Key

CSE 303 Midterm Exam

Submit your answers to these questions to the Curator under Quizzes as HW08 by the posted due date and time. No late submissions will be accepted.

C0MP1911 Final Exam 1337 Computing 1

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

Fundamental of Programming (C)

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

"'"' .;, b) F or ( c = l ; c <= 10,"' c++ ) X { JJ. ~. Computer Programming I COME Midterm Examination. Fall November 2016

16.216: ECE Application Programming Fall 2011

COS 126 MIDTERM 1, FALL

CS 108 Computing Fundamentals. October/November Array Bootcamp

Programming Language B

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

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

Computer Science & Engineering 150A Problem Solving Using Computers

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Introduction to arrays

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

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

EECE.2160: ECE Application Programming Spring 2018

CS 101, Spring 2017 Apr 11th Exam 3

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

COS 126 General Computer Science Spring Written Exam 1

Faculty of Applied Science and Engineering, University of Toronto CSC181: Introduction to Computer Programming, Fall 2000

CSCI 2132 Software Development. Lecture 18: Functions

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Q1: C input/output; operators / 46 Q2: Conditional statements / 34 Q3: While and do-while loops / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

EECE.2160: ECE Application Programming Spring 2016 Exam 1 Solution

16.216: ECE Application Programming Fall 2013

Arrays and Pointers (part 2) Be extra careful with pointers!

Programming Language A

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Subject: Fundamental of Computer Programming 2068

Procedural programming with C

Two Dimensional Array - An array with a multiple indexs.

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

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

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Principles of Programming. Chapter 6: Arrays

MARKS: Q1 /20 /15 /15 /15 / 5 /30 TOTAL: /100

CMSC 313 COMPUTER ORGANIZATION & ASSEMBLY LANGUAGE PROGRAMMING LECTURE 13, SPRING 2013

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Functions in C. Lecture Topics. Lecture materials. Homework. Machine problem. Announcements. ECE 190 Lecture 16 March 9, 2011

SAMPLE MIDTERM SOLUTION

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

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

EECE.2160: ECE Application Programming Fall 2016 Exam 1 Solution

EECE.2160: ECE Application Programming Fall 2017

M105: Introduction to Programming with Java Midterm Examination (MTA) Makeup Spring 2013 / 2014

CS16 Midterm Exam 1 E01, 10S, Phill Conrad, UC Santa Barbara Wednesday, 04/21/2010, 1pm-1:50pm

Structured programming

University of Toronto Mississauga. Flip to the back cover and write down your name and student number.

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

Example: Pointer Basics

MIDTERM EXAM. CS 217 October 28, Name: Precept: Honor Code: Score: Problem Score Max

C Programming Language

19-Nov CSCI 2132 Software Development Lecture 29: Linked Lists. Faculty of Computer Science, Dalhousie University Heap (Free Store)

10/20/2015. Midterm Topic Review. Pointer Basics. C Language III. CMSC 313 Sections 01, 02. Adapted from Richard Chang, CMSC 313 Spring 2013

While single variables have many uses, there are times when we wish to store multiple related values. For these we may wish to use an array.

CSE101-Lec#18. Multidimensional Arrays Application of arrays. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU. LPU CSE101 C Programming

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

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Array Initialization

Arrays and Applications

Computer Science 217 Final Exam May 15, :30pm-4:30pm

CC112 Structured Programming

Content. In this chapter, you will learn:

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

Arrays and Pointers (part 2) Be extra careful with pointers!

Principles of C and Memory Management

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

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

University of Toronto

ECE264 Fall 2013 Exam 1, September 24, 2013

Midterm Exam Nov 8th, COMS W3157 Advanced Programming Columbia University Fall Instructor: Jae Woo Lee.

Integer Representation. Variables. Real Representation. Integer Overflow/Underflow

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

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

Transcription:

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Midterm Examination October 28, 2008 12:20 p.m. 1:50 p.m. Examiners: Jason Anderson, Tom Fairgrieve, Baochun Li 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 or the back of each page to complete your answer. Please indicate clearly on which question(s) you are answering. You must use the C programming language to answer programming questions. The examination has 11 pages, including this one. Circle your lecture section (one mark deduction if you do not correctly indicate your section): L0101 or L0102 or L0103 or L0104 Fairgrieve (M14) Anderson (M9) Fairgrieve (M11) Li (M11) Full Name: Student Number: ECF Login: MARKS 1 2 3 4 5 6 7 8 9 10 11 12 13 Total /3 /3 /3 /3 /4 /4 /8 /8 /8 /14 /14 /14 /14 /100

Question 1 [3 Marks] Given the following C code: int j, sum = 0; for (j = 0; j < 10; j++) sum += j; Rewrite the C statements so that they compute the same quantity but use a while loop instead of a for loop. int j = 0, sum = 0; while (j < 10) sum += j; j ++; Question 2 [3 Marks] Write a single C statement that rounds the value of a double variable averagegrade to four digits after the decimal point, and stores the result in averagegrade. For example, if averagegrade is 3.1415927, after the statement is executed, averagegrade is 3.1416. You may use any of the mathematical functions discussed in the course. averagegrade = rint(averagegrade * 10000) / 10000; Question 3 [3 Marks] Write a single C statement that uses malloc to create an array named list that holds 100 values of type double. double * list = (double *) malloc(sizeof(double) * 100); Question 4 [3 Marks] Show the output produced by the following C program fragment. int i = 1, j = 2, k = 3; printf("%d", (i + 5) % (j + 2) / k); 0 Question 5 [4 Marks] Write a single C statement that determines an even random integer number between 50 and 50 inclusive, and then assigns it to an int variable r that has already been declared. r = rand() % 51 * 2-50; 2

Question 6 [4 Marks] Suppose that a is a one-dimensional int array and p is a pointer to int variable. Assuming that the assignment p = a has just been performed, which of the following expressions are illegal because of mismatched types? Of the remaining expressions, which are true? Expression Answer p == a[0] illegal p == &a[0] true *p == a[0] true p[0] == a[0] true 3

Question 7 [8 Marks] Given the following C program: #include <stdio.h> int main (void) int r, c, n = 3, v[] = 8, 3, 9, 6, 5; int toeplitz[n][n]; for (r = 0; r < n; r++) for (c = 0; c < n; c++) toeplitz[r][c] = v[(n-1) - (r-c)]; printf("%d ", toeplitz[r][c]); printf("\n"); return 0; What is the output from an execution of this C program? 9 6 5 3 9 6 8 3 9 4

Question 8 [8 Marks] Given the following C program: #include <stdio.h> int main (void) int i = 0, j = 21; for (; i < j; i += 2, j -= 3) printf("%d\n", i * j); printf("%d,%d\n", i, j); return 0; What is the output from an execution of this C program? 0 36 60 72 72 10,6 5

Question 9 [8 Marks] If you have a certain number of US dollars and wish to convert them to Canadian dollars, you could use the Canadian dollar to US dollar exchange rate (for example: 1 Canadian dollar = 0.81 US dollar). Write a complete C program that prompts its user for the current Canadian dollar to US dollar exchange rate (e.g. 0.81) and a value in US dollars, and then prints the value in Canadian dollars. #include <stdio.h> int main (void) double exchangerate, cdn, usd; printf("enter the exchange rate (1 CAD =? USD): "); scanf("%lf", &exchangerate); printf("enter the value in US dollars: "); scanf("%lf", &usd); cdn = usd / exchangerate; printf("the value in Canadian dollars is %f.\n", cdn); return 0; 6

Question 10 [14 Marks] Write a complete C function named finditem, the prototype of which is given below, that returns the pointer to the first occurrence of an integer value item (specified as a parameter) in an array list. If the value item is not found in the array, the function returns the null pointer. Both the array and its size are specified as parameters. int * finditem(int * list, int size, int item) int i; for (i = 0; i < size; i ++) if (*(list + i) == item) return list + i; return NULL; 7

Question 11 [14 Marks] Write a complete C function named printtriangle that prints the pattern shown below, with the given number of rows passed in as a parameter. For example, if the number of rows is 7, the function should produce the following output. * *** ***** ******* ********* *********** ************* If the parameter value is not positive, the function should print nothing. void printtriangle(int rows) int i, j; for (i = 1; i <= rows; i ++) for (j = 1; j <= rows - i; j ++) printf(" "); for (j = 1; j <= i * 2-1; j ++) printf("*"); printf("\n"); 8

Question 12 [14 Marks] Write a complete C function named swaplargest that accepts two parameters: (1) an array of integers and (2) the array s length. The function finds the largest element in the array, and swaps the largest element with the element in the last array position. void swaplargest(int list[], int length) int largest = 0; int i, temp; for (i = 1; i < length; i++) if (list[i] > list[largest]) largest = i; temp = list[largest]; list[largest] = list[length-1]; list[length-1] = temp; 9

Question 13 [14 Marks] The value of the mathematical constant e can be expressed using the infinite series: e = 1 + 1/1! + 1/2! + 1/3! +... Write a complete C program that approximates e by approximating the value of 1 + 1/1! + 1/2! + 1/3! +... Rather than adding an infinite number of terms, your program should continue adding terms until the value of a term is less than 0.001. Your program should print the approximation to e and the number of terms used to determine the approximation. The terms in the series are 1, 1/1!, 1/2!, and so on. int main(void) const double TOLERANCE = 0.001; double sum = 0.0, term = 1.0; int n = 0; while (term >= TOLERANCE) sum = sum + term; n = n + 1; term = term / n; // accumulate the term // determine next term printf("the value of e is approximately %f.\n", sum); printf("the number of terms in the sum is %d.\n", n); return 0; 10

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