The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Similar documents
University of Calgary Department of Electrical and Computer Engineering ENCM 335 Instructor: Steve Norman

University of Calgary Department of Electrical and Computer Engineering ENCM 339 Lecture Section 01 Instructor: Steve Norman

ESC101N: Fundamentals of Computing End-sem st semester

Slide Set 8. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

University of Calgary Department of Electrical and Computer Engineering ENCM 339: Programming Fundamentals, Section 01 Instructor: Steve Norman

ECE264 Spring 2014 Exam 2, March 11, 2014

ECE264 Fall 2013 Exam 1, September 24, 2013

ENCM 335 Fall 2018 Lab 6 for the Week of October 22 Complete Instructions

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

Winter 2017 MIDTERM TEST #1 Wednesday, February 8 7:00pm to 8:30pm. Please do not write your U of C ID number on this cover page.

ENCM 339 Fall 2017 Tutorial for Week 8

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

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

CSCI 171 Chapter Outlines

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

Approximately a Final Exam CPSC 206

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

Slide Set 2. for ENCM 335 in Fall Steve Norman, PhD, PEng

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

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

A Crash Course in C. Steven Reeves

Slide Set 3. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

ECE551 Midterm Version 1

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

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

ECE264 Spring 2013 Final Exam, April 30, 2013

Matlab? Chapter 3-4 Matlab and IPT Basics. Working Environment. Matlab Demo. Array. Data Type. MATLAB Desktop:

Structures. Basics of Structures (6.1) EECS l Now struct point is a valid type. l Defining struct variables: struct point { int x; int y; };

ECE264 Fall 2013 Exam 2, October 24, 2013

ECE264 Fall 2013 Exam 3, November 20, 2013

Slide Set 6. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

!"#$% &'($) *+!$ 0!'" 0+'&"$.&0-2$ 10.+3&2),&/3+, %&&/3+, C,-"!.&/+"*0.&('1 :2 %*10% *%7)/ 30'&. 0% /4%./

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

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

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

Winter 2012 MID-SESSION TEST Tuesday, March 6 6:30pm to 8:15pm. Please do not write your U of C ID number on this cover page.

ECE551 Midterm Version 2

Laboratory 2: Programming Basics and Variables. Lecture notes: 1. A quick review of hello_comment.c 2. Some useful information

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

This exam is to be taken by yourself with closed books, closed notes, no calculators.

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

ECE 15B COMPUTER ORGANIZATION

Dynamic Data Structures. CSCI 112: Programming in C

Slide Set 9. for ENCM 335 in Fall Steve Norman, PhD, PEng

Exercise Session 2 Simon Gerber

BIL 104E Introduction to Scientific and Engineering Computing. Lecture 12

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

Fundamental Data Types. CSE 130: Introduction to Programming in C Stony Brook University

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Approximately a Test II CPSC 206

Winter 2003 MID-SESSION TEST Monday, March 10 6:30 to 8:00pm

Signature: ECE 551 Midterm Exam

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

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

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

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

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

High Performance Programming Programming in C part 1

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

ECE551 Midterm. There are 7 questions, with the point values as shown below. You have 75 minutes with a total of 75 points. Pace yourself accordingly.

Writing an ANSI C Program Getting Ready to Program A First Program Variables, Expressions, and Assignments Initialization The Use of #define and

Slide Set 4. for ENCM 339 Fall 2017 Section 01. Steve Norman, PhD, PEng

Subject: PROBLEM SOLVING THROUGH C Time: 3 Hours Max. Marks: 100

CS201- Introduction to Programming Latest Solved Mcqs from Midterm Papers May 07,2011. MIDTERM EXAMINATION Spring 2010

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

CS24 Week 2 Lecture 1

ENCM 339 Fall 2017 Lecture Section 01 Lab 3 for the Week of October 2

Slide Set 4. for ENCM 335 in Fall Steve Norman, PhD, PEng

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

C Fundamentals & Formatted Input/Output. adopted from KNK C Programming : A Modern Approach

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

CSE 5A Final Fall 2006

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

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

Review: C Strings. A string in C is just an array of characters. Lecture #4 C Strings, Arrays, & Malloc

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

JTSK Programming in C II C-Lab II. Lecture 3 & 4

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

Procedural programming with C

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

Advanced Pointer Topics

First of all, it is a variable, just like other variables you studied

.. Cal Poly CPE 101: Fundamentals of Computer Science I Alexander Dekhtyar..

Mid-term Exam. Fall Semester 2017 KAIST EE209 Programming Structures for Electrical Engineering. Name: Student ID:

CSE 333 Midterm Exam 5/10/13

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

C Syntax Arrays and Loops Math Strings Structures Pointers File I/O. Final Review CS Prof. Jonathan Ventura. Prof. Jonathan Ventura Final Review

BSM540 Basics of C Language

C Concepts - I/O. Lecture 19 COP 3014 Fall November 29, 2017

