Student Number: Instructor: Reid Section: L5101 (6:10-7:00pm)

Similar documents
Student Number: Instructor: Reid Section: L0101 (10:10-11:00am)

Student Number: Instructor: Reid Section: L0201 (12:10-1:00pm)

Question 1. Part (a) CSC 209H1 S 2017

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 7

[6 marks] All parts of this question assume the following C statement. Parts (b) through (e) assume a variable called ptrs.

CSC 209H1 S 2012 Midterm Test Duration 50 minutes Aids allowed: none. Student Number: # 1: / 6

CSC209H1S Day Midterm Solutions Winter 2010

Getting Started. Project 1

Pointers, Arrays, and Strings. CS449 Spring 2016

Jacobs University Bremen February 7 th, 2017 Dr. Kinga Lipskoch. Practice Sheet. First Name:... Last Name:... Matriculation Number:...

File IO and command line input CSE 2451

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

25.2 Opening and Closing a File

COMP 2355 Introduction to Systems Programming

This code has a bug that allows a hacker to take control of its execution and run evilfunc().

structs as arguments

Lecture 05 Pointers ctd..

Chapter 5, Standard I/O. Not UNIX... C standard (library) Why? UNIX programmed in C stdio is very UNIX based

Mode Meaning r Opens the file for reading. If the file doesn't exist, fopen() returns NULL.

C-Refresher: Session 10 Disk IO

Input/Output and the Operating Systems

Standard File Pointers

File (1A) Young Won Lim 11/25/16

ECE551 Midterm Version 1

Lab # 4. Files & Queues in C

C Input/Output. Before we discuss I/O in C, let's review how C++ I/O works. int i; double x;

System Programming. Standard Input/Output Library (Cont d)

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.

ECE551 Midterm Version 2

ECE551 Midterm Version 1

ECE 551D Spring 2018 Midterm Exam

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

File Handling. Reference:

CSE 333 SECTION 3. POSIX I/O Functions

Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

UNIVERSITY OF TORONTO Faculty of Arts and Science. St. George Campus DECEMBER 2017 EXAMINATIONS. CSC 209H1F Liu, Reid 3 hours. No Examination Aids

Arrays and Strings. CS449 Fall 2017

Midterm Review. What makes a file executable? Dr. Jack Lange 2/10/16. In UNIX: Everything is a file

Standard I/O in C, Computer System and programming in C

Stream Model of I/O. Basic I/O in C

CSC209F Midterm (L0101) Fall 1999 University of Toronto Department of Computer Science

File and Console I/O. CS449 Fall 2017

C mini reference. 5 Binary numbers 12

File I/O. Preprocessor Macros

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

File and Console I/O. CS449 Spring 2016

csc/cpe 357 C Quiz Archive

Characters and Strings

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

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

CMPE-013/L. File I/O. File Processing. Gabriel Hugh Elkaim Winter File Processing. Files and Streams. Outline.

CSI 402 Systems Programming LECTURE 4 FILES AND FILE OPERATIONS

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

File Access. FILE * fopen(const char *name, const char * mode);

CSCI 2132 Software Development. Lecture 29: Dynamic Memory Allocation

Princeton University Computer Science 217: Introduction to Programming Systems. Modularity design principles

CSC209H1F Test 2 Solutions Fall 2009

CS240: Programming in C

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

Content. Input Output Devices File access Function of File I/O Redirection Command-line arguments

Characters, Character Strings, and string-manipulation functions in C

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

ECE 551D Spring 2018 Midterm Exam

15213 Recitation Section C

Programming in C. 4. Misc. Library Features, Gotchas, Hints and Tips. Dr. Neel Krishnaswami University of Cambridge

File I/O. Arash Rafiey. November 7, 2017

Operating System Labs. Yuanbin Wu

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

System Software Experiment 1 Lecture 7

PLEASE HAND IN UNIVERSITY OF TORONTO Faculty of Arts and Science

CSci 4061 Introduction to Operating Systems. Input/Output: High-level

Procedural Programming

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

PRINCIPLES OF OPERATING SYSTEMS

