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

Similar documents
ECE264 Fall 2013 Exam 1, September 24, 2013

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.

ECE264 Fall 2013 Exam 3, November 20, 2013

ECE551 Midterm Version 1

ECE551 Midterm Version 2

ECE264 Spring 2014 Exam 2, March 11, 2014

ECE551 Midterm Version 1

ECE264 Fall 2013 Exam 2, October 24, 2013

ECE264 Spring 2013 Exam 1, February 14, 2013

ECE 551D Spring 2018 Midterm Exam

ECE264 Spring 2013 Final Exam, April 30, 2013

COMP 2355 Introduction to Systems Programming

Procedural Programming

File IO and command line input CSE 2451

ECE264 Summer 2013 Exam 1, June 20, 2013

PRINCIPLES OF OPERATING SYSTEMS

CS 0449 Sample Midterm

Pointers, Arrays, and Strings. CS449 Spring 2016

CS240: Programming in C

Signature: ECE 551 Midterm Exam

CSC209H Lecture 3. Dan Zingaro. January 21, 2015

Understanding Pointers

ECE 551D Spring 2018 Midterm Exam

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

CS 261 Fall Mike Lam, Professor. Structs and I/O

Procedural Programming & Fundamentals of Programming

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

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

CSE 333 Midterm Exam July 24, Name UW ID#

CSE 333 Midterm Exam 5/10/13

LSN 3 C Concepts for OS Programming

CSE 333 Midterm Exam 2/14/14

CSC209H Lecture 4. Dan Zingaro. January 28, 2015

CSE 230 Intermediate Programming in C and C++ Arrays, Pointers and Strings

Dynamic memory allocation

Tutorial 1 C Tutorial: Pointers, Strings, Exec

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

structs as arguments

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

CSE 303 Midterm Exam

Memory. What is memory? How is memory organized? Storage for variables, data, code etc. Text (Code) Data (Constants) BSS (Global and static variables)

Fundamental of Programming (C)

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

Dynamic memory. EECS 211 Winter 2019

Winter 2017 CS107 Midterm #2 Examination (Practice #2) Problem Points Score Grader TOTAL 30

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

Final CSE 131B Spring 2004

Character Strings. String-copy Example

C mini reference. 5 Binary numbers 12

So far, system calls have had easy syntax. Integer, character string, and structure arguments.

C: Pointers, Arrays, and strings. Department of Computer Science College of Engineering Boise State University. August 25, /36

Common Misunderstandings from Exam 1 Material

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

Files and Streams Opening and Closing a File Reading/Writing Text Reading/Writing Raw Data Random Access Files. C File Processing CS 2060

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Lecture 05 Pointers ctd..

8. Characters, Strings and Files

Lab Exam 1 D [1 mark] Give an example of a sample input which would make the function

CSE 303 Winter 2008 Midterm Key

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

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

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

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

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

CSE 333 Midterm Exam 7/29/13

CSE 333 Midterm Exam July 24, 2017 Sample Solution

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

Winter 2017 March 2, 2017 CS107 2 nd Midterm Examination

The C Programming Language

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

Lecture 8 Dynamic Memory Allocation

Array Initialization

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Lecture 04 Introduction to pointers

Deep C. Multifile projects Getting it running Data types Typecasting Memory management Pointers. CS-343 Operating Systems

Department of Electrical and Computer Engineering The University of Texas at Austin

Memory Allocation. General Questions

C programming basics T3-1 -

Memory Management. a C view. Dr Alun Moon KF5010. Computer Science. Dr Alun Moon (Computer Science) Memory Management KF / 24

upper and lower case English letters: A-Z and a-z digits: 0-9 common punctuation symbols special non-printing characters: e.g newline and space.

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

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

Armide Documentation. Release Kyle Mayes

Kurt Schmidt. October 30, 2018

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

Principles of C and Memory Management

CSE 333 Midterm Exam 2/12/16. Name UW ID#

Memory Management I. two kinds of memory: stack and heap

Arrays and Pointers. Arrays. Arrays: Example. Arrays: Definition and Access. Arrays Stored in Memory. Initialization. EECS 2031 Fall 2014.

211: Computer Architecture Summer 2016