CS61C Machine Structures. Lecture 4 C Structs & Memory Management. 9/5/2007 John Wawrzynek. www-inst.eecs.berkeley.edu/~cs61c/

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

#1 #2 with corrections Monday, March 12 7:00pm to 8:30pm. Please do not write your U of C ID number on this cover page.

CS 326 Operating Systems C Programming. Greg Benson Department of Computer Science University of San Francisco

Introduction to Computers II Lecture 4. Dr Ali Ziya Alkar Dr Mehmet Demirer

CSE 333 Midterm Exam Sample Solution 5/10/13

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

Binghamton University. CS-211 Fall Syntax. What the Compiler needs to understand your program

Intermediate Programming, Spring 2017*

Lesson 5: Functions and Libraries. EE3490E: Programming S1 2018/2019 Dr. Đào Trung Kiên Hanoi Univ. of Science and Technology

Transcription:

The University of Calgary ENCM 339 Programming Fundamentals Fall 2016 Instructors: S. Norman, and M. Moussavi Wednesday, November 2 7:00 to 9:00 PM The First Letter of your Last Name:! Please Print your Name - First Name and Last Name: Please check the box for your lecture section Lecture Section L01 (MWF 1:00 to 1:50 PM) Dr. Norman:! Lecture Section L02- (MWF 12:00 to 12:50 PM) - Dr. Moussavi:! Your Signature: Please also write your name and your U of C Student ID at the bottom of the last page Instructions To prevent distracting your fellow students, you may not leave the exam room during the last ten minutes of the exam. No electronic calculators or computers may be used during the test. This is a closed-book test. You must not refer to books or notes during the test. Write legibly! What cannot be read will not be marked. If you remove the staple from the paper, write your name at the top of every loose page. Attempt all questions. Get the easiest marks first! If you find a particular question difficult, return to it after completing easier questions. If you write any rough work on the question paper, cross it out with a large X and write, Rough work beside it. Rough work will not be marked. Follow the directions for each problem carefully. 1