DAY 3. CS3600, Northeastern University. Alan Mislove

PROGRAMMAZIONE I A.A. 2017/2018

CS201 Lecture 2 GDB, The C Library

Engineering program development 7. Edited by Péter Vass

HIGH LEVEL FILE PROCESSING

Computer Programming Unit v

Computer Programming: Skills & Concepts (CP) Strings

CSE 333 SECTION 3. POSIX I/O Functions

UNIT-V CONSOLE I/O. This section examines in detail the console I/O functions.

Programming in C. Session 8. Seema Sirpal Delhi University Computer Centre

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

EM108 Software Development for Engineers

CSE2301. Introduction. Streams and Files. File Access Random Numbers Testing and Debugging. In this part, we introduce

Fundamentals of Programming & Procedural Programming

C BOOTCAMP DAY 2. CS3600, Northeastern University. Alan Mislove. Slides adapted from Anandha Gopalan s CS132 course at Univ.

Course organization. Course introduction ( Week 1)

CSE 333 Midterm Exam July 24, Name UW ID#

CS360 Midterm 2 Spring, 2016 James S. Plank March 10, 2016

EXAMINATION REGULATIONS and REFERENCE MATERIAL for ENCM 339 Fall 2017 Section 01 Final Examination

COMP 2355 Introduction to Systems Programming

CP2 Revision. theme: file access and unix programs

Communication. Serial port programming

Introduction to C Programming (Part C) Copyright 2008 W. W. Norton & Company. All rights Reserved

