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

BSM540 Basics of C Language

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

16.216: ECE Application Programming Fall 2011

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 5. Playing with Data Modifiers and Math Functions Getting Controls

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

CS 108 Computing Fundamentals. October/November Array Bootcamp

ESC 101N: Fundmentals of Computing ( IInd Semester) Mid Sem II Examination PM, Monday 7th March, 2011

Fundamental of Programming (C)

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

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

EECE.2160: ECE Application Programming Spring 2018

Slides adopted from T. Ferguson Spring 2016

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

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

EECE.2160: ECE Application Programming Fall 2017

COS 126 General Computer Science Spring Written Exam 1

C Program Reviews 2-12

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

LESSON 5 FUNDAMENTAL DATA TYPES. char short int long unsigned char unsigned short unsigned unsigned long

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

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

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

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

ECE 15 Fall 15 Midterm Solutions

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

Computer Programming: Skills & Concepts (CP) arithmetic, if and booleans (cont)

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

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

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

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

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

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

16.216: ECE Application Programming Fall 2013

1. The keyword main in C language is used for

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Fundamentals of Programming & Procedural Programming

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

Programming Language A

Lectures 4 and 5 (Julian) Computer Programming: Skills & Concepts (INF-1-CP1) double; float; quadratic equations. Practical 1.

Structured programming. Exercises 3

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

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

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

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

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

C introduction: part 1

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

Lab Session # 1 Introduction to C Language. ALQUDS University Department of Computer Engineering

#include <stdio.h> int main() { char s[] = Hsjodi, *p; for (p = s + 5; p >= s; p--) --*p; puts(s); return 0;

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

Procedural programming with C

Arrays. Systems Programming Concepts

Fundamental of Programming (C)

Structured programming

2. Numbers In, Numbers Out

C Exercises. Ben Vandiver February 28, 2003

Lecture 10: Recursive Functions. Computer System and programming in C 1

16.216: ECE Application Programming Spring 2015 Exam 2 Solution

APS105. Modularity. C pre-defined functions 11/5/2013. Functions. Functions (and Pointers) main. Modularity. Math functions. Benefits of modularity:

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Outline Arrays Examples of array usage Passing arrays to functions 2D arrays Strings Searching arrays Next Time. C Arrays.

CpSc 1011 Lab 4 Formatting and Flow Control Windchill Temps

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Chapter 3: Arrays and More C Functionality

Yacoub Sabatin Muntaser Abulafi Omar Qaraeen

CS16 Exam #1 7/17/ Minutes 100 Points total

Fundamental of Programming (C)

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

CSCI 2132 Software Development. Lecture 20: Program Organization

Assoc. Prof. Dr. Tansu FİLİK

Midterm Exam 2 Solutions C Programming Dr. Beeson, Spring 2009

Module 6: Array in C

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

CSE 142 Wi01 Midterm 2 page 1 of 6

LAB 7 FUNCTION PART 2

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

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

CSCI 2132 Software Development. Lecture 19: Generating Permutations

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

Basic C Programming (2) Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

Name Roll No. Section

ESC101N: Fundamentals of Computing End-sem st semester

Slide Set 1. for ENCM 339 Fall Steve Norman, PhD, PEng. Electrical & Computer Engineering Schulich School of Engineering University of Calgary

Programming I Key 1 COMSC 1613 Autumn 1999

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

2. Numbers In, Numbers Out

CSE123 LECTURE 3-1. Program Design and Control Structures Repetitions (Loops) 1-1

Fundamentals of Programming

CS13002 Programming and Data Structures, Spring 2005

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

Transcription:

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING APS 105 Computer Fundamentals Midterm Examination October 20, 2011 6:15 p.m. 8:00 p.m. (105 minutes) Examiners: J. Anderson, T. Fairgrieve, B. Li, G. Steffan, A. Veneris 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 write #include directives in your solutions. You may use any math function that you have learned, as necessary. The examination has 14 pages, including this one. Circle your lecture section (one mark deduction if you do not correctly indicate your section): L0101 L0102 L0103 L0104 L0105 L0106 Li Fairgrieve Anderson Steffan Fairgrieve Veneris Monday 2 PM Monday 9 AM Monday 11 AM Monday 11 AM Monday 4 PM Monday 4 PM Full Name: Student Number: UTORid: MARKS 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Total /3 /3 /3 /4 /4 /3 /8 /8 /4 /12 /12 /12 /12 /12 /100

