Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : Mid-Semester Examination

Similar documents
Name Roll No. Section

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn : End-Semester Examination

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

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

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

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

Indian Institute of Technology, Kharagpur

Lecture 6 Sorting and Searching

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Design and Analysis of Algorithms

CS13002 Programming and Data Structures, Spring 2005

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

CS13002 Programming and Data Structures, Spring 2005

Programming & Data Structure: CS Section - 1/A DO NOT POWER ON THE MACHINE

THE UNIVERSITY OF WESTERN AUSTRALIA

Programming and Data Structures Mid-Semester - Solutions to Sample Questions Dept. of Computer Science and Engg. IIT Kharagpur Spring

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

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

Goals of this Lecture

COS 126 General Computer Science Fall Midterm 1

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

COMP1917 Computing 1 Written Exam Sample Questions

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

ECE 15 Fall 15 Final Solutions

LECTURE 17. Array Searching and Sorting

1. Basics 1. Write a program to add any two-given integer. Algorithm Code 2. Write a program to calculate the volume of a given sphere Formula Code

THE FUNDAMENTAL DATA TYPES

Midterm Exam. CSCI 2132: Software Development. March 4, Marks. Question 1 (10) Question 2 (10) Question 3 (10) Question 4 (10) Question 5 (5)

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

Number Systems Prof. Indranil Sen Gupta Dept. of Computer Science & Engg. Indian Institute of Technology Kharagpur Number Representation

Subject: Fundamental of Computer Programming 2068

CSCI 2132 Software Development. Lecture 19: Generating Permutations

More Recursive Programming

Binary Representation. Decimal Representation. Hexadecimal Representation. Binary to Hexadecimal

Decimal Representation

EECE.2160: ECE Application Programming Spring 2018

PROGRAMMING IN C LAB MANUAL FOR DIPLOMA IN ECE/EEE

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

ESC101 : Fundamental of Computing

PDS: CS Computer Sc & Engg: IIT Kharagpur 1. for Statement

8/5/10 TODAY'S OUTLINE. Recursion COMP 10 EXPLORING COMPUTER SCIENCE. Revisit search and sorting using recursion. Recursion WHAT DOES THIS CODE DO?

Arrays and Applications

ESC101N: Fundamentals of Computing End-sem st semester

Department of Computer Science Purdue University, West Lafayette

LAB 1 Binary Numbers/ Introduction to C. Rajesh Rajamani. ME 4231 Department of Mechanical Engineering University Of Minnesota

Question Bank (SPA SEM II)

Unit 1 - Arrays. 1 What is an array? Explain with Example. What are the advantages of using an array?

I. Algorithms. examples of cubic, quadratic, NlogN, linear, logarithmic, and constant

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Quicksort. Part 3: Analyzing the Running Time

Dalhousie University CSCI 2132 Software Development Winter 2018 Midterm Examination II March 12 15:37-16:24

For questions 4 through 7, select the value assigned to the relevant variable, given the declarations: 3) ) This is not allowed

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

Structured programming

(2 7 *0) + (2 6 *1) + (2 5 *1) + (2 4 *0) + (2 3 *1) + (2 2 *1)+(2 1 *0)+(2 0 *1)

School of Computer Science Introduction to Algorithms and Programming Winter Midterm Examination # 1 Wednesday, February 11, 2015

Structured programming. Exercises 3

Pointers. Pointer Variables. Chapter 11. Pointer Variables. Pointer Variables. Pointer Variables. Declaring Pointer Variables

Programming and Data Structures

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

Searching Elements in an Array: Linear and Binary Search. Spring Semester 2007 Programming and Data Structure 1

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Conditional Statement

Sorting & Searching. Hours: 10. Marks: 16

ECE 2020B Fundamentals of Digital Design Spring problems, 6 pages Exam Two Solutions 26 February 2014

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Variables Data types Variable I/O. C introduction. Variables. Variables 1 / 14

Dept. of CSE, IIT KGP

Fundamental of Programming (C)

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

COS 126 General Computer Science Spring Written Exam 1

Algorithm Analysis. Spring Semester 2007 Programming and Data Structure 1

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

Chapter 11 Introduction to Programming in C

Q. 1 What will be the output of the following program? Justify your answer. [4] #include <stdio.h> main(){ int i=4, a[5]={1,2,3,4,5};

Procedural programming with C

Department of Computer Science & Engineering Indian Institute of Technology Kharagpur. Practice Sheet #07. Topic: Pointer in C Date:

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Stamp / Signature of the Invigilator

INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR

UNIVERSITY OF TORONTO FACULTY OF APPLIED SCIENCE AND ENGINEERING