Quick review of previous lecture Ch6 Structure Ch7 I/O. EECS2031 Software Tools. C - Structures, Unions, Enums & Typedef (K&R Ch.

SYSTEM AND LIBRARY CALLS. UNIX Programming 2015 Fall by Euiseong Seo

LSN 3 C Concepts for OS Programming

Transcription:

Final Test Duration 50 minutes Aids allowed: none Last Name: Student Number: First Name: Instructor: Reid Section: L5101 (6:10-7:00pm) Do not turn this page until you have received the signal to start. (Please fill out the identification section above, write your name on the back of the test, and read the instructions below.) Good Luck! This midterm consists of 5 questions on 8 pages (including this one). When you receive the signal to start, please make sure that your copy is complete. Comments are not required, although they may help us mark your answers. They may also get you part marks if you can t figure out how to write the code. No error checking is required unless you are specifically requested to do it for an individual question. You do not need to provide the include statements for your programs. If you use any space for rough work, indicate clearly what you want marked. # 1: / 8 # 2: / 2 # 3: / 4 # 4: / 6 # 5: / 6 TOTAL: /26 Total Pages = 8 Page 1

Question 1. [8 marks] The following program reads star data from a text file in the following format. Each line contains a string (with no spaces in it) representing the name of a star followed by a space and the magnitude of the star as a floating point number. In writing the code below, you may assume that the file has the correct format. Example: Polaris 1.97 Vega 0.03 Aldebaran 0.87 Part (a) [5 marks] Complete the function read stars that will read star data from the open text file pointer passed in as an argument. It will create and build a linked list of struct star s from this data. New elements may be inserted at the head of the list. If the file is empty, return NULL Assume the length of a star name is no more than MAXSIZE - 1. Values stored in the linked list should occupy no more space than necesssary. struct star { char *name; double magnitude; struct star *next; }; struct star *read_stars(file *fp) { Page 2 of 8

Part (b) [3 marks] Complete the function below that calls free on memory allocated for the linked list of stars, head. Remember not to leave dangling pointers. void free_stars(struct star *head) { Question 2. [2 marks] I spent a long time working on my print ftree program on cdf and it works perfectly, but when I compile and run it on another Linux machine, once in a while it gives me a segmentation fault. Part (a) [1 mark] Is there a bug in my code? Explain your answer. Part (b) [1 mark] When does the segmentation fault message appear? Check the appropriate answer or answers. Compile time Run time When runnning make print ftree Page 3 of 8

Question 3. [4 marks] Consider the following Makefile from Assignment 1 FLAGS = -Wall -std=c99 compute_hash: compute_hash.o hash_functions.o gcc ${FLAGS} -o $@ $^ %.o : %.c gcc ${FLAGS} -c $< clean : rm *.o compute_hash The contents of the current working directory are: Makefile compute_hash.c hash_functions.c In the questions below, identify the files that are created, modified or deleted when each command is run in sequence. Part (a) [1 mark] make compute_hash.o Created Modified Deleted compute_hash.o Part (b) [1 mark] make Created Modified Deleted hash_functions.o compute_hash Part (c) [1 mark] First hash functions.c is modified and then make compute hash is run. Created Modified Deleted (hash_functions.c) hash_functions.o compute_hash Part (d) [1 mark] I do some more work on the program and add a new file called helper.c that contains functions called by code in compute hash.c. (Assume I updated the source files correctly.) Modify the Makefile above so that the new file will be compiled correctly with the program. Page 4 of 8

Question 4. [6 marks] struct team { char name[16]; char *players[4]; }; Part (a) [3 marks] struct team *ont; struct team scotties[10]; Give the declaration for the variable x in each of the statements below, or if there is an error in the statments, describe the error in a few words. Assume that ont and scotties (above) have been appropriately initialized with all necessary memory allocated. Statement x = ont->name; Declaration of x or error x = ont->name[0]; x = &ont; x = scotties[2]; x = scotties.players[1]; x = &scotties[2].players[1]; Part (b) [3 marks] For each of the following independent snippet of code identify the problem with the code, or write no problem if there are no potential problems with the snippet. Statements ont->name = malloc(10); strncpy(ont->name, "Homan", strlen("homan")); Problem ont->name = malloc(10); ont->name = "Homan"; char name[10] = "Homan"; ont->name = name; Page 5 of 8

Question 5. [6 marks] Complete the function below, so that the following examples will work correctly and the minimum amount of memory is used. The only string library functions you may use are strlen and strncpy. char str[] = "first,,third,fourth"; printf("0 %s\n", getfield(str, 0)); printf("1 %s\n", getfield(str, 1)); printf("2 %s\n", getfield(str, 2)); printf("3 %s\n", getfield(str, 3)); Prints: 0 first,,third,fourth 1,third,fourth 2 third,fourth 3 fourth /* Returns a new string containing the characters starting from * the nth comma in line until the end of the line. * Does not modify line. * Returns NULL if n is larger than the number of commas in line */ char *getfield(char *line, int n) { Page 6 of 8

C function prototypes: int fclose(file *stream) char *fgets(char *s, int n, FILE *stream) FILE *fopen(const char *file, const char *mode) int fprintf(file *stream, const char *format,...) size t fread(void *ptr, size t size, size t nmemb, FILE *stream) void free(void *ptr) int fscanf(file *restrict stream, const char *restrict format,...) int fseek(file *stream, long offset, int whence) size t fwrite(const void *ptr, size t size, size t nmemb, FILE *stream) void *malloc(size t size) DIR *opendir(const char *name) void perror(const char *s) int printf(const char *format,...) struct dirent *readdir(dir *dir) int scanf(const char *restrict format,...) int lstat(const char *file name, struct stat *buf) char *strchr(const char *s, int c) size t strlen(const char *s) char *strncat(char *dest, const char *src, size t n) int strncmp(const char *s1, const char *s2, size t n) char *strncpy(char *dest, const char *src, size t n) char *strrchr(const char *s, int c) char *strstr(const char *haystack, const char *needle) Excerpt from fgets man page: fgets() reads in at most one less than size characters from stream and stores them into the buffer pointed to by s. Reading stops after an EOF or a newline. If a newline is read, it is stored into the buffer. A terminating null byte ( \0 ) is stored after the last character in the buffer. Excerpt from scanf/fscanf man page: RETURN VALUES scanf and fscanf return the number of input items assigned. This can be fewer than provided for, or even zero, in the event of a matching failure. The value EOF is returned if an input failure occurs before any conversion such as an end- of-file occurs. Makefile variables: $@ is the target, $^ is the list of prerequisites $< is the first prerequisite. Page 7 of 8

Print your name in this box. Page 8 of 8 End of Examination