Question 1 [3 Marks] Write a single C statement that declares a bool variable named mybool and sets its value to true if and only if the value stored in an integer variable named myint is an even positive number. You may assume that the variable myint has been declared and initialized already to some value. bool mybool = ( (myint % 2 == 0) && (myint > 0)); Question 2 [3 Marks] The golden ratio conjugate Φ is defined as Φ = 5 1 2. This mathematical constant is used in different fields of computer engineering such as numerical analysis, combinatorial algorithms, etc. Write a single C statement that declares a constant variable named PHI and sets PHI to be equal to the value of Φ given above. You may make use of functions defined in math.h. const double PHI = (sqrt(5.0)-1)/2; Question 3 [3 Marks] Suppose we have the following declaration and initialization of variables: double a = 23.0, b = 20.0, c = 15.0, d = 8.0, e = 40.0; What is the value for the following expression? (int) (a + b / (c - 5.0)) / ((d + 7.0) / (e - 37.0) / 2.0) The final value is 10. 2

Question 4 [4 Marks] Let i be an int variable with value 5, j be an int variable with value 0, and k be a bool variable set to false. State whether the following expressions evaluate to true or false. Expression Answer ( (i >= 3) && (j == (int) 0.0) ) TRUE ( ((i!= 3) (j!= 4)) ==!((i == 3) && (j == 4)) ) TRUE!( (i-4) <= 0 ) TRUE ( k && (sin(42.0) == 0.1372) ) FALSE Question 5 [4 Marks] Write a single C statement that declares an int type variable named lotto, and initializes it to be an odd random integer between 601 and 649 (inclusive). int lotto = 601 + ( rand() % 25 * 2 ); Question 6 [3 Marks] What will be printed by the following C program? int main (void) int i; for(i = 0; i < 5; i++) printf("%c", a +i); return 0; abcde 3

Question 7 [8 Marks] Given the following C program: #include <stdio.h> int main (void) int a = 1; int b = 2; while (a <= 45) printf("%d\n", a); a += b; b++; return 0; What is the output from an execution of this C program? 1 3 6 10 15 21 28 36 45 4

Question 8 [8 Marks] Given the following C program: #include <stdio.h> int main (void) int row, col; for (row = 1; row <= 5; row++) for (col = 1; col <= 9; col++) if ((col > row) && (col <= (9-row))) printf(" "); else printf("*"); printf("\n"); return 0; What is the output from an execution of this C program? * * ** ** *** *** **** **** ********* 5

Question 9 [4 Marks] For each of the following parts, determine the number of times that the word Chicken will be printed. Place your answer in the appropriate box. (a) for (int i=0; i < 10; i++) for (int j=0; j < 10; j++) if (i < j) printf("chicken\n"); Will print Chicken 45 times. (b) for (int i=0; i >= 0; i++) for ( ; i < 100; ) while (i!= 0) printf("chicken\n"); Will print Chicken 0 times. 6

Question 10 [12 Marks] Write a program that prompts the user to enter two dates and then indicates which date comes earlier in the calendar. Assume that the user enters valid dates. Here is a sample output from an execution of the program: Enter first date (day/month): 20/10<enter> Enter second date (day/month): 22/9<enter> 22/9 is earlier than 20/10 Here is another sample output from an execution of the program: Enter first date (day/month): 20/10<enter> Enter second date (day/month): 20/10<enter> The two dates are the same. #include <stdio.h> int main (void) int day1, month1; int day2, month2; int cmp = 0; printf("enter first date (day/month): "); scanf("%d/%d", &day1, &month1); printf("enter second date (day/month): "); scanf("%d/%d", &day2, &month2); if ( month1 < month2 ) cmp = -1; else if ( month2 < month1 ) cmp = +1; else if ( day1 < day2 ) cmp = -1; else if ( day2 < day1 ) cmp = +1; else cmp = 0; if ( cmp == -1 ) 7

