0% found this document useful (0 votes)
32 views6 pages

L-L/T - l/CSE Date: 17/08/2017: - Section - A Four Three

The document contains instructions for a C programming exam. It includes 4 questions with multiple parts each. Question 1 involves writing programs to: (1) print integers with commas, (2) map letters to subsequent letters in the alphabet, and (3) compare fractional numbers. Question 2 involves writing programs to: (1) calculate a series, (2) print a pyramid pattern, and (3) check if a number is a power of 5. Question 3 involves writing functions for: (1) union and intersection of arrays, and (2) finding GCD of array elements. Question 4 involves writing functions using only bitwise and arithmetic operators to: (1) check equality/inequality, (2) copy bits,

Uploaded by

FuadAlAlam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views6 pages

L-L/T - l/CSE Date: 17/08/2017: - Section - A Four Three

The document contains instructions for a C programming exam. It includes 4 questions with multiple parts each. Question 1 involves writing programs to: (1) print integers with commas, (2) map letters to subsequent letters in the alphabet, and (3) compare fractional numbers. Question 2 involves writing programs to: (1) calculate a series, (2) print a pyramid pattern, and (3) check if a number is a power of 5. Question 3 involves writing functions for: (1) union and intersection of arrays, and (2) finding GCD of array elements. Question 4 involves writing functions using only bitwise and arithmetic operators to: (1) check equality/inequality, (2) copy bits,

Uploaded by

FuadAlAlam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

L-l/T-l/CSE Date: 17/08/2017

BANGLADESH UNIVERSITY OF ENGINEERING AND TECHNOLOGY, DHAKA


L-l/T -1 B. Sc. Engineering Examinations 2015-2016

Sub: CSE 101 (Structural Programming Language)


Full Marks : 210 Time : 3 Hours
The figures in the margin indicate full marks.
USE SEPARATE SCRIPTS FOR EACH SECTION
----------------_ _ _ _--_ _ __ _--_._-_
.. - _ __ .._--
SECTION -A
There are FOUR questions in this Section. Answer any THREE.
Please read carefully, some questions might have additional restrictions such as not allowing
the use of any library functions except the I/O related ones.

1. (a) Is the following code fragment valid? What will be the output if so? State your reason.
int a=2, b=5, c ;
c=a+++b;

