Trending ▼
ICSE
CBSE 10th
ISC
CBSE 12th
CTET
GATE
UGC NET
Vestibulares
ResFinder
C++ mid term
4 pages, 0 questions, 0 questions with responses, 0 total responses
,
0
0
jaishri
+Fave
Message
Profile
Timeline
Uploads
Home
>
jaishri
>
Formatting page ...
100 marks CS 101 Mid. Exam (Closed book, No clari cations) 9:30-11:30, 12/9/8 Write your roll number and LAB BATCH as you know it currently on your answer bok. Problem 1:[20 marks] A number is said to be perfect if it is the sum of its all possible factors (except itself!). For example, 6 has factors 1,2,3 and 1+2+3=6 and hence it is perfect. Also 28=1+2+4+7+14 is perfect. Write a function that determines whether a given number is perfect. bool perfect(int n){ int sum = 0; for(int i=1; i<n; i++){ if (n % i == 0) sum += i; } return (sum == n); } syntactically correct function de nition: 5 marks. returned value is correct or correct message is printed: 3 marks. Sum initialized 2 marks. sum correctly updated, 5 marks (-2 if n is also included in loop bound). Overall correctness 5 marks. Key point: When you are asked to write a function, usually you are expected to return the result, not print it. This time we gave marks for printing the result also, but from now on, we expect a proper value to be returned. If a value is returned, the function can be used by other functions; printing gets done only rarely. Problem 2:[20 marks] A sequence of numbers a0 , a1 , . . . , an 1 is said to be a palindrome if a0 = an 1 , a1 = an 2 , a2 = an 3 and so on. Write a program which takes as input an integer n, and the sequence a0 , a1 , . . . , an 1 , and determines if the sequence is a palindrome. Try to keep the number of comparisons you do in your algorithm small. array declaration: 2 marks reading in the array: 3 marks Proper element comparison: 5 marks n/2 comparisons worst case: 3 marks early termination of the loop (break or equivalent): 3 marks overall correct answer: 4 marks int n; cin >> n; int a[n]; for(int i=0; i<n; i++) cin >> a[i]; bool ans = true; for(int i=0; i<n/2; i++) if( a[i] != a[n-1-i] ){ ans = false; break; } cout << ans << endl; Problem 3:(a)[10 marks] Show what is printed when the code fragment below is executed. (b)[5 marks] Say what value ans will take at the end in general, as a function of the values assigned to a,b. Hint: try out a few other examples. No explanation expected. int a=13, b=12, ans=0; while(a>0){ if(a % 2 == 1) ans += b; a = a/2; b = b*2; cout << a << " " << b << " " << ans << endl; } cout << ans; (a) 6 24 12 3 48 12 1 96 60 0 192 156 156 2 marks for each line above. You were expected to give exactly the above printout. Some credit is given if it appears that you understood something, but it will be small. (b) product of a,b. Problem 4: A sequence of 3 drawings (let us call them cactuses, or more accurately, cactii) is given above. In these 3 drawings, there are squares of just 3 sizes, and these have side lengths 1, 3, 9. (a)[5 marks] Draw what you think is the next cactus drawing in this sequence. If there is a lot of repetition, you can just draw parts and show what gets repeated. (b)[20 marks] Write a function that makes these drawings. Before giving the code state its precondition i.e. for the cactus to be drawn in a certain direction and at a certain point where does the turtle need to be, and in which direction should it point. Also give the postcondition, i.e. at the end of the function 2 where the turtle is and where does it point. Finally, state how you will use your procedure to generate the 3 drawings. You may not get any credit if you dont state these. (a) 3 smaller squares attach to each smallest square in the 3rd drawing; after that entire drawing scaled up by a factor 3. Give ve marks even if nothing said about size of the squares. No text is really expected, picture will su ce. void cactus(float n){ if (n >= 1){ // any number bigger than 1/3 is fine. left(45); for(int i=0;i<3;i++){ forward(n); left(45); cactus(n/3); right(135); } forward(n); right(135); } } Precondition for above code: turtle should be at the root of the cactus and point in the direction in which the cactus is to grow. Postcondition: turtle is at the root, facing in the direction in which the cactus grew, i.e. exactly as at the beginning. 1+1 for stating any sensible pre and post conditions (position and orientation). 3 marks to say how to generate the drawings. In the above case, the drawings would be made by calling cactus(1), cactus(3), cactus(9). 1. Recursive calls: there should be 3. 2. Without recursive calls, a square should get drawn. 3. Square edges and recursive calls alternate. 4. Post condition is ensured. 5. Base case is consistent with pre and post conditions (base case need not always be empty only if pre and post condition coincide). 6. Squares are smaller by factor of 3. 2 marks for each point 12 overall. 3 marks if overall correct. No credit given for writing separate programs for each drawing. Problem 5: Consider the sequence of numbers: 1 1 1 x2 = 2 + x3 = 2 + x0 = 2 x 1 = 2 + 1 1 2 2+ 2 2 + 2+ 1 2 x4 = 2 + 1 2+ 1 2+ 1 2+ 1 2 In general, in xi there are i levels of fractions. (a)[10 marks] Write a recursive function that returns xi given i. (b)[10 marks] Write a non-recursive function (i.e. using a loop) that does the same. 3 5 marks for getting the idea: what should be in the loop or in the recursion. 5 marks for getting the syntax right. Clearly specifying the base case, clearly writing the loop update etc. Arrays were used by many, but they were not needed. But marks have been given if the use is proper, with proper declarations. This problem was meant to be slightly unusual. However, we really have done many problems on recursion, and consultants have also discussed many recursion problems in tutorials. Yes, tutorials are not compulsory, but then you should at least solve the problem sets on your own. int n; cin >> n; double ans = 2; for(int i=0; i<n; i++) ans = 2 + 1.0/ans; double fun(int a){ if (a > 0) return 2 + 1.0/fun(a-1); else return 2; } 4
Formatting page ...
Formatting page ...
Formatting page ...
Print intermediate debugging step
Show debugging info
Hide debugging info
Horizontal lines at:
Guest Horizontal lines at:
AutoRM Data:
Box geometries:
Box geometries:
Text Data:
© 2010 - 2026 ResPaper.
Terms of Service
Contact Us
Advertise with us