SECTION 1 - Multiple Choice Questions (1 mark each) Total 5 marks: Notes: To answer the questions in this section you can make the following assumptions: o All necessary header files are included o Size of int data type is 4 bytes, and size of double data type and pointers are 8 bytes. 1. What is the output of the following code fragment? char* s1 = "apple"; char s2[]= "Victor"; int* p = malloc(5 * sizeof(int)); printf("%d %d %d %d", (int) sizeof(s1), (int) sizeof(s2), (int) strlen(s2), (int) sizeof(p)); a. 5 6 5 8 b. 8 7 6 8 c. 8 8 4 8 d. 6 6 5 8 e. 6 6 6 4 f. None of the above. 2. What is the output of the following code fragment? int a[] = {44, 88, 66, 22, 10, 12, 5; int* p1 = a; int* p2 = &a[6]; printf("%d", (int) (p1 (p2 + 1)); a. 6 b. 5 c. -7 d. 7 e. None of the above. 3. What if anything is wrong with the following code fragment? char code[6] = "Apple"; const char* csp = code + 1; csp++; *code = csp[0]; *csp = *code; a. There is a compilation error on the second-last line of the code fragment. b. There is a compilation error on the last line of the code fragment. c. The fragment will cause the containing program to crash when it runs. d. All of the above are correct e. None of the above is correct. 4. Consider the following code fragment and select one of the following answers. Assume that the user enters the following text on the keyboard, then presses the Enter key: 52.75 12.625x double d = 0.125; int i = 42, ns = 99; printf("please enter two numbers: "); ns = scanf("%d %lf", &i, &d); printf("%d %d %f\n", ns, i, d); a. The program output will be: 0 42 0.125000 b. The program output will be: 1 52 0.125000 c. The program output will be: 2 52 0.750000 d. The program output will be: 2 52 12.625000 e. None of the above. 5. What is the output of the following code fragment? char course[] = "ENCM339F2016"; char* sp = course + 2 ; while(*sp!= '1'){ printf("%c", *sp); sp = sp + 2 ; a. ENM339F20 b. CM33F20 c. C3921 d. EC3921 e. None of the above. 2

SECTION 2 Short Answer Questions Total 12 marks: 1. (4 marks) The following partial C program should open a text file called input.txt, with several int values separated by whitespace, reads one int value at a time, and copy each value into a binary file, until the end of the input file is reached. You may assume that the data in the text file are all valid integers and that both files will open successfully without errors. Your job is to fill the blank spaces with correct C expressions. int number, nscan; FILE *fp1 = fopen("input.txt", "r"); FILE *fp2 = fopen("output.dat", "wb"); while (1) { nscan = fscanf(, "%d", ); if (nscan!=. ) break; fwrite ((void *)., sizeof(. ), 1,..); fclose(... ); fclose(... ); 2. (2 marks) Assume that the macros FOO, BAR and QUUX are not defined in <stdio.h>. What is the output of the following program? #define FOO 47 #define QUUX #if defined(quux) printf("99 QUUX\n"); #ifndef FOO printf("foo\n"); #endif #if 1 printf("bar\n"); #endif #endif Write your answer in the following space: 3. (4 marks) Assume that the call to malloc succeeds. Draw a memory diagram for point one in the space beside the code listing. 4. #include <stdlib.h> int glob[3] = { 31, 57 ; int loc[3]; int *dyn = malloc(3 * sizeof(int)); loc[0] = glob[0]; dyn[1] = glob[1]; // point one 5. (2 marks) Write a #define macro with three arguments, called IN_RANGE, that produces a value of 1 if the value of the first argument is greater than or equal to the second argument and less than or equal to the third argument, and otherwise produces a value of 0. Write your macro in the following space: 3

SECTION 3 Functions 26 marks Part a (8 marks): Write a function definition based on the given function interface comment. int remove_outliers(int* x, int n, int min, int max); // REQUIRES: n > 0. Elements x[0]... x[n-1] exist. // PROMISES: Elements less than min or greater than max are removed from the array. // Let rv be the return value. Then rv == number of elements that are >= min // and <= max, and x[0]... x[rv-1] contain those elements. // EXAMPLE: n is 5, original values of x[0] to x[4] are 5, -1, 4, 11, 10, min is 0, // max is 10. Then rv will be 3, x[0] to x[2] get 5, 4, 10, and it does not matter // what is left in x[3] and x[4]. Part b (6 marks): In mathematics, a Fibonacci integer sequence may have any two integers for its first two values. After that, each value is the sum of the previous two values. The Fibonacci numbers... 0, 1, 1, 2, 3, 5, 8, 13, 21, are an example of such a sequence. Write a function definition for is_fibonacci. int is_fibonacci(const int* x, int n); // REQUIRES: n >= 3. Elements x[0]... [n-1] exist. // PROMISES: Return value is 1 if x[0]... x[n-1] are the first n numbers of a // Fibonacci integer sequence. Otherwise the return value is 0. 4

Part c (6 marks): In the following space write the definition of a function called count_lower_cases that receives one argument of type const char*, called str, which must points to the beginning of a valid C string. The function should return the number of the lower-case characters in the string. Examples: Function returns 5 for these example arguments "AaBbCbBbAbYYYYYYY" "abbbbaccca" "ABAbbbbb" "AaabddA" Function returns 0 for these example arguments "ABCA" "" "!" "123445" "123ABC&%$" Important Note: You are NOT allowed to use any of the C library functions in your solution. You may assume that the character set is ASCII, in which 'a' == 97, 'b' == 98, and so on. Part d (6 marks) Write a function definition to match the following interface. void trim_trailing_spaces(char* str); // REQUIRES: str points to the beginning of a valid C-string. // PROMISES: If the string has trailing spaces, the string is shortened to eliminate those // spaces. Otherwise the string is not changed. // EXAMPLE: Assume each little box,, represents a space in the following string: // char[ ] foo = "AB% & 98ax "; // after the call trim_trailing_spaces(foo), the string should match "AB% & 98ax" // Notice: Only the trailing spaces are removed. 5

SECTION 4 Arrays and Pointers (10 marks): Consider the definition of the following program and draw memory diagrams for point one and two in the given boxes to the right of the code: void saturn(const char *str, char arr[]); void mercury(int x[], int* y); Point 1 int main(void) { char code[] = "APEX"; const char* exam = "FALL16"; int array[] = {4, 17, 300, 400; saturn(&exam[1], code + 2); mercury(array, array + 2); void saturn(const char *str, char arr[]) { arr[0] = *(str-1); // point one void mercury(int x[], int* y) { int z = *x; *x = y[0]; y[0] = z; Point two // point 2 6

SECTION 5 Structures Total 10 marks Consider the following declarations: typedef struct Point { double x, y; Point; typedef struct Rectangle { Point left_top_corner; double width, height; Rectangle; Part 1 (4 marks) - In the following space, draw an AR diagram for point one in function funct: void funct(rectangle rect, Rectangle* prect) { prect->width = rect.width; // point one Rectangle a = {{20, 30, 15, 66, b = {{10, 20; double y; funct(a, &b); y = diagonal(&a); printf( "diagonal distance between the top-left and bottom-right corners is: %f\n ", y); Part 2 (6 marks) Consider the following function prototype and its interface comment, and write the definition of function diagonal. (Hint: Assume that #include <math.h> has been done. double sqrt(double x); is a function prototype in that header file.) double diagonal(const Rectangle *rect); // REQUIRES: rect points to an instance of Rectangle. // PROMISES: Return value is the diagonal distance between the left-top corner of the // rectangle and its right-bottom corner. Your Name and U of C Student ID Number: Don't write anything in this box this box is for marking Section I Section II Section III Section IV Section V Total /5 /12 /26 /10 10 /63 7