C-Refresher: Session 10 Disk IO

Lecture 7: Files. opening/closing files reading/writing strings reading/writing numbers (conversion to ASCII) command line arguments

Computer Programming: Skills & Concepts (CP) Strings

Final exam. Scores. Fall term 2012 KAIST EE209 Programming Structures for EE. Thursday Dec 20, Student's name: Student ID:

Lab # 4. Files & Queues in C

CYSE 411/AIT681 Secure Software Engineering Topic #10. Secure Coding: Integer Security

Exercise Session 2 Simon Gerber

Question 1. [15 marks]

2/9/18. Readings. CYSE 411/AIT681 Secure Software Engineering. Introductory Example. Secure Coding. Vulnerability. Introductory Example.

Transcription:

ECE 264 Exam 2 6:30-7:30PM, March 9, 2011 I certify that I will not receive nor provide aid to any other student for this exam. Signature: You must sign here. Otherwise you will receive a 1-point penalty. Read the questions carefully. Please write legibly. Your exam is not graded if your writing is hard to read. This exam is printed double sided. Please read the questions carefully. Two common mistakes are answering a wrong question and failing to answer all questions. This is an open-book, open-note exam. You can use any book or note or program printouts. Please turn off your cellular phone and ipod. No electronic device is allowed. Outcomes 2 and 3 are tested in this exam. To pass an outcome, you must receive 50% or more points in the corresponding questions. 1

Contents 1 Structure (Outcome 2, 5 points) 4 2 Linked List (Outcome 3, 7 points) 7 3 File (3 points) 11 Passed Outcomes: outcome 2 outcome 3 Total Score: out of 15. 2

This page is blank. You can write answers here. 3

1 Structure (Outcome 2, 5 points) Assume you have a C structure for a student database with this definition: typedef struct char *name; int year; Student; // name of student // birth year Please fill in your answers to the questions in the skeleton code given below. Hint: The functions may be useful: strcpy(char *dest, const char *src) strdup(const char *s) size_t strlen(const char *s); malloc(size_t size) free(void * ptr) You can treat size t as int. (a) Write a constructor function that allocates a new Student record (i.e. object) and fills it with data passed as arguments. (b) Write a function for retrieving the name of a student from a student record. (c) Write a function for retrieving the year of a student from a student record. (d) Write a function for copying a student record. Bear in mind that the copy should be deep, i.e., you will need to duplicate strings and not just their pointers. (e) Write a function for deallocating all of the memory associated with a Student, including the Student record itself. Remember to deallocate all strings as well. Student *Student_create(char *name, int year) /* === (a) Create a new student record (1.7 point) */ 4

char *Student_getName(Student *s) /* === (b) Retrieve the name for a student record (0.4 point) */ int Student_getYear(Student *s) /* === (c) Retrieve the birth year for a student record (0.4 point) */ Student *Student_clone(Student *s) /* === (d) Copy a student record (deep copy) (1.6 point) */ /* The new Student object should not share memory with the argument s */ 5

void Student_destroy(Student *s) /* === (e) Deallocate all memory for a student record (0.9 point) */ int main(int argc, char **argv) Student *s1 = Student_create("John Doe", 1985); printf("name: %s, Year: %d\n", Student_getName(s1), Student_getYear(s1)); /* Name: John Doe, Year: 1985 */ Student *s2 = Student_clone(s1); printf("name: %s, Year: %d\n", Student_getName(s2), Student_getYear(s2)); /* Name: John Doe, Year: 1985 */ strcpy(s2 -> name, "Amy"); s2 -> year = 1990; printf("name: %s, Year: %d\n", Student_getName(s2), Student_getYear(s2)); /* Name: Amy, Year: 1990 */ printf("name: %s, Year: %d\n", Student_getName(s1), Student_getYear(s1)); /* Name: John Doe, Year: 1985 */ Student_destroy(s1); Student_destroy(s2); return 0; 6

