You are on page 1of 10
L-V/T-1/CSE Date : 03/05/2023 BANGLADESH UNIVERSITY OF ENGINEERING AND TECHNOLOGY, DHAKA L-U/T-1_B, Sc. Engineering Examinations 2021-2022 Sub: CSE 101 (Structured Programming Language) Full Marks : 210 Time : 3 Hours ‘The figures in the margin indicate full marks. USE SEPARATE SCRIPTS FOR EACH SECTION SECTIC There are FOUR questions in this section. Answer any THREE. 1. (@) Write two functions that return the ceiling and floor of a fractional number up to n decimal places. The functions ceiling and floor returns the nearest fractional numbers with n decimal places greater than or equal fo and smaller than or equal to the number passed as argument, respectively. Use of any built-in function is not allowed in writing the functions. (10) (b) Write down the output given by the printf functions of the main program shown below: @) void swap(int x, int y, int * count) i fea epiast y i static int i-0; 8 int = x7 xey yo ies veount=i; ) int main() { : int a= 4, y = S,count; swap(a, b, &count) ; printé ("Swapping count ¥4 ¥i $i", a, b, count); count=3; ewap(a, b, count) ; printé ("Swapping count $i ¥i $i", a, b, count); (©) Write program that demonstrates the addition, subtraction, multiplication of two ‘matrices. There must be a function to get the elements of the matrix. The program will ask for the dimension of the matrix. You have to allocate memory dynamically for the matrix of the specified dimension only. Addition, subtraction, multiplication functions will create a new resultant matrix. There must be a function that makes the allocated memory {free for the matrix. There must be a function to print the matrix in a formatted way. (22) Contd CSE 101 2. (@) Explain the file opening modes sopcified by "t","w", "a", "+", "w#" and "at", © (©) The following code is writen for reading first 10 integers from a fle and writing the sum ‘ofthe integer at the end of the file, Make necessary correction inthe code ifthere is any. (0) FILE *fp=fopen(*input.txt", °r")7 int num=0, sum=0; for (int ino; icio; i¢+)( fscanf (fp ,“¥d", énum); ‘suné=num; Eprinté(fp ,*¥d", sum); (©) Write a program that creates a new file containing the content of the text of a file twice. Consider the input text file contains the following line: (10) Dhaka ie the capital of Bangladesh ‘The content of the newly created text file will be as follows: Dhaka is the capital of Bangladesh Dhaka is the capital of Bangladesh (@ You are to take input of the marks obtained by 10 students ofa class of $ subjects. Show the names and total marks ofthe students obtaining the highest and lowest number. ‘You are not allowed to use aray datatype to store the marks and names ofthe stadens Itis expected that file will be used to store the data o solve the problem. (as) 3. (@ Define string data type, How can you implement string datatype in C programming Janguage? - © (b) Take two strings as input, Next, check whether the first string is inside the second one and if yes then output how many times. For example, desh i in Bangladesh once but aa is nana twice ® (© Write a program that takes an integer input in decimal format from keyboard and conver itt in function, (10) (© Write the following functions using pointer manipulation technique: (a2) (@ strepy for copying one string to another of hexadecimal format. Do not use any (i stremp for comparing two strings (Git) concatenating two strings Contd .. CSE 101 4. (@) Write a program to compute summation of the series expressed by dye” + apy! + s+. ag%°, Use minimum number of loops in your program. (10) (b) Describe different ways to declare an array of integers of size 100. @ (©) Consider the following code which uses array and pointers, Find out the output of the program written below. @ int afio); for (int is0;1<10;i++) ali? int *p; pass; for (ie0;icS;i++) printe(*\ntdr, *(pe4)); peas2; for (i#0;1<10;i+4) { printf ("\ntd", *p); Pte (@ Write a function that takes four parameters, The first one is an array, the second one is clement to be inserted, the third one is the index where it will be inserted and the fourth one is the size of the array. The function will insert the element in the specified place. ® (©) Write a program that determines whether an input number x in between | and 100 is a prime number. You are to find this using minimum number of division operations. Explain the correctness of the methodology used. (a0) SECTION -B ‘There are FOUR questions in this section, Answer any THREE. 5, (a) Answer the questions below for the given C program (Listing 1) and the corresponding stack frame (Table 1) ofthe main function: Listing 1: C program for question 5( include 1 2 3 void swap(int *a, int *b) 4 5 Ant temp = +a; 6 a= ob) 1 *b = temp; sy 9 10 void swap_2(int +**a, int «*+b) i 4 12 Ant sstemp = ray B sas +b; 4 +b = temp; Is) 16 Contd PVs CSE 101 Contd ... Q.No. S(a) 17 amt main(y ( 18 int p= 25, q = 50; 19 Ante a = 6p; 20 Ante b= sai 2 Ant +40 = Ga; 2 int sed = Gb; 2B Ant #s0e = ce; 4 Ant «+f = ca) 25 % swap (+se, 46) 2 Prine ("td td 8d 4d\n", p,q, va, sb); 2B Printe ("td td td td $d Ad\n", a, by cy dy ey £17 2 Prine (*td 4d $4 td\n", v0, seve, anf, s4e8); 0 31 swap_2ie, £); 2 PEAnte("d 4d 84 Ad\n", py qe ea, +b) 3 Print ("td 4d td td 4d 4d\n", a, b, cy dy ey £5 4 Print£("td 4d td Rd\n", se, sece, sof, cnel); 35 // Note: Al1 the format specifiers are intentionally in ta 36 // You can safely assume that the 8 bytes pointer values can be converted into # bytes int values 7 return 0; ‘Variable Name | Stack Address (Decimal) B 7000 1004 1008 1016 1024 1032 1040 1048 ‘Table 1: Stack addresses of the variables used in the main Function of the program for question 5(a). () Briefly explain the effects of line 26 and line 31 in the program. i) Write the output of lines 27, 28, and 29 ofthe program. (Gi) Write the output of lines 32, 33, and 34 of the program, Gv) Modify the body of the function swap_2 so that the replacement of line 26 with swap_2(e, 1) will bring the same effect as swap(*e, **f) does. You cannot change any function prototypes. Only write the modified function, You must use dynamic allocation for the temp variable and make sure to not leak memory. Comtd «ose PIS 6 © 6) ® CSE 101 Contd... No.5 (b) Answer the questions below for the given program (Listing 2) Listing 2: C program for question 5(b) ar Hinclude 1 2 3 fdefine cube(a) a+ as a; 4 Hdefine swap(t, x, y) tex; xey, yaty 5 define max(x, y) «> y? x: y; 6 1 8 ant main() ( int a = 10, b = 20; 9 int t = 0; 10 if (a > b) swapit, a, bi; n printf ("$d d\n", a,b); 2 int ¢ = cube(b + 2); 1B print£("8d\n", 0); “4 int max_val = max(att, bt+); 15 printf ("Sd td td\n", a, by maxval); 16 return 0; "7 @ Provide the output of the given program. © Gi) Make necessary corrections in the given program. No need to write the full program. Just write the lines which are corrected. © 6. @ Write the necessary functions so that the given program (Listing 3) prints the total vowels and consonants in the given string "str". You eannot change the main function. Only ite the required functions so that the main function runs correctly. . as Listing 3: C program for question 6(a) 1 Hinelude 3 // you cannot change the main function 4 ant main() ( 5 char «str = "I am confident I will pass this exam! 6 Ant (4callback_ptr_vowel) (char) =. process_char_vowel! 7 Ant (scallback_ptr_cons) (char) = process_char_cons; a Ant total_vowels = count_char_types (str, callback_ptr_vowel) 9 Ant total_cons = count.char_types(str, callback_ptr_cons ve 10 Printf("td\n", total_vowels); nl print¢("ad\n", total_cons); 2 B return 0; 4 Contd .. CSE 101 Contd... 0.No. 6 (©) Write the required function for the given program (Listing 4) so that the main function runs correetly. The required function takes one integer and a string as fixed arguments and then a variable number of arguments. The first argument denotes the total number of lists, and the second argument denotes the format of each list. Note all the lists have the same format, Each list element can be a character, an int, ora string. The format string consists of character 's' which denotes the string element, i' which denotes the integer element, and 'e' which denotes the character element, The function will return the summation of all elements of all lists. The value of the string element is the summation of the ASCII values of all character, (10) 2 B 4 5 16 Listing 4: C program for question 6(b) Winclude #inelude include // you cannot change the main function int maing) int total_sum ~ getTotaisum(2, *sice", "1 am confident", + 2, 'pr, fat, "I will pass", 3, ‘pt, "B'}; // fizst argument "2" means 2 lists // second agreenent "sice" means each list has a total of 4 elenents. The first element is a string, the @ second elenent is an integer, che third element is a character, and the fourth element is a character. // Then a total of 8 arguments are passed as 2 lists © each having 4 elements requiring 8 arguments PEint£("4d\n", total_sun); // prints 2737 total_sun = getTotalsum(2, "ic", 10, ‘A’, 20, at); printf ("$d\n", total_sum); // prints 192 (10 + 65 + 20 +4 37) return 0; d 2 (©) Write the output of the given program (Listing 5). Q@% x4=10) Listing 5: C program for question 6(c) Winclude // size of long double -> 16 bytes 7/ size of float -> 4 bytes 1/ size of int -> 4 bytes // size of char -> 1 byte // size of char pointer -> & bytes typedef union { int i; char «ptr; float £; jexamplel; 7 Jeng double b; 18 char + ¢; 19 )example2; 20 21 typedef struct | 2 long double b; 2B char 3; mw char + cj 25 yexample3; 6 27. typedef struct { 28 char a(10}; 29 Ant ci} 30 char b(10}; 31 Jexamplea; 32 33° int main() { 34 printf ("41d\n", sizeof (example); 35 Print£("S1d\n", sizeof (example2)}; 36 printf ("#1d\n", eizeof(example3)); 37 print£(**1d\n", sizeof (example) ); 38 return 0; 39) 7. (@) Implement the functions ereateAccount, searchAccount, addBalance, ransferBalance, and prinfTransactions in the given program (Listing 6). You cannot change any function prototype. Follow the instructions given in the comments. Properly handle pointer assignments in the structures. (8+4+3+4+6=25) Listing 6: C program for question 7(a) Las finclude Winclude 1 2 3 4 5) define TOTAL_ACCOUNTS_AT_WOST 1000 6 define TOTAL_TRANSACTIONS_AT_MOST 10000 7 8 9 typedef enum ( SAVINGS, 10 ‘CURRENT 11} Aecounttype; 2 13. typedef enum ( 4 WITHDRAW, 15 DEPOSIT, 16 ‘TRANSFER 17} Teansactiontype; 18 19 typedef struct ( 20 int date; 21 Ant month; 2 int yeai ) dates Contd .-..sss.+ PIB CSE 101 Contd... Q. No. 7a) 2s 6 a 28 typedef struct ( Date date; int hour; Ant mins; int seconds; ) Time; typedef struct { ‘char +name; int accountNunber; int balance; AccountType type: Date dob; Time openingTime; ) Account; typedef struct ( Account *sentFrom Account. *sentTo; Time time; int amount; Transact ionType type; } Transaction; Accounts al1Accounts [TOTAL_ACCOUNTS_AT_MOST} 5 Transactions a11Traneact ions [TOTAL_TRANSACTIONS_AT_MOST) ; Time getCurrentTime(); // exeate an account and add the account to allAccounts array // set account opening time by calling the function getCurrent Time Account *createAccount (char *name, int accountNumber, int yo AnitdalBalance, AccountType type, Date dob)? // search for the account number given. Account +searchAccount (int accountNumber) ; // add balance to an accountNunber. Add a transaction to thew al1Trasanétions array void addBalance(int account, Ant amount) ; // withdzaw balance. Add a transaction to the aliTrasanctions array void withdrawBalance(int account, int amount); // transfer balance from one account to another account. Add ‘2 transaction to the allTrasanctions array void transferBalance (int from, int to, int amount); // print all transactions of an account void printTransactions (int account); ee CSE 101 Contd... 0. No.7 (©) Answer the questions below for the given program (Listing 7): (i) Draw the recursive tree for the function call Gd) Write the output of the given program. Listing 7: C program for question 7(0) Hinelude Jong fib(int nun) ( 4¢(num <= 3) return 1; Jong vall = fib(nun-1); printe ("81d ") vail); long val2 = fib(nun-2); printe ("tid ", val2); Jong val3 = £ib(nun-3) printé ("sid ", val3); return vall + val2 + val3; ) Ant main() ( Jong ret = £ib(6); printé(*"\ntld", ret); en ei ee aN ine 15. 6) © 8, Answer the questions below for the given program (Listing 8): 1 2 3 4 5 6 7 8 9 10 n 2 3 “4 13 16 7 18 9 20 21 2 B 4 5 6 a 2B a Listing 8: C program for question 8 include // print the bits of integer x (assue little endian machine) void printBits (int x); // set the bit at position p int setBit (int x, int p); // clear the bit at position p int clearBit (int x, int p); // Snvert the bit at position p int invertBit (int x, int p); // set left n bits starting from position p int setBits(int x, int p, int n); // clear lett n bits sterting from position p int clearBits(int x, int p, int n); // invert left n bits starting from position p int invertBits(int x, int p, int n); 7 right circular shift by n bits Ant rightCircularshift (int x, int n); // lett circular shift by n bits Ant leftcircularshift (int x, int m); CSE 101 Contd ... Q. No. 8 30 int setbefaultsystemstatus (int x) 3 32 // x has a total of 32 bits 33 // the first 16 bits are used for storing the os status uM 7// the next 12 bits are used for storing the total number + of running processes 35 // the next 3 bits are used for the error status 36 7/ the last bit is unused 0 3B // setting the os status to Oxff11 (1111111100010001) 39 x = clearBits(x, 0, 16); 40 % = setBit (x, 0); 4 x = setBit (x, 4); a2 x = setBits(x, 8, 8)7 a 44 // setting the total number of processes to 0xf43 + (211102000011) 45 x = clearBits(x, 16, 12); 46 x = setBits(x, 16, 2); a7 x = sevBit (x, 22); 4B x= setBits(x, 24, 4)7 49 50 // setting the error status to 0x6 (110) Sl “y= clearBits(x, 28, 3) 32 x = setBits(x, 29, 2)7 33 “1 54 xeturn x; sy 56 S37 dnt mainwy Bot 59 Ant x; 60 // x has a total of 32 bits 6 // the first 16 bits are used for storing the os status 2 // the next 12 bits are used for storing the total number of running processes 8 // che next 3 bits are used for the error status 64 // the last bit is unused 65 x = setDefaultsystemstatus (x); 66 PeintBits (x)? 5 oy (@) Implement the following functions: (You cannot change any function prototypes and follow the instructions given in the comments. You must use bit manipulation techniques, ‘and cannot use any loops and cannot include any libraries). (@) printBits @ i) setBit, clearBit, and invertBit (2x3=6) ii) setBits, clearBits, and invertBits (Bx3=9) jv) rightCircularShift, and leftCircularShift (343-6) (®) Write a new program where you must implement the functionalities of the setDefaultSystemStatus function using Bit Field Structure. Note that you cannot use any bit manipulation technique in this program, :, (10)

You might also like