Computer Programing. for Physicists [SCPY204] Class 02: 25 Jan 2018

Write a C program using arrays and structure

One Dimension Arrays 1

g(n) time to computer answer directly from small inputs. f(n) time for dividing P and combining solution to sub problems

only in the space provided. Do the rough work in the space provided for it. The question paper has total 12 pages.

Data Structure & Algorithms Laboratory Manual (CS 392)

DEPARTMENT OF ELECTRICAL AND ELECTRONIC ENGINEERING PROGRAMMING TECHNIQUES I EE271

16.216: ECE Application Programming Fall 2015 Exam 1 Solution

Subject: PIC Chapter 2.

COS 126 General Computer Science Spring Midterm 1

SOFTWARE Ph.D. Qualifying Exam Fall 2017

Programming Language A

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

ECE 15 Fall 15 Midterm Solutions

Basic Assignment and Arithmetic Operators

Fundamentals of Programming Session 15

Sample Problems for Quiz # 2

Transcription:

Indian Institute of Technology Kharagpur Programming and Data Structures (CS10001) Autumn 2017-18: Mid-Semester Examination Time: 2 Hours Full Marks: 60 INSTRUCTIONS 1. Answer ALL questions 2. Please write the answers either within the boxes provided or on the blank lines to be filled up. Any answer written elsewhere will not be evaluated. 3. You may use the last two blank pages for your rough works. Q.1. Answer the following questions as directed. a) What will get displayed when the following program is executed? [1] int x = 2, y = 17, result = 5; result -= x/5 13 y/3 x; printf( result=%d\n, result); result=5 b) What will get displayed when the following program is executed? [1] int x = -5, y = 10; if (x > y) x = 1; else if (y < 0) x = x (-1); else x = 2 x; printf( x=%d\n, x); x=-10 c) What is the 8-bit two s complement representation of the decimal number 37? [2] 11011011 1 of 8

d) What will get displayed when the following program is executed? [1] char a = 'a'; while ((a > 'a') && (a <= 'c')) a++; printf( %c\n, a); a e) What will get displayed when the following program is executed? [2] int sum = 1, index = 9; do { index = index - 1; sum = 2 sum; while (index > 9); printf( sum=%d, index=%d\n, sum, index); sum=2, index=8 f) What value will the following function return when called as recur(3)? [2] int recur(int data) { if (data > 2) return (recur(data - 1) - recur(data - 2)); else return 1; 0 g) What value will the following function return when called as g(1024)? [1] int g(int n) { if (n < 2) return n; return g(n/2); 1 h) What is the binary number corresponding to the hexadecimal number C5.75? [2] 11000101.01110101 2 of 8

