

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Computer science-2nd year course
Typology: Quizzes
1 / 2
This page cannot be seen from the preview
Don't miss anything!


Birla Institute of Technology & Science, Pilani Second Semester 2015-2016, CS F111 Computer Programming Mid Semester Test (Open Book)
Date: March 13, 2016 Time: 11.00 - 12.30 PM Max. Marks: 75 [ NOTE: Answer all parts of a question sequentially ]
Q1. Attempt the following ( sequentially ) for two numbers Num1 = (-127) 10 and Num2 = (127) 8 [ 4x2 + 4 = 12M ] [ NOTE : There is no interdependency among the following parts, i.e., for each part, take Num1 = (-127) 10 and Num2 = (127) 8 ]. a) Assuming 16-bit 2's complement number representation, convert Num1 and Num2 to Hexadecimal. b) Assuming 15-bit 2's complement number representation, find Num3 (in octal representation), where Num3 = Num1 - Num2. Here, you should clearly show the steps for finding Num. c) Assuming 9-bit sign magnitude number representation, convert Num2 to base-5 representation. d) Assuming 1's complement representation, represent Num1 in binary using minimum number of bits. e) For the following equation, what is the highest value of Num3 (in decimal) so that while calculating Num no overflow occurs. Assume 8-bit 2's complement number representation here. Num4 = (Num1 XOR Num2) + Num Q2. Attempt the following sequentially [ 3+2+2 = 7M ] a) Write the decimal equivalent for the following IEEE floating point number: 1 01111101 10000000000000000000000 b) Write a single UNIX command to compile the C program 'first.c' and store the executable in file 'firstExe'. c) Give a single UNIX command to remove a directory (named csf111 ) in the current folder if it is not empty.
Q3. Attempt the following five parts sequentially: [ 6x3 = 18M ]
(a) What is the output of the following? For float, consider a precision of 2 digits. int main() {
int a=25, b=7, c=15, d; float m=12.5, x, y; d = a + 3 / b * c; x = (float) (a / b); y = m * (b / c); c = a + b / c; printf ("%d %d %f %f", c, d, x, y); return 0; }
(d) What is the output of the following?
int main() { int i=-2, j=3, k=0, m; m = ++i - ++j + ++k; printf("%d, %d\n", i, j); printf("%d, %d ",k, m); return 0; }
(b) In the following program, replace printf statement with an if-else construct to produce the same output. int main() { int str[] = {1,2,3,4,5}; int a; scanf ("%d",&a); printf(a>10?"CP\n":"%d\n",str[4]); return 0; }
(c) How many times the condition in the while loop (i.e. j <= 255) will be tested? int main() { char j=1; while(j <= 255) { printf("%c %d\n", j, j); j++; } return 0; }
(e) The cell phone charges are computed per minute with each partial minute being treated as a full minute. For example if your call is for 75 seconds, you will be charged for 2 minutes of use. Assume that an integer variable
talktimeinsecs stores the exact number of seconds of a cell phone conversation. Write a program fragment (not a complete program, no declarations, no printf’s or scanf’s,... ) that assigns a value to the integer variable talktimeinmins , consistent with the “ partial minute counts as full minute ” policy.
(f) Given below is an incomplete program that reads an integer n and computes and prints the term an where a 1 = 1, a 2 = 2, a 3 = 3, and for k > 3 : ak = ak−2 + 2 × ak−3. Assume n > 0. For ex: if n = 5, output is a 5 is 7. Complete it. void main() { int a_nMinus3 = 1, a_nMinus2= 2, a_nMinus1 = 3, a_n, n, i; printf("Enter n: "); scanf("%d", &n); if (n <= 3) printf("a%d is %d\n", n, n); for (i = 3; i < n; i++) { //Write code here to complete your program. //Just write these lines in your answer sheet } printf("a_%d is %d\n", n, a_n); }
void main(void) { int Mat[100][100], Arr[100]; for(i=0 ;i<100;i++) for(j=0;j<100;j++) scanf(“%d”,&Mat[i][j]); for(i=0;i<100;i++) { / Q 4 Write this code in your answer sheet to populate the entries of Arr / } } //FIGURE 1
main () { int n, div, rem, bit, i; printf ("Enter a number:\n"); scanf ("%d", &n); div = n; while (div >= 0) { div = div / 10; rem = div % 10; bit = rem; printf ("%d ", rem); for (i = 2 ; i >= 16 ; i *= 2){ bit = rem % i; bit = bit / i / 2; printf ("%d", bit); } //End for Loop printf ("\n"); } //End While loop } //FIGURE 2
int main(void) { int n, k, i; float mavg; printf("Enter array size"); scanf("%d",&n); int a[n]; // Q 6 (a) write code to take all the array //elements as input from the user. printf("Enter value of window size k:"); scanf("%d",&k); for(i=0; i<_____; i++) { /* *Q 6 (b) Using for loop, write this code to compute moving average for all moving sequences and print each such moving average. / } } //FIGURE 3
Q4. Consider an NxN 2D integer matrix Mat whose ith^ row has the elements {ai0, ai1,… aiN-1 } where 0 ≤i<N ; and a 1D integer array Arr of size N. In the kth entry of Arr, where (0≤k<N) , we want to store the dot product of two vectors Rk and Ck , where Rk contains elements of the kth^ row of Mat , starting from column 0 until (and including) the diagonal element when moved from left to right; and Ck contains elements of the kth^ column of Mat starting from row 0 until (and including) the diagonal element when moved from top to bottom. An example matrix Mat and corresponding Arr is shown below:
Mat =
Complete the program shown in FIGURE 1 to populate the entries of Arr, using only for loops (if required ). [Note: You can declare additional variables. Dot product of two vectors V 1 = {v 11 ,v 12 , … , v1m} and V 2 = {v 21 ,v 22 , … , v2m} is defined as the value v 11 *v 21 + v 12 v 22 + … + v1mv2m] [ 14M ]
Q5. Consider the program shown in FIGURE 2 which should do the following:
Step1 : Take a number n as input from user. Assume n > 0. Step2 : Extract each digit rem starting from right most position (or from Least Significant digit). Step3 : For each digit rem , extracted in step2, find equivalent 4-bit unsigned binary representation and print it. For Example, if n = 476, then output should be: 6 0110 7 0111 4 0100 However, while writing the program, the programmer has done some errors in it. Rewrite the program in your answer sheet after removing all the errors and underline the changes done. You should not introduce any new variables and don't change the logic. [ 12M ]
Q6. Given a Sequence of n values x 1 , x 2 , ..., xn and a window size k>0, the k Moving Averages of the corresponding Moving Sequence is defined as follows: y 1 = (x 1 +x 2 +......xk)/k y 2 = (x 2 +x 3 +......xk+1)/k ….. yn-k+1 = (xn-k+1 + xn-k+2 + ….+xn)/k
Example : For a sequence {1 2 3 4 5 6 7 8 9 10} with n=10 and window size k= 4, Moving Sequences are: {1,2,3,4}; {2,3,4,5}; {3,4,5,6}; {4,5,6,7}; {5,6,7,8}; {6,7,8,9}; {7,8,9,10}. Also, corresponding Moving Averages are: {2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5} The C program shown in FIGURE 3 reads number of elements n. It then reads all n values and value of k as user input. The program then displays all the moving averages (mavg). Give your answer for Q6(a) and Q6(b) as shown in the program. Just write required code in your answer sheet. You can declare additional variables, but (for each part) explicitly show the declaration(s) under the heading "Additional Variables". [ 12M ]
Arr =^1 46 218 For example: Arr[1] = dot product of {5,6} and {2, 6}