printf("%d/%d is earlier than %d/%d\n", day1, month1, day2, month2); else if ( cmp == +1 ) printf("%d/%d is earlier than %d/%d\n", day2, month2, day1, month1); else printf("the two dates are the same.\n"); return 0; 8

Question 11 [12 Marks] Write a program that finds the largest positive number in a list of numbers entered by the user. The program must prompt the user to enter numbers one by one. When the user enters 0 or a negative number, the program must stop prompting the user for numbers and display the largest positive number that was entered. Note that the length of the list of positive numbers could be arbitrarily large. You may assume that at least one positive number will be input. Here is a sample output from an execution of the program: Enter a number: 60<enter> Enter a number: 4.89<enter> Enter a number: 100.62<enter> Enter a number: 75.2295<enter> Enter a number: 0<enter> The largest positive number entered was: 100.620000 Notice that the numbers aren t necessarily integers. #include <stdio.h> int main (void) double d, largest; printf("enter a number: "); scanf("%lf", &d); largest = d; while (d > 0) if (d > largest) largest = d; printf("enter a number: "); scanf("%lf", &d); printf("the largest positive number entered was: %f\n", largest); return 0; 9

Question 12 [12 Marks] Write a function named pythagoreantriples that accepts a positive integer x as its parameter, and prints one triple (x, y, z) such that: (a) x 2 + y 2 = z 2 ; (b) y > 0 and y <= 100; (c) y < z; (d) x, y, z are all positive integers. If no triple exists that satisfies all four conditions, then the function prints "No solution exists.". Assume that the argument value for x is a positive integer. For example, if we compile and run the pythagoreantriples function with the following main() program: int main (void) pythagoreantriples(3); pythagoreantriples(12); pythagoreantriples(20); pythagoreantriples(49); return 0; the following will be printed: (3, 4, 5) (12, 5, 13) (20, 15, 25) No solution exists. Place your solution to this question on the next page. 10

Place your solution to Question 12 on this page. void pythagoreantriples (int x) int y = 1; bool triple = false; while (y <= 100 &&!triple) int z = sqrt(x * x + y * y); if (x * x + y * y == z * z) printf("(%d, %d, %d)\n", x, y, z); triple = true; y++; if (!triple) printf("no solution exists.\n"); 11

Question 13 [12 Marks] In the indicated position in the program below, define a function called rotate that rotates the values pointed to by the three pointer-to-integer parameters in such a way that after executing the function, the first has the original value of the third, the second has the original value of the first, and the third has the original value of the second. Example 1: Please input three integers: 3 4 5 After rotation, the list of integers is: 5 3 4 Example 2: Please input three integers: 9 3 7 After rotation, the list of integers is: 7 9 3 Program: #include <stdio.h> // define rotate HERE: void rotate (int *a, int *b, int *c) int tmp = *c; *c = *b; *b = *a; *a = tmp; int main (void) int x, y, z; printf("please input three integers: "); scanf("%d %d %d",&x,&y,&z); rotate(&x,&y,&z); printf("after rotation, the list of integers is: %d %d %d\n",x,y,z); return 0; 12

Question 14 [12 Marks] Amicable numbers are two different positive integers such that the sum of the proper exact divisors of each is equal to the other number. A proper exact divisor is a positive integer exact divisor of a number, other than the number itself. For example, the proper exact divisors of 6 are 1, 2, and 3. 220 and 284 are amicable numbers. The reason is as follows: the proper exact divisors of 220 are 1, 2, 4, 5, 10, 11, 20, 22, 44, 55, and 110, the sum of which is 284. The proper exact divisors of 284 are 1, 2, 4, 71, and 142, the sum of which is 220. Write a complete C function called areamicable, the prototype of which is given below, that accepts two numbers of type int as parameters, named m and n. The function must return the Boolean type bool. The function should return true if m and n are amicable numbers, and should return false otherwise. You may assume that the arguments passed to the function are indeed positive integers. bool areamicable (int m, int n) int sum1 = 0; int sum2 = 0; int i,j; if (m == n) return false; for (i = 1; i < m; i++) if (m % i == 0) sum1 += i; for (j = 1; j < n; j++) if (n % j == 0) sum2 += j; if ((sum1 == n) && (sum2 == m)) return true; return false; 13

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