printf("%d, %d, %d, a, b, c) ; (5)


(b) As a human being we prefer to read integers in a comma separated readable format
where a comma is placed after every three digits from the right. Unfortunately print in C
cannot print it in that way, you need to write your own. Write down a program that will
take an integer (positive or negative) as input and will print it in a comma separated

~~rmat. See the following example: (15)

--sample fnputs - u_ -Sample outputs .


-1234567890 -1,234,567,890
-123456 -123,456
-12345 -12,345
-1000 -1,000
-999 -999
-1 -1
00
1 1
999 999
1000 1,000
12345 12,345
123456 123,456 .
1234567890 1,234,567,890

(c) Write down a program that will take an English letter in upper case as input and will
print two letters after it in the alphabet in lower case. For example, if the user enters D the
output will be f. If the input is Z the output will be b. You are not allowed to use any if-

else, switch-case or ternary operator to solve the problem. (10)


(d) Show a code segment to determine the largest of three fractional numbers a, b, and c.
You are only allowed to use conditional operators as many as needed to solve this

problem (no if-else, no switch-case). (5)


Contd P/2
=2=

CSE 101
2. (a) Write down a program that will take a float x and integer n as input and will calculate
the value of the following series. You are not allowed to use any library function(s)

except the input output related ones, (12)

x2 x3 x4 xn
x--+--_ ...--
21 31 41 nl
(b) Write down a program that will take a positive integer n as input and display a

pyramid pattern. The pattern that needs to be displayed for n = 5 is shown below. (12)

*
* II *
I' ,-------*11'11
II *

* """ 1\ II II *
. * * * ~"'" * * ••.

(c) Write down a function with the following prototype: (11)


int isPowerOfFive(int n) ;
The function checks whether the number n passed as parameter is a power of 5 or not. If
n can be expressed as power of 5 the function returns I; otherwise it returns O. You are
not allowed to use any library functions. Few sample calls and return values are given in
the following table:

. Function call Return value


isPowerOfF i ve(5) 1
isPowerOfFi ve(lO) 0
isPowerOfFi ve(25) 1
isPowerOfF i ve(33) . 0
I

3. ! (a) Giv~n tv:o unsorted ~rrays-Aand. B with size ~ and n, ~rite down two ot~er functions voia1iJ
. umon(lOt A[], lnt B[]' lOt C[], lnt rnJ lnt n) to in:tplement, C = AU Band;
: void intersect(int A[], int B[]' int D[]' int rnJ int n).toimplement D =j (2x9=18)
A n B. As an example, suppose A"'d= {7, 1,5, 2, 3, 6, I} and B = {3, ~)j, .20-';1,
8}. Then C={7, 1,5, :
2,3,6,8,20}, and,D = P, 3, 6}. -",,"

(b) Suppose an array has n positive integers (n ~ 2). Show a code segment to find QCD of

all numbers in the array. (8)


(c) Discuss the problem and solution of the following parameterized macros: (3x3=9)

(i) #define square(x) .'x:i:x'


. (ii) #define' swap(tJxJy) t = x; x'= Y; Y = t;
(iii) #define swap(tJxJy) {t' = x; x = Y; Y = t;}
.
-~-- .

Contd , .. , P/3
=3=

CSE 101
4. (a) Write down the following functions. You are not allowed to use logical operators,
relational operators, if-else or loop. You can only use bitwise operators and arithmetic

operators as needed . (5x5=25)

(i) . int isNotEqual(int x, intv) returns a if x is equal to V and nonzero if x is not equal to V,
(ii) int coPVABit(int x, int n) copies nth bit to all bit positions, where 1::;;n S 32.
coPVABit(S,3) returns -1 (Le., 1 in all bit positions)
coPVABit(S,2) returns a (Le., a in all bit positions)
(iii) int isNegative(int x) returns a if x is positive and 1 otherwise
(iv) int isSm.aller(int x, int V) returns a if x is not smaller than V and nonzero otherwise . I
.. (v) .. int invertBits(int x, int p, int n) inverts n bits starting from position p of x and returns.,
Assume that, 1 ~ n ::;;32 and a ~ p ::;;31

(b) Write down a function that will take an integer x as parameter and returns after
interchanging all pairs of consecutive bits. Assume the size of integers is 32 bits. Here is

an example: (10)
x = 1011 1111 1110 1010 1001 0000 1000 0001
return value = 0111 1111 1101 0101 0110 0000 0100 0010

SECTION -B
There are FOUR questions in this Section. Answer any THREE questions.

5. (a) Consider the following declaration: (10)


int p[2][3][2] ={I, 2, 3,4, 5, 6, 7, 8, 9, 10, 11, 12}; the first element of p located at
address 12040 and an integer is represented by 2 bytes, then show the addresses of each
of the integers in the list. Write down the values of the following expressions:
(i) **P (ii) *(*(*p+ 1)+2 (iii) *(**(p+2)) (iv) *(*p+ 1)+ 1

(b) Explain the declaration of the following function prototype: (8)


char (*(*funct[])(int(*a) [])) [];
(c) Re-declare the following using pointer and malloc() function. Do not use [ ] operator.

double A[1 0][20][30]; (7)


(d) Write down a C function that will return the index of first occurrence of a substring in
a string. The string and substring will be passed as the first and the second argument of
the function respectively. You must write down your program codes using pointer and

avoiding array. The prototype of the function is as follow: (10)


int findSubStrlndex (const char *str, const char *substr);
For example findSubStrlndex("This is a pen.", "is") will return 2, while
findSubStrlndex("This is a pen.", "ball") will return -1.
Contd P/4
=4=

CSE 101
6. (b) A hall management system records information of all students who are either resident
or attached to that hall. The record has the following fields:
Fields . Description
name Name of the student
stdID Student number
DoB Death of birth ofthe student
enrol Date Date of admission into the hall
Room No Room number of the residential student
GPA An alTay contains semester-wise GPA

(a) Write down a suitable data structure to store the above mentioned information of all

students associated with the hall. (10)


(b) Assume that studentList is a pointer to the list of all students as declared below:
Student *StudnetList;
Write down a C function to list all the students (containing name, stdID and roomNo)

whose CGPA is less than 3.0 and occupied seats in the hall. (10)
Write a C function that will retrieve information of all students stored in the hard-disk
into the variable studentList as declared in (b). At the end of the program, you are
required to replace the information stored in the hard-disk with the information recorded
in studentList, which is referred as save operation. Write another C function for save
operation. Both of the function must be written in such a way that a student's information
is considered as a single entity. (Hints: You are not allowed to use fscanO or fprintO

functions in your program.) (8+7)


7. (a) Define three two dimensional, 100 x 100 integer arrays. Please note that number of
rows and columns are same for all three arrays. Prompt for number of rows and columns
and read the values of m and n respectively. Read mxn integers for first two arrays,
perform matrix multiplication and store the results in third array. Print the elements in

third alTay. (10)


(b) Write a C function to calculate the transpose of a square matrix. The function

prototype is as follows: (10)


void transpose(int *Iist, int n); Illist represents the matrix
II n is the number of rows/columns
(c) A gray scale image is represented by a 2-dimensinal integer array where value of each.
pixel represented by a n~mber ranges from 0 to 255. A sample gray image is given

below: (15)
231 187 178 194 203 190
202 195 198 195 204 192
214 197 179 196 200 197
230 190 180 198 201 187
224 219 193 200 201 186
231 123 189 201 203 197
Contd P/f

.~. l
l~
• r
I

= 5=

CSE 101
eoutd .eo Q. No.7(c)
An error matrix in calculated as follows. At first average of all the pixels are calculated
and then error of each pixel is calculated by subtracting the average from the pixel value.
Write a C function to calculate error matrix of a given image. Also write a C function to
calculate the error frequency list, which represents how many times each error occurs.
Arrange the list in ascending order on error values.

8. (a) A binary search tree, abbreviated as BST, is a binary tree where all the element in the
left subtree of a node are less than or equal to the element at that node and all the

elements at the right subtree are higher than it. (25)

Figure 8(a)-1: BST

A BST can be stored in a ID array where the root of the tree is stored at index 1. node(i)
represent the element stored in the index i. Left child of node(i) is node(2*i) and the right
child of node(i) is node(2*i+ 1). The value of an element -1 in an index means there is no
element at that index. The BST in the Figure is represented in the following array. For
example, 15 is root and hence it is placed in index 1. Left child of 15 is 9, which is placed
in index 2* 1 = 2 and the right child of 15 is 19 which is placed in index 2* 1+ 1 = 3 and so
on.
Index 0 1 2 3 4 5 6 7 8 9 10 11 12
Value -1 15 9 19 -1 12 17 21 -1 -1 -1 -1 -1

(i) Write a recursive C function that will insert a node in the given tree. If the tree does
not exist, inserting a node means that the done become the root of tree, Use the following
function prototype
void BST _insert( int *node, intx); II element x to be inserted into the BST
II and node is name of array.
(ii) The height of a node of the tree is defined as follows: the height of the root is zero. If
a node is at a height of n, then its left and right child are at the height of n+ 1. Write a
recursive C function that will return the height of a given node. The function prototype is
as follows:
int BST_height(int *node, int x); II element x is the element whose height
I I need to be determined
II and node is the name of the array
If the element x is not present in the tree, then -1 will be return.
Contd P16

=6=

CSE 101
Contd ... Q. No.8

(b) Write down a recursive C function for binary search within a sorted list that returns
the index of the array if the element is found otherwise returns -1. The prototype of the

function is as follows: (10)


int binSearch( int *list, int low, int high, int value);
where list is the name of the sorted array and value is the element to be searched.
Variables low and high represent the searching range.

You might also like