i) Consider the program segment given below to read a letter from a..z and A..Z from the keyboard and convert it to uppercase if not already so. It is assumed that the user will only input a character from a..z and A..Z. Fill up the missing line with a single C expression so that the variable ch will contain the character in uppercase. Do not use any library functions. [2] char ch; ch = getchar(); ch = (ch>= A )&&(ch<= Z )?ch: A +ch- a ; j) The following program segment is supposed to check whether the values stored by three integer variables a, b, and c are in ascending order. However, it contains an error. Encircle the part of the program that contains the error and write only that part corrected. [1] if (a < b < c) printf( Numbers in ascending order \n ); else printf( Not in ascending order\n ); ((a<b) && (b<c)) Q.2. Answer the following questions as directed. a) What will get displayed when the following program is executed? [2] int i; for (i = 1; i = -1; i++) if (i < 5) break; printf( %d\n, i); -1 b) What will get displayed when the following program is executed? [2] void increment(int i) { i++; int i = 0, j = 0; while (i++ < 10) increment(j); printf( i=%d, j=%d\n, i, j); 3 of 8 i=11, j=0

c) What will get displayed when the following program is executed? [2] float j = 1.0, i = 2.0; int n = 0; while (i/j > 0.05) { j = j + j; n++; printf( n=%d, j=%f\n, n, j); n=6, j=64.000000 d) What will get displayed when the following function is called as f(2, 8)? [2] int f(int x, int y) { int sum = 0; y--; if (x == 0) else { printf( %d :, x); sum = y + f(x 1, y); printf ( %d :, sum); return sum; 2 : 1 : 6 : 13 : e) What will get displayed when the following program is executed? [2] int sum = 0, i = 3; while (i < 100) { sum = sum + i; i = i + 3; printf( sum=%d, i=%d\n, sum, i); sum=1683, i=102 4 of 8

Q.3. a) A number is said to be perfect if it is the sum of all its factors (except itself). For example, 6 has factors 1, 2, 3 and 1+2+3 = 6, hence it is perfect. Also, 28 = 1+2+4+7+14 is perfect. In the following function checkperfect fill up the missing lines so that it returns 1 if n is a perfect number and 0 if n is not a perfect number. [2 + 2] int checkperfect(int n) { int i, sum = 0; for (i = 1; i < n; i++) { if (n % i == 0 ) sum += i; return (sum == n ); b) The following function strequal takes two strings S1 and S2 as parameters. Fill up the missing lines in the function so that it returns 1 if the two strings are the same, 0 otherwise. [1 + 2 + 2] int strequal(char S1[], char S2[]) { int i = 0; / Go on until the end of either of the strings / while (s1[i]!= '\0' && s2[i]!= \0 ) { if (S1[i]!= S2[i]) i++; if (s1[i] == '\0' && s2[i] =='\0') return 1; else 5 of 8

Q.4. a) The following recursive function find_power should return x n when called as find_power(x,n), n being a non-negative integer. Fill up the missing lines in the function so that it returns x n. [1 + 1 + 2] float find_power(float x, int n) { if (n == 0 ) else return 1; return x*find_power(x,n-1); b) Fill up the missing lines in the following program so that it will display the sum of the elements of the array A when executed. [2 + 2] int i, n, k = 0, A[10], lim; printf("enter number of elements "); scanf("%d", &n); printf("enter the elements "); for (i = 0; i < n; i++) scanf("%d", &A[i]); for (i = 0, lim = n/2; i < lim; i++) { / Accumulate Sum / k = k + A[i]+A[n-i-1]; if (lim <(n-lim)) / if middle element left out / k = k + A[i]; printf("%d\n", k); 6 of 8

Q.5. a) The following program is supposed to insert a new integer value x into an already sorted (in ascending order) array A containing n distinct integers. You can assume that x does not already exist in A, and there is space available to insert x in A. For example, assume that n is 10, and A has the elements 10, 20, 30, 40, 60, 70, 80, 90, 100, 110, and x is 56. After insertion of x, the array would become 10, 20, 30, 40, 56, 60, 70, 80, 90, 100, 110, and n would be 11. Fill up the missing lines in the program so that the program inserts x in the sorted array A. [2 + 2 + 2 + 2] int main(){ int x, i = 0, j, n, A[100]; scanf("%d%d", &n, &x); for (j = 0; j < n; j++) scanf("%d", &A[j]); while (x > A[i] && i < n) i++; / find position after which to insert / for (j=n; j>= i+1; j--) / make space for inserting x / n++; A[j] = A[j-1]; A[i] = x; / insert the element at the required place / for (i = 0; i < n; i++) printf("%d ", A[i]); b) The following recursive function reverse takes as parameters an integer array A and two other integers leftindex and rightindex which are indices of A. After the function returns, only the part of the array from leftindex to rightindex (including both) should be reversed if leftindex < rightindex. For example, if the elements of the array A are 1, 2, 3, 4, 5, 6, 7, and the function call reverse(a,2,6) is made, then on return, the array A will contain 1, 2, 7, 6, 5, 4, 3 (i.e., A[0] and A[1] remain unchanged, and A[2] to A[6] get reversed). Fill up the missing lines in the function so that it reverses the part of the array A between leftindex and rightindex. [1 + 3] void reverse(int A[], int leftindex, int rightindex) { int temp; if (leftindex < rightindex) { temp = A[leftIndex]; A[leftIndex] = A[rightIndex]; A[rightIndex] = temp; reverse(a, leftindex+1, rightindex-1); 7 of 8

Q.6. The function closest given below takes as parameters an integer array A, the number of elements n in A, and an integer val. Assume that all integers in the array A are distinct and A is already sorted in ascending order. The function returns the index of the element in A with minimum absolute difference with val (i.e., it returns the index i such that A[i] val is minimum). If more than one element has the same minimum absolute difference with val, then it returns the smallest index. For example, if A contains the elements 10, 13, 15, 19, 110 and val is 18, the function returns 3, which is the index of 19 (as 19 18 is the minimum). However, if val is 14, it returns 1 (as both 15 14 and 13 14 are the minimum, 13 occurs at index 1 and 15 at index 2, and 1 is the smaller index). Fill up the missing lines in the function so that it does the above. [2 + 2 + 2] int closest(int A[], int n, int val) { int index, i; if (val < A[0]) / smaller than the smallest element / index = 0; else if (val > A[n-1]) / larger than the largest element / index = n - 1; else { / find the elements closest to val / for (i = 0; A[i]<val ; i++) ; if ((A[i]-val)<(val-A[i-1])) / if current element closest / index = i; else index= i-1 ; / set index to the closest element / return index; --- The End--- 8 of 8