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

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

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

Homework 3 CS161 Computer Security, Fall 2008 Assigned 10/07/08 Due 10/13/08

Programming. Lists, Stacks, Queues

ECE264 Spring 2014 Exam 2, March 11, 2014

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

ECE264 Fall 2013 Exam 3, November 20, 2013

ECE264 Spring 2013 Final Exam, April 30, 2013

The combination of pointers, structs, and dynamic memory allocation allow for creation of data structures

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

Final CSE 131B Spring 2004

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

Class Information ANNOUCEMENTS

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

Question 1. [15 marks]

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

CSE 303, Spring 2009 Final Exam Wednesday, June 10, 2009

CS61, Fall 2012 Section 2 Notes

CS 1713 Introduction to Programming II

Cpt S 122 Data Structures. Data Structures

EL2310 Scientific Programming

Dynamic Memory Management. Bin Li Assistant Professor Dept. of Electrical, Computer and Biomedical Engineering University of Rhode Island

CS 1713 Introduction to Programming II

EE 312 Fall 2017 Midterm 1 October 12, 2017

Computer Science Foundation Exam

Midterm Review (overview of fall 2005 midterm)" Jennifer Rexford!

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

Lecture Notes on Types in C

Chapter 17: Linked Lists

l Determine if a number is odd or even l Determine if a number/character is in a range - 1 to 10 (inclusive) - between a and z (inclusive)

Quiz 0 Review Session. October 13th, 2014

CS 11 C track: lecture 5

Exercise Session 2 Systems Programming and Computer Architecture

ECE 190 Final Exam. Practice exam

CS11001/CS11002 Programming and Data Structures (PDS) (Theory: 3-1-0) Allocating Space

CS 261 Fall C Introduction. Variables, Memory Model, Pointers, and Debugging. Mike Lam, Professor

Procedural Programming

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Understanding Pointers

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

CSE 333 Midterm Exam 5/10/13

Programming in C. Lecture Tiina Niklander. Faculty of Science

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

University of California, Berkeley College of Engineering

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Chapter 17: Linked Lists

CA341 - Comparative Programming Languages

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

CPSC 211, Sections : Data Structures and Implementations, Honors Final Exam May 4, 2001

Approximately a Test II CPSC 206

CS 610: Intermediate Programming: C/C++ Making Programs General An Introduction to Linked Lists

CSE 333 Midterm Exam 2/14/14

These problems are provided to you as a guide for practice. The questions cover important concepts covered in class.

CSE351 Spring 2018, Final Exam June 6, 2018

(13-2) Dynamic Data Structures I H&K Chapter 13. Instructor - Andrew S. O Fallon CptS 121 (November 17, 2017) Washington State University

Introduction to C. Zhiyuan Teo. Cornell CS 4411, August 26, Geared toward programmers

CISC 3130 Data Structures Spring 2018

Project#3 [40 points] COP 2220 Fall 2017

Kurt Schmidt. October 30, 2018

Department of Electrical Engineering and Computer Sciences Spring 2007 Instructor: Dr. Dan Garcia

Wawrzynek & Weaver CS 61C. Sp 2018 Great Ideas in Computer Architecture MT 1. Print your name:,

CS 61C: Great Ideas in Computer Architecture C Pointers. Instructors: Vladimir Stojanovic & Nicholas Weaver

! Determine if a number is odd or even. ! Determine if a number/character is in a range. - 1 to 10 (inclusive) - between a and z (inclusive)

CSE 333 Midterm Exam Sample Solution 5/10/13

unsigned char memory[] STACK ¼ 0x xC of address space globals function KERNEL code local variables

Computer Science Foundation Exam

C Introduction. Comparison w/ Java, Memory Model, and Pointers

However, in C we can group related variables together into something called a struct.

Announcements. Lecture 05a Header Classes. Midterm Format. Midterm Questions. More Midterm Stuff 9/19/17. Memory Management Strategy #0 (Review)

MPATE-GE 2618: C Programming for Music Technology. Syllabus

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

Approximately a Final Exam CPSC 206

Malloc Lab & Midterm Solutions. Recitation 11: Tuesday: 11/08/2016

Dynamic Memory Allocation

This document contains the questions and solutions to the CS107 midterm given in Winter 2018 by instructor Chris Gregg. This was a 120-minute exam.

CS 2301 Exam 3 B-Term 2011

Linked List in Data Structure. By Prof. B J Gorad, BECSE, M.Tech CST, PHD(CSE)* Assistant Professor, CSE, SITCOE, Ichalkaranji,Kolhapur, Maharashtra

CS24 Week 4 Lecture 2

! Determine if a number is odd or even. ! Determine if a number/character is in a range. ! Assign a category based on ranges (wind speed)

Dynamic Data Structures. CSCI 112: Programming in C

Introduction to C. Sean Ogden. Cornell CS 4411, August 30, Geared toward programmers

CP2 Revision. theme: dynamic datatypes & data structures

Lecture 8 Dynamic Memory Allocation

CS 216 Exam 1 Fall SOLUTION

Computer Science Foundation Exam

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Introduction to C. Ayush Dubey. Cornell CS 4411, August 31, Geared toward programmers

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

Computer Architecture I Mid-Term I

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

CS 2505 Computer Organization I Test 2. Do not start the test until instructed to do so! printed

CSE 332 Spring 2013: Midterm Exam (closed book, closed notes, no calculators)

When you add a number to a pointer, that number is added, but first it is multiplied by the sizeof the type the pointer points to.

CSE 12 Spring 2016 Week One, Lecture Two

Data Structure Series

PRINCIPLES OF OPERATING SYSTEMS

6.S096: Introduction to C/C++

Department of Electrical Engineering and Computer Sciences Spring 2010 Instructor: Dr. Dan Garcia CS61C Midterm

COP Programming Concepts Spring 1999 CLOSED BOOK Exam #1 100 Points NAME

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

Transcription:

Department of Electrical and Computer Engineering The University of Texas at Austin EE 312, Spring 2015 Aater Suleman, Instructor Owais Khan, Chirag Sakhuja, TAs Exam 1, March 4, 2015 Name: Problem 1 (20 points): Problem 2 (10 points): Problem 3 (10 points): Problem 4 (15 points): Problem 5 (20 points): Problem 6 (20 points): Total (95 points): Note: Please be sure that your answers to all questions (and all supporting work that is required) are contained in the space provided. Note: Please be sure your name is recorded on each sheet of the exam. I will not cheat on this exam. Signature GOOD LUCK!

Problem 1. (20 points): Part a. (5 points): How many bytes are required to store the following variables on an Android device that has 64 bit addresses? int32_t *a; char *b; int32_t c; Number of Bytes: Part b. (5 points): What will the following line of code print? printf("%p\n", ((int64_t *) 0x100000) + 1); Output: Part c. (5 points): Dynamically allocated memory is used when the size of the memory to be allocated is unknown at COMPILE / RUN time (circle one answer).

Part d. (5 points): What will the contents of the stack be once the following code has completed executing? struct date { int32_t epoch; ; int main() { int8_t a = 1; struct date d; d.epoch = 0xdeadbeef; char *name = (char *) 0xfffffff; Assumptions: Machine is little endian (little end first) No compiler optimizations for alignment Local variables and struct elements are pushed on the stack in the order they are defined Pointers are 4 bytes Stack is initially empty (the first byte pushed on the stack will be saved at the location pointer to by the stack pointer) Please fill in the stack entries below. Each entry corresponds to one byte. You may not need to fill in all of the entries. stack grows upwards stack pointer

Problem 2. (10 points): Part a. (5 points): Specify an expression in the big-oh notation for the amount of time the following function will take in the worst case. int bigoh1(int N){ int count = 0; for(int i=0; i<100; i++){ for(int j=0; j<100; j++){ count++; return count; O( ) Part b. (5 points): Specify an expression in the big-oh notation for the amount of time the following function will take in the worst case. int bigoh2(int N){ int count = 0; for(int i=2; i<n; i=i*i){ count++; return count; O( )

Problem 3. (10 points): An Aggie was hired to write a function called calloc that: 1. Allocates memory using malloc 2. Initializes all the memory locations in the allocated memory block to 0 He made a mistake while implementing the function. He wrote the following code. void* calloc(int32 t size){ unsigned char* memory = (unsigned char*) malloc(size); for(int32 t i=0; i<size; i++){ *memory = 0; memory++; return (void*) memory; void debug1(){ int32 t number = 1000; char* memory = (char*) calloc(number); free(memory); void debug2(){ int32 t number = 1000; int32 t* memory = (int32 t*) calloc(number); // the next few lines will be the code that uses the allocated memory...... free(memory);

Part a. (5 points): To test the implementation, Aggie calls debug1() but the program crashes. Identify the bug in the calloc function and complete the code below to fix it. Note1: You are only allowed to write one line of code in each box. Note2: Even though there are three empty boxes, the correct solution only requires you to use one of them. Note3: There is more than one correct solution and specifying any one of them will earn you full credit. void* calloc(int32 t size){ unsigned char* memory = (unsigned char*) malloc(size); for(int32 t i=0; i<size; i++){ *memory = 0; memory++; return (void*) memory; Part b. (5 points): After fixing the bug in the calloc implementation, Aggie calls debug2(). However, the Aggie does not get the correct results. Identify the bug in debug2() and complete the code below to fix it. Write down the correct code in the blocks below. void debug2(){ int32 t number = 1000; // next few lines contain code which use the allocated memory......

Problem 4. (15 points): Please answer the questions about the following code. You may assume that there are no compiler optimizations enabled and there is no padding on the stack. int32_t *add(int32_t a, int32_t b) { int32_t c; c = a + b; return &c; int32_t main(int32_t argc, const char *argv[]) { int32_t z = 0; int32_t *t = add(2, 1); z = *t; add(z, 1); printf("%d %d\n", *t, z); return 0; Part a. (8 points): What is the output of the program? Output: Please explain in less than 15 words why this output is different from the expected output of 3 3. Part b. (7 points): Correct the code so that it produces the expected output. Do not change the main function. Note: For this problem you are not required to handle memory leaks. Hint: The solution can be written in 3 lines of code. int32_t *add(int32_t a, int32_t b) {

Problem 5. (20 points): In class we talked about one possible way to implement Canvas using abstract data types. In this problem, you will design the function that will be called to display the home page of Canvas when a student logs in. Below are some of the ADTs we came up. Note: Only the variables relevant to this problem are shown. struct ClassType {... /* null-terminated name of this class */ char *classname; struct StudentType {... /* number of classes the student is enrolled in */ int numclasses; ; /* number of announcements this class has had */ int numannouncements; /* array of all announcements this this class has had */ /* note: the size of this array is numannouncements */ struct AnnouncementType *announce;... struct GradeType {... /* class this grade was for */ struct ClassType *class; ; /* a single grade */ int grade;... ; /* array of all the classes the student is enrolled in */ /* note: the size of this array is numclasses */ struct ClassType *classes; /* total number of grades the student has received in all classes */ int numgrades; /* array of all the grades the student has received */ /* note: the size of this array is numgrades */ struct GradeType *grades;... In addition to the data types, you can assume that the following functions have been implemented for you: uint8_t wasrecentannouncement(struct AnnouncementType *a): Given a pointer to an AnnouncementType, this function will return whether or not the announcement was made in the previous week. The return value will be a 0 if the announcement was not made in the last week, and it will be a 1 if it was made in the last week. void printannouncement(struct AnnouncementType *a): Given a pointer to an AnnouncementType, this function will output the date and contents of the announcement on the console in the correct format. int computeaveragegrade(struct GradeType *grades, struct ClassType *class): Given a class and the array of all the grades, this function will compute the average grade for the class. The value returned is always rounded up. Problem continued on next page.

Your goal is to implement the function that displays the home page of Canvas. For each class, the home page contains the name of the class, all of the announcements in that class posted within the last week (using the printannouncement function), and the student s average. Between each class, print the string ==========. An example home page could look like: EE312 March 02: Review session in class Average: 92 ========== EE313 February 28: The homework assignment will be due on Monday, March 2. Average: 95 ========== Fill in the missing code to implement the fuction printhomepage. Note 1: You may assume that every class will have at least one recent announcement and a valid grade average. Note 2: To print an announcement, you can simply call the printannouncement function. Hint: Your code should mostly consist of calls to the functions provided. A TA wrote a solution with seven lines of code. // The function will print the home page for the student passed as argument 1. void printhomepage(struct StudentType *student) { for(int i = 0; i < student->numclasses; i++) { struct ClassType *class = student->classes[i]; // Your code begins here // Your code ends here printf("==========\n");

Problem 6. (20 points): Your job is to help us complete the implementation of an abstract data type called queue. The queue supports FIFO ordering such that the first node to enter the queue must be the first node to leave the queue. Under the hood, we will implement the queue using a doubly linked list (DLL). DLL is similar to a traditional linked list (LL). In an LL, each node has a link (pointer) to the next node. In DLL, each node has two links: a link to the next node and a link to the previous node, as shown below. Each DLL node consists of a: 1. next pointer: Contains either the starting address of the next node or NULL if this is the last node. 2. back pointer: Contains either the starting address of the previous node or NULL if this is the first node. 3. value: The actual value stored in the node, an integer in our example Additionally, we maintain a queue pointer that contains either the starting address of the first node in queue or NULL if queue is empty. The implementation consists of three functions. 1. pushback(): Inserts an element at the end of the queue. 2. popfront(): Removes the element at the head of the queue. 3. erase(): Removes all elements in the queue which match the specified value. Your job: Fill in the missing lines of code such that the queue behaves as specified above.

#include <stdio.h> #include <stdlib.h> typedef struct node{ node; struct node* next; struct node* back; int32 t value; Part a. (4 points): Complete the implementation of the pushback function by filling in the missing code node* pushback(node* queue, int32 t value){ node* newnode = (node*) malloc(sizeof(node)); newnode->value = value; newnode->next = NULL; if (queue == NULL){ newnode->back = NULL; queue = newnode; else{ node* currnode = queue; while(currnode->next!=null){ currnode = newnode->back = currnode; currnode->next = newnode; return queue;

Part b. (8 points): Complete the implementation of the popfront function by filling in the missing code node* popfront(node* queue,int32 t* value){ node* firstnode = queue; if(queue==null){ printf("poping an empty queue.\n"); *value = -1; else if(firstnode->next==null){ *value = firstnode->value; free(firstnode); queue = NULL; else{ *value = firstnode->value; firstnode->next->back= queue = firstnode->next; return queue;

Part c. (8 points): Complete the implementation of the erase function by filling in the missing code node* erase(node* queue, int32 t value){ node* currnode = queue; // no elements in queue if(queue == NULL){ return queue; // one element in queue if(currnode->next==null){ if(currnode->value==value){ free(currnode); queue = NULL; return queue; while(currnode->next!=null){ if(currnode->value == value){ if(currnode->back==null){ currnode->next->back = NULL; queue = currnode->next; free(currnode); currnode = queue; else{ currnode->back->next = currnode->next->back = node* tmp = currnode->next; free(currnode); currnode = tmp; else{ currnode = currnode->next; // check last element if(currnode->value== value){ currnode->back->next = NULL; free(currnode); return queue;