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

Similar documents
University of California San Diego Department of Electrical and Computer Engineering. ECE 15 Midterm Exam

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

UNIT 9C Randomness in Computation: Cellular Automata Principles of Computing, Carnegie Mellon University

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

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

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

16.216: ECE Application Programming Fall 2011

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

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

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

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

ESC101N: Fundamentals of Computing End-sem st semester

CS , Fall 2003 Exam 2

Signature: ECE 551 Midterm Exam

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

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

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

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

ECE15: Lab #4. Problem 1. University of California San Diego

Programming Language B

Dynamic Memory Allocation

Tecniche di Progettazione: Design Patterns

EECE.2160: ECE Application Programming Fall 2017

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

EECE.2160: ECE Application Programming Fall 2017

CSE 333 Lecture 2 - arrays, memory, pointers

Assignment 4. Aggregate Objects, Command-Line Arguments, ArrayLists. COMP-202B, Winter 2011, All Sections. Due: Tuesday, March 22, 2011 (13:00)

CSE 303 Midterm Exam

//If target was found, then //found == true and a[index] == target.

High Performance Programming Programming in C part 1

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

ECE264 Spring 2013 Final Exam, April 30, 2013

Arrays and Pointers. CSE 2031 Fall November 11, 2013

Final Exam 1 /12 2 /12 3 /10 4 /7 5 /4 6 /10 7 /8 8 /9 9 /8 10 /11 11 /8 12 /10 13 /9 14 /13 15 /10 16 /10 17 /12. Faculty of Computer Science

Midterm Exam Answers Instructor: Randy Shepherd CSCI-UA.0201 Spring 2017

Name: Username: I. 20. Section: II. p p p III. p p p p Total 100. CMSC 202 Section 06 Fall 2015

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

The output will be: marks all or nothing. 1 #include <stdio.h> 2 main() { 3 int i; int j; 4 int *p; int *q; 6 p = &i; 7 q = &j; 8 i = 1;

Content. In this chapter, you will learn:

Q1: /8 Q2: /30 Q3: /30 Q4: /32. Total: /100

Question 1. [15 marks]

Variation of Pointers

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

Computer Science Foundation Exam

Lecture 04 Introduction to pointers

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

Forest Fire Simulation Using Multiple Processes and Pipes

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

CS2 Practical 1 CS2A 22/09/2004

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

PDS Class Test 2. Room Sections No of students

The University of Calgary. ENCM 339 Programming Fundamentals Fall 2016

Lecture 3. Review. CS 141 Lecture 3 By Ziad Kobti -Control Structures Examples -Built-in functions. Conditions: Loops: if( ) / else switch

Lecture07: Strings, Variable Scope, Memory Model 4/8/2013

CSE 374 Midterm Exam 2/6/17 Sample Solution. Question 1. (12 points, 4 each) Suppose we have the following files and directories:

Lecture 5: Multidimensional Arrays. Wednesday, 11 February 2009

CSE 333 Lecture 3 - arrays, memory, pointers

ECE15: Lab #2. Problem 1. University of California San Diego

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING PROGRAMMING TECHNIQUES I EE271

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

LOÏC CAPPANERA. 1. Memory management The variables involved in a C program can be stored either statically or dynamically.

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

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

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

ECE264 Fall 2013 Exam 1, September 24, 2013

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

Chapter 2 (Dynamic variable (i.e. pointer), Static variable)

Dynamic Memory Allocation (and Multi-Dimensional Arrays)

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

Dynamic Memory. Dynamic Memory Allocation Strings. September 18, 2017 Hassan Khosravi / Geoffrey Tien 1

Programming Language A

Warmup January 9th, What is the value of the following C expression? 8*9 % 10/ 2

SOFTWARE Ph.D. Qualifying Exam Fall 2017

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

Lesson 7. Reading and Writing a.k.a. Input and Output

EECE.2160: ECE Application Programming Spring 2018 Exam 3 May 10, 2018

#3. (Recursion) Write a recursive function to compute f(x) = f(x - 1) + f(x - 2) with f(0) = 0 and f(1) = 1.

CSE351 Winter 2016, Final Examination March 16, 2016

Programming Assignment #4 Arrays and Pointers

Arrays. An array is a collection of several elements of the same type. An array variable is declared as array name[size]

FORM 2 (Please put your name and form # on the scantron!!!!) CS 161 Exam II:

ECE551 Midterm Version 2

Dynamic memory allocation

Arrays and Pointers in C. Alan L. Cox

Computer Science & Engineering 150A Problem Solving Using Computers

CSE 374 Midterm Exam 11/2/15. Name Id #

Class Information ANNOUCEMENTS

Engineering program development 6. Edited by Péter Vass

CSE 333 Midterm Exam 7/25/16 Sample Solution. Question 1. (10 points) Preprocessor. Suppose we have the following two files:

CS 241 Data Organization using C Project 3: Tron Spring 2017

Final Intro to C Review

ch = argv[i][++j]; /* why does ++j but j++ does not? */

15-122: Principles of Imperative Computation, Spring Written Homework 12. Due: Sunday 15 th April, 2018 by 10pm. Name: Andrew ID: Section:

ECE264 Spring 2013 Exam 1, February 14, 2013

Procedural programming with C

Language Design COMS W4115. Prof. Stephen A. Edwards Spring 2003 Columbia University Department of Computer Science

CSCI 4210 Operating Systems CSCI 6140 Computer Operating Systems Homework 3 (document version 1.2) Multi-threading in C using Pthreads

Transcription:

University of California San Diego Department of Electrical and Computer Engineering ECE 15 Final Exam Tuesday, March 21, 2017 3:00 p.m. 6:00 p.m. Room 109, Pepper Canyon Hall Name Class Account: ee15w Student ID Number Signature Grading 1. 6 points INSTRUCTIONS The exam consists of six problems worth a total of 100 points. Write your answers in the spaces provided. If you need extra space, please use the back of the previous page. Partial credit will be given only for substantial progress. Good luck! 2. 9 points 3. 12 points 4. 20 points 5. 18 points 6. 35 points TOTAL

Problem 1 (6 points) Consider the following simple function: int isequal(int x, int y) if (x == y) return 1; else return 0; Rewrite this function below without using the keywords if and else. You are also required to use the keyword return exactly once. int isequal(int x, int y) Hint: Use the ternary conditional operator? :.

Problem 2 (9 points) Consider the following simple function: void foo(int n) if (n %2 == 0) printf("2"); else if (n %3 == 0) printf("3"); else if (n %5 == 0) printf("5"); else if (n %7 == 0) printf("7"); else printf("no divisors!"); Rewrite this function below without using the keywords else or switch. You also cannot use the keyword if more than once in your source code. void foo(int n) Hint: Store the numbers 2, 3, 5, 7 in an array, and process this array in a loop.

Problem 3 (12 points) Consider the following C program that contains global, local, and static variables: #include <stdio.h> #define N 5 int i,j; /* global variables */ /* Forward function declarations */ void f1(int); int *f2(int *); int f3(int); int main() int a = N; int myarray[n] = ; int *p = myarray; for (i = 0; i < N; i++) int j = i % 2; /* local variable */ if (i == 0) f1(a); else if (j == 0) p = f2(p); else if (j == 1) a = f3(a); *p = a; // most of the action happens here! for (j = 0; j < N; j++) printf("%d ", myarray[j]); printf("\n"); return 0; void f1(int a) a += ++j; int *f2(int *a) return a += j++; int f3(int a) static int i = 0; return a+i++;

You should assume that the program compiles and runs successfully. Your task is to determine the output of the program. Write your answers in the space provided. Explanations are not required.

Problem 4 (20 points) In this problem, you are given several exercises, each containing a short C program. You should assume that the program compiles and runs successfully. Your task is to determine, in each case, the output of the program. Write your answers in the space provided. Explanations are not required. a. #include <stdio.h> int main() int x,y,z; x = y = z = 1; ++x && ++y ++z; printf("a. x = %d, y = %d, z = %d\n", x,y,z); x = y = z = 1; x += y += z; printf("b.? = %d, ", (x < y)? x++ : y++); printf("x = %d, y = %d\n", x,y); x = 1; y = 2; z = 3; printf("c.? = %s\n", (z > y > x)? "True" : "False"); return 0; The program output is: a. x =, y =, z = b.? =, x =, y = c.? =

b. #include <stdio.h> void foo(int, int *, int *); int main() int a[3] = 1729,42,0; int *ptr = a; int x = 10, z = 12; foo(x, ptr, &z); printf("x = %d, z = %d\n", x,z); printf("array elements: %d, %d, %d\n", a[0],a[1],a[2]); return 0; void foo(int x, int *y, int *z) printf("x = %d, z = %d\n", x,*z); x++; (*z)++; printf("x = %d, z = %d\n", x,*z); x = *(y++); *z = *(y++); (*y)++; The program output is: x =, z = x =, z = x =, z = Array elements:

c. #include <stdio.h> int main() int i,j; int M[5][5] = 0; for (i = 0; i < 5; i++) for (j = 0; j < 5; j++) if ((i*j) % 2) *(*(M+i)+j) = i*j; *((*M+i)+j) = i*j; for (i = 0; i < 5; i++) for (j = 0; j < 5; j++) printf("%d ", M[i][j]); if (j == 4) printf("\n"); return 0; The program output is:

Problem 5 (18 points) Write a C program, called anagrams.c, that prompts the user to enter two strings of characters S 1 and S 2, and then determines whether these strings are anagrams of each other. The strings S 1 and S 2 are said to be anagrams of each other if the characters in S 1 can be re-arranged to produce S 2. For example, the words student and stunted are anagrams, as are the words admirer and married. On the other hand, the words cats and stack are not anagrams. Clearly, a necessary condition for S 1 and S 2 to be anagrams is that they have the same length. Note that the strings S 1 and S 2 do not have to consist of letters. For example, 123456789! and 9876!54321 are valid anagrams. On the other hand, the words Admirer and Married are not anagrams, since our definition of anagrams is case sensitive. Write a C program that prompts the user to enter two strings (terminated by the newline character \n), determines whether these strings are anagrams of each other, and reports the results to the user. Here are several sample runs of this program: /home/userxyz/ece15/final> anagrams Enter the first string: 123456789! Enter the second string: 9876!54321 Your strings "123456789!" and "9876!54321" are anagrams! /home/userxyz/ece15/final> anagrams Enter the first string: admirer Enter the second string: married Your strings "admirer" and "married" are anagrams! /home/userxyz/ece15/final> anagrams Enter the first string: Admirer Enter the second string: Married Your strings "Admirer" and "Married" are NOT anagrams! /home/userxyz/ece15/final> anagrams Enter the first string: cats Enter the second string: stack Your strings "cats" and "stack" are NOT anagrams! Hint: For each character in S 1, count how many times this character appears in S 1 and S 2, respectively. The strings are anagrams if and only if these counts coincide for every character of S 1. Notes: You can assume that the user input to your program is always valid. That is, when prompted, the user always enters a string (without whitespace) terminated by the newline character \n. You can also assume that the longest string the user will enter does not exceed MAXLENGTH characters, where MAXLENGTH is a symbolic constant.

#include <stdio.h> #include <string.h> #define MAXLENGTH 80 int main() return 0;

Problem 6 (35 points) Your goal in this problem is to write a C program, called life.c, that implements the Game of Life invented by John H. Conway in 1970. For the purposes of this problem, the Game of Life is played on an n n two-dimensional square grid (or board, or matrix, or array) of cells, where n is a variable the program should read from its input. Each cell can be in one of two possible states: alive or dead. Every cell interacts with its 8 neighbors (or 5 neighbors, or 3 neighbors, if the cell is close to an edge or a corner of the grid). The neighbors of a cell grid[i, j] are the cells that are horizontally, vertically, or diagonally adjacent to the cell grid[i, j]. Note that grid[i, j] is not considered its own neighbor. The game begins with an initial configuration, called the seed, that specifies the state of every cell in the grid. The program should read this initial configuration from its input. The game then evolves in a sequence of generations according to the following rules: Any live cell with 2 or 3 live neighbors lives on to the next generation. Any live cell with more than 3 live neighbors dies (by overcrowding), and any live cell with less than 2 live neighbors also dies (from loneliness). Any dead cell with exactly 3 live neighbors becomes a live cell in the next generation (by reproduction). Any dead cell with more than 3 or less than 3 live neighbors remains dead. The initial seed is considered the 0-th generation. The first generation is created by applying the above rules simultaneously to every cell in the seed array. The rules then continue to be applied repeatedly to create further generations, where each generation is completely determined by the previous one. Your C program should read its input from the file life input.dat. The first line of the input file contains a positive integer that specifies the dimension n of the n n grid on which the game is played. The second line contains another positive integer that specifies the number of generations you are required to compute. The following n lines specify the initial seed configuration of the grid, where a dead cell is given as 0 and a live cell is given as 1. Here is a sample input file: 9 5 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 0 0 1 0 0 1 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 You can assume that the input file will conform to the specifications above. You can also assume that the required number of generations (second line of input) will be at most 8192. However, you should not assume anything regarding the size n of the grid (first line), except that allocating a few n n arrays of int will not exhaust the available memory. After reading the input file, the C program should iteratively compute the required number of generations and print them to stdout, starting

with the 0-th generation (read from the input file). Here is a sample run of the program, assuming the input file life input.dat is exactly as shown on the previous page: /home/userxyz/ece15/final> life Generation 0: ---**---- ----*-*-- -*-----*- -**---*-- --*-*---- ---*----- Generation 1: ---***--- ---***--- -**--***- -**------ -***----- ---*----- ---*----- Generation 2: ----*---- ---*-*--- -*---**-- *-----*-- -*-*----- ---**---- Generation 3: ----*---- ----*---- ----***-- -----**-- ***--**-- --***---- --***---- Generation 4: ---**---- ----*-*-- -*-----*- -**---*-- --*-*---- ---*----- Generation 5: ---***--- ---***--- -**--***- -**------ -***----- ---*----- ---*----- Note that in the program output above dead cells are represented by - while live cells are represented by * (also note that, in this example, the generations repeat in a cycle starting with the 4-th one).

a. Provided below is part of the source code that implements the C program you are asked to write. This part contains all the global variable(s), all the forward function declarations, and the complete implementation of the function main(). #include <stdio.h> #include <stdlib.h> FILE *input; // // int **alloc int matrix(int n); void read int matrix(file *input, int **grid, int n); void print int matrix(int **grid, int n); void copy int matrix(int **from, int **to, int n); int inbounds(int i, int j, int n); int new state(int **grid, int i, int j, int n); void new generation(int **grid, int n); int main() int **grid; // int n,generations,i; // // if ((input = fopen("life input.dat","r")) == NULL) return 1; // fscanf(input,"%d",&n); fscanf(input,"%d",& generations); // grid = alloc int matrix(n); read int matrix(input,grid,n); // printf("generation 0:\n"); print int matrix(grid,n); // for (i = 1; i <= generations; i++) new generation(grid,n); printf("\ngeneration %d:\n",i); print int matrix(grid,n); return 0; Add comments above, where indicated by // or anywhere else you wish, in order to show your understanding of what the various pieces of code do. In particular, indicate which variables are local and which are global, and for pointer variables state their type.

b. The function main() calls the function read int matrix(input,grid,n), which reads the initial seed configuration of the grid from the file pointed to by input into a twodimensional square array of int, of a given size n by n. The function main() also calls the function print int matrix(grid,n) that prints the current state of the grid array (using * for a live cell and - for a dead cell) to stdout. Implement these functions below. void read int matrix(file *input, int **grid, int n) void print int matrix(int **grid, int n) Notes: Both functions should assume that the two-dimensional int array grid is already allocated. No need to worry about memory allocation here. Comments are not required, but could help you receive partial credit: programs that are not correct and are difficult to understand will receive zero credit, even if partially correct.

c. The function main() calls the function alloc int matrix(n). The latter function dynamically allocates a memory structure that can serve as a two-dimensional array of int of size n by n, and then returns a pointer to this structure. Implement this function below. int **alloc int matrix(int n) Notes: You can call either malloc or calloc standard library functions. The corresponding header file <stdlib.h> is already included in part (a). Do not forget to check for the situation where the requested block of memory is not available, and then deal with this situation if it arises.

d. We will need the functions inbounds(i,j,n) and copy int matrix(from,to,n) later in this program. Let us implement these functions here. The function inbounds(i,j,n) simply checks if the pair i,j is a valid index for a twodimensional array of size n by n. This function returns 1 (True) if i,j is a valid index and 0 (False) otherwise. The function copy int matrix(from,to,n) simply copies a twodimensional array of int of size n by n from one place (pointed to by from) to another place (pointed to by to). Implement the two functions below. int inbounds(int i, int j, int n) void copy int matrix(int **from, int **to, int n)

e. Implement the function new state(grid,i,j,n). This function receives as arguments (a pointer to) a two-dimensional array grid, a pair of integers i,j that is assumed to be a valid index for the array, and an integer n that specifies the size of the array. The function uses the current cell states stored in the array grid to compute the state of the cell grid[i][j] in the next generation. This new state of grid[i][j], namely dead (0) or alive (1), is returned by new state(grid,i,j,n). Implement this function below. int new state(int **grid, int i, int j, int n) Notes: This is where the rules of life and/or death in the next generation are implemented. See the description of the Game of Life at the beginning of this problem. To correctly count the number of live neighbors, it would be useful to call inbounds. Also note that a cell is not a neighbor of itself.

f. Now implement the last and most important function new generation(grid,n). This function receives as arguments (a pointer to) a two-dimensional array grid and an integer n that specifies its size. It computes the new state (the state in the next generation) for each cell in the grid, by invoking the function new state(grid,i,j,n). It then overwrites the values of all the cells in the grid with their state in the next generation. void new generation(int **grid, int n) Notes: This function should also call copy int matrix() and alloc int matrix(). It would be helpful to understand why this is so before proceeding. The function needs to dynamically allocate memory to itself. But beware of memory leaks over many generations! There are two ways to proceed. One option is to free the memory every time before the function terminates. A better option is to allocate the memory only once only the first time the function is called (hint: use static variables).