2 Linked List (Outcome 3, 7 points) Assume you have a C structure for a linked list of integers with this definition: /* Node.h */ struct Node int value; struct Node *next; ; Implement the missing code for the below four functions for inserting values at the beginning, end, and at a specific index in a linked list of integers. You may use the provided functions. #include <stdlib.h> #include <stdio.h> #include "Node.h" struct Node *Node_create(int value) struct Node *node = malloc(sizeof(struct Node)); node->value = value; node->next = NULL; return node; void Node_destroy(struct Node *n) free(n); void List_destroy(struct Node *head) struct Node * p; while (head!= NULL) p = head; head = head -> next; free(p); void List_print(struct Node *head) while (head!= NULL) printf("%d ", head->value); 7

head = head->next; printf("\n"); struct Node *List_insertFront(struct Node *head, int value) /* Insert an element (i.e. a Node) with an integer value at the beginning of the list. */ /* head = the first element of the original list */ /* value = the value of a new element of the list */ /* The original list may be empty */ /* Returns the pointer to the first element. */ /* === FILL IN CODE BELOW (1 point) */ struct Node *List_insertBack(struct Node *head, int value) /* Insert an element (i.e. a Node) with an integer value at the end of the list. */ /* head = the first element of the original list */ /* value = the value of a new element of the list */ /* The original list may be empty */ /* Returns the pointer to the first element. */ /* === FILL IN CODE BELOW (2 points) */ 8

struct Node *List_insertAt(struct Node *head, int index, int value) /* Inserts an element (i.e. a Node) with an integer value at a specific location, specified by the index, in list. */ /* If index is negative, the new element is at the beginning of the list */ /* If index is larger than the list s length, the new element is at the end of the list. */ /* Returns the pointer to the first element. */ /* Hint: you can use List_insertFront and List_insertBack */ /* === FILL IN CODE BELOW (2 points) */ struct Node *List_reverse(struct Node *head) /* NOTE: This function should ===NOT=== allocate any new memory. */ 9

/* Reverse the integer values in the list so that the order of the element is reverse. */ /* If a Node is the first element in the original list, this Node becomes the last element in the new list */ /* If the head pointer is NULL, this function returns NULL. */ /* Returns the pointer to the new (reversed) list. */ /* === FILL IN CODE BELOW (2 points) */ int main(int argc, char *argv[]) struct Node *head = NULL; head = List_insertFront(head, 42); head = List_insertFront(head, 10); List_print(head); /* Output: 10 42 */ head = List_insertBack(head, 66); List_print(head); /* Output: 10 42 66 */ head = List_insertAt(head, 0, 1); head = List_insertAt(head, 70, 72); head = List_insertAt(head, -9, 0); List_print(head); /* Output: 0 1 10 42 66 72 */ head = List_reverse(head); List_print(head); /* Output: 72 66 42 10 1 0 */ List_destroy(head); return 0; 10

3 File (3 points) The most basic file reading operation is fgetc that reads a single character from a file record pointer. It has the following function prototype: int fgetc ( FILE *stream ); stream: Pointer to a FILE object that identifies the stream the character is to be read from. Return value: fgetc() reads the next character from stream and returns it as an unsigned char cast to an int, or EOF on end of file or error. We want to implement a new function that reads more than just one character. This function is int fgetline (char *buf, int n, FILE *stream ); The function reads a line from a text file. A line is defined as a sequence of (i) at most (n - 1) characters or (ii) until a newline ( \n ) is found. The characters, in case (ii) including the newline, are stored in an array called buf. A \0 character is appended to the end of the string. The function fgetline does not check whether buf has enough space. This is the caller s responsibility. buf: An array of characters. It stores the characters read from the file. n: At most (n - 1) characters are read from the file. stream: Pointer to a FILE object that identifies the stream the character is to be read from. Return value: fgetline returns the number of characters read from the file. Please implement this fgetline function. Hint: Since fgetc casts a character to an integer, you can assign the return value of fgetc to a character: char ch; FILE * fhd; fhd = fopen(...);... ch = fgetc(fhd); 11

#include <stdio.h> int fgetline(char *buf, int n, FILE *fhd) int main(int argc, char **argv) if (argc < 2) return -1; FILE *f = fopen(argv[1], "r"); const int BUFFER_SIZE = 10; char buffer[buffer_size]; while (fgetline(buffer, BUFFER_SIZE, f)!= 0) printf("%s", buffer); fclose(f); return 0; 12