What will the output of the following code snippet be? Why? What is a safer alternate implementation?

Similar documents
Please submit your request directly to Noha Sinno

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

CS 161 Exam II Winter 2018 FORM 1

CPSC 213, Summer 2017, Term 2 Midterm Exam Solution Date: July 27, 2017; Instructor: Anthony Estey

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

CS 161 Intro to CS I. Finish Pointers/Start Recursion

Scheme G. Sample Test Paper-I. Course Name : Computer Engineering Group Course Code : CO/CD/CM/CW/IF Semester : Second Subject Tile : Programming in C

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

SYSC 2006 C Winter 2012

I2204 ImperativeProgramming Semester: 1 Academic Year: 2018/2019 Credits: 5 Dr Antoun Yaacoub

Procedural programming with C

Dynamic Data Structures (II)

ECE264 Fall 2013 Exam 1, September 24, 2013

EK131 E5 Introduction to Engineering

Parameter passing. Programming in C. Important. Parameter passing... C implements call-by-value parameter passing. UVic SEng 265

1 Dynamic Memory continued: Memory Leaks

INITIALISING POINTER VARIABLES; DYNAMIC VARIABLES; OPERATIONS ON POINTERS

UNIVERSITY OF WINDSOR Fall 2007 QUIZ # 2 Solution. Examiner : Ritu Chaturvedi Dated :November 27th, Student Name: Student Number:

Reminder of midterm 1. Next Thursday, Feb. 14, in class Look at Piazza announcement for rules and preparations


Quiz 0 Review Session. October 13th, 2014

[0569] p 0318 garbage

34. Recursion. Java. Summer 2008 Instructor: Dr. Masoud Yaghini

Final Intro to C Review

Function with default arguments

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

CS 11 C track: lecture 5

Programming. Pointers, Multi-dimensional Arrays and Memory Management

Introduction to C Language (M3-R )

AMCAT Procedure functions and scope Sample Questions

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

Tribhuvan University Institute of Science and Technology 2065

CSC 211 Intermediate Programming. Arrays & Pointers

Memory Allocation. General Questions

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

P.G.TRB - COMPUTER SCIENCE. c) data processing language d) none of the above

16.216: ECE Application Programming Fall 2011

ESC101N: Fundamentals of Computing End-sem st semester

Pointers and Dynamic Memory Allocation

Questions Bank. 14) State any four advantages of using flow-chart

CSC 270 Survey of Programming Languages. What is a Pointer?

Manual Allocation. CS 1622: Garbage Collection. Example 1. Memory Leaks. Example 3. Example 2 11/26/2012. Jonathan Misurda

Data Types (cont.) Subset. subtype in Ada. Powerset. set of in Pascal. implementations. CSE 3302 Programming Languages 10/1/2007

Solutions to Assessment

CPSC 213, Summer 2017, Term 2 Midterm Exam Date: July 27, 2017; Instructor: Anthony Estey

EL2310 Scientific Programming

Assoc. Prof. Dr. Tansu FİLİK

Programming and Data Structure Solved. MCQs- Part 2

Lecture Notes on Dynamic Programming

At this time we have all the pieces necessary to allocate memory for an array dynamically. Following our example, we allocate N integers as follows:

Question 1. [15 marks]

Applied Programming and Computer Science, DD2325/appcs15 PODF, Programmering och datalogi för fysiker, DA7011

COP4020 Fall 2006 Final Exam

Memory and C++ Pointers

Computer Science & Engineering 150A Problem Solving Using Computers

FORM 2 (Please put your name and form # on the scantron!!!!)

Computer Programming Unit 3

Q1 (15) Q2 (15) Q3 (15) Q4 (15) Total (60)

Applied Programming and Computer Science, DD2325/appcs14 PODF, Programmering och datalogi för fysiker, DA7011

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

ECE264 Spring 2013 Final Exam, April 30, 2013

1.7 Recursion. Department of CSE

Functions. Chapter 5

IMPORTANT QUESTIONS IN C FOR THE INTERVIEW

BBM 201 DATA STRUCTURES

C-LANGUAGE CURRICULAM

Question 1. Part (a) [2 marks] error: assignment of read-only variable x ( x = 20 tries to modify a constant) Part (b) [1 mark]

Principles of Computer Science

C PROGRAMMING LANGUAGE. POINTERS, ARRAYS, OPERATORS AND LOOP. CAAM 519, CHAPTER5

Recursion. What is Recursion? Simple Example. Repeatedly Reduce the Problem Into Smaller Problems to Solve the Big Problem

Pointers. Addresses in Memory. Exam 1 on July 18, :00-11:40am

Arrays and Memory Management

Fundamental of Programming (C)

CS61, Fall 2012 Section 2 Notes

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

Recursion. CSCI 112: Programming in C

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

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

Name :. Roll No. :... Invigilator s Signature : INTRODUCTION TO PROGRAMMING. Time Allotted : 3 Hours Full Marks : 70

Fundamentals of Programming & Procedural Programming

PDF created with pdffactory Pro trial version Recursion

ALGORITHM 2-1 Solution for Exercise 4

C Language Summary (Continued)

Computer Programming

CSE 230 Intermediate Programming in C and C++ Recursion

C programming basics T3-1 -

CSE 351 Midterm - Winter 2015 Solutions

Name Roll No. Section

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

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

Tech Mahindra Placement Paper 1. C-Test Paper. 1. #include. * What is wrong in the following problem. main() { int i,j; j = 10;

Flow Control. CSC215 Lecture

Programming II (CS300)

COS 126 General Computer Science Spring Written Exam 1

Two s Complement Review. Two s Complement Review. Agenda. Agenda 6/21/2011

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

M.EC201 Programming language

Programming Language B

Introduction to Programming

Decision Making and Loops

Transcription:

Note to Students: This file contains sample solutions to the term test. Please read the solutions carefully. Make sure that you understand why the solutions given here are correct, that you understand the mistakes that you made (if any), and that you understand why your mistakes were mistakes. For all remarking requests, please refer to the portal for instructions. Question 1. [20 marks] Please provide short answers to each one of the following sub-questions. Part (a) [2 marks] Assume that the number of bits allocated in a computer for short int is 16 bits. The range of short integers that can be stored in this computer is therefore [-32768, 32767]. Will a problem occur if this code snippet is executed in this computer? If so, what is the problem called and why is it caused? short int i=32768; printf("i is %d\n", i); This is an integer overflow problem. short int is only allocated 16 bits in the computer. The value 32768 requires more than 16 bits of storage space. Part (b) [3 marks] What will the output of the following code snippet be? Why? What is a safer alternate implementation? float a=4.5, c; int b=3, d; c=a+b; d=a+b; printf("c is %f\n", c); printf("c is %d\n", d); The output is 7.500000 for the first print statement and 7 for the second print statement. This is due to implicit typecasting. A proper way to write this code will be to explicitly typecast (i.e. c=a+(float)b; and d=(int)a+b;) Part (c) [2 marks] Write the logical expression required to evaluate whether a double variable i falls within the range n=1.2 and m=3.4. This range should include the values n=1.2 and m=3.4. Also, write the expression that evaluates whether i does not fall in this range. Within the range: i>=1.2 && i<=3.4. Complement: i<1.2 i>3.4 Page 1 of 8

Part (d) [2 marks] Re-write the equivalent code snippet of the following for loop in terms of a while loop. for(i=0; i<n; i++) printf("%d\n", i); i=0; while(i<n) printf("%d\n", i); i++; Part (e) [2 marks] What problem can occur in the following function (assume that i is a positive number)? How can this be fixed? int func(int i) if(i<1) return 1; else func(i+1); Infinite regression. The recursive calls do not result in moving towards the base case. This can be fixed by changing i to approach closer and closer to 1 at every recursive call. Part (f) [3 marks] What problem can occur in the following code snippet? What are the two ways this problem be avoided? int * i = (int *)malloc(sizeof(int)); int j=2; i=&j; Inaccessible memory location. Ensure that another pointer holds the memory address of the dynamically allocated space or the dynamically allocated space must be freed. Part (g) [2 marks] What problem can occur in the following code snippet? How can this problem be avoided? int * i = (int *)malloc(sizeof(int)); free(i); *i=3; Page 2 of 8

Dangling pointer. The freed pointer should be set to NULL. Part (h) [4 marks] What is the issue in this function implementation? Use dynamic allocation and write a correct implementation of this function. int * func(void) int i, *j; j=&i; *j=0; return j; Returning the memory address of a local variable declared within a function is incorrect as that variable will cease to exist when the function ends. int * func(void) int i, *j; j=(int*)malloc(sizeof(int)); *j=0; return j; Page 3 of 8

Question 2. [20 marks] In the following, you will be asked to provide a series of function implementations andor diagrams. Part (a) [5 marks] Implement the function void initarray(int ** a, int n) which dynamically allocates an array of size n for storing n integer elements. Each element in the array should be set to 0. Double pointer a, passed as an argument to the function, should be set to point to the dynamically allocated array. void initarray(int **a, int n) int i; int *array=(int*)malloc(n*sizeof(int)); for (i=0; i<n; i++) array[i]=0; *a=array; Part (b) [5 marks] Complete the main function below to call the function you have defined above and print the contents of the array. Pass the single pointer a declared in the main function to the initarray function in a suitable form. Ensure that there will be no memory leaks in your implementation. int main() int *a; int i; initarray(&a, 5); for (i=0; i<5; i++) printf("%d\n", a[i]); free(a); Part (c) [6 marks] Implement the function int factorial(int n) using recursion. A factorial, n!, is the multiplication of a decreasing sequence of consecutive integers ranging from n to 1 (i.e. n! = n (n 1)... 1). For instance, 4! = 4 3 2 1. int factorial(int n) int res; if (n==1) res=1; Page 4 of 8

else res=factorial(n-1)*n; return res; Page 5 of 8

Part (d) [4 marks] Draw the call tree resulting from the recursive implementation of the factorial function for the specific function call factorial(4). factorial(4) 24 factorial(3)*4 6*4=24 factorial(2)*3 2*3=6 factorial(1)*2 1 Page 6 of 8

Question 3. [10 marks] You will implement the function int iispalidrome(int * a, int n) in an iterative manner (i.e. without using recursion). This function will check whether an array of numbers represents a palindrome or not. A palindrome is a sequence of symbols that are read the same way in the forward and backward directions. For instance, the number sequence 1-2-4-2-1 is a palindrome as when you read this sequence in the forward direction you will read it as 1-2-4-2-1 and when you read it backwards you will also read this as 1-2-4-2-1. Similarly, the sequence 1-2-2-1 is also a palindrome. Variables a and n are passed as arguments to the function. a is an array containing n elements. If the sequence of numbers in the array is a palindrome, then the function should return 1. Otherwise, it should return 0. int iispalidrome(int * a, int n) int res=1; for (int i=0; i<n2; i++) if (a[i]!=a[n-1-i]) res=0; return res; Page 7 of 8

CSC 190 H1S Test #1 Winter 2015 Question 4. [15 marks] Finding the minimum element in an array problem. Part (a) [10 marks] In this question, you will implement the recursive function int rfindmin(int * a, int i, int n, int *index). This function will recursively find the minimum element in an array, a, containing unique integer elements and return this value. The index at which this minimum element resides must also be indirectly passed to the calling function via the pointer variable index. Variables a, n and index are passed as arguments to the function. i and n are integers whose values will be 0 i n size where size is the size of the array a. For instance, in the array 5, 4, 7, 1, 3, the minimum element is 1 and this element resides at index 3. An initial recursive call for this example can be rfindmin(a, 0, 4, index) where a is the array containing the elements and index is a single integer pointer variable. int rfindmin(int * a, int i, int n, int *index) int res; if (i==n) res=a[n]; *index=n; else res=rfindmin(a, i+1, n, index); if(a[i]<res) res=a[i]; *index=i; return res; Part (b) [5 marks] Please provide the call tree diagram for the specific instance rfindmin(a, 0, 4, index) where a is an array containing integer elements 5, 4, 7, 1, 3 and index is a single pointer variable. rfindmin(a,0,4,index): res=1, *index=3 rfindmin(a,1,4,index): res=1, *index=3 rfindmin(a,2,4,index): res=1, *index=3 rfindmin(a,3,4,index): res=1, *index=3 rfindmin(a,4,4,index): res=3, *index=4 Page 8 of 8