munotes®
C Programming, solved papers

May 2018 - CBSGS

Paper 25 of 38 18 questions answered 80 marks 3 hours

munotes.in i
munotes.in ii

The Paper as Set

The questions in this volume are the questions asked at the May 2018 - CBSGS examination, reproduced as the University of Mumbai set them, in the order it set them. Nothing has been reworded, added or left out. Only the answers are ours. See the original question paper.

Duration 3 hours  ·  Total marks 80  ·  18 questions answered

How to use this volume

Solve the paper first, under exam conditions and against the clock. Then read the answers here and mark your own. Reading a solution before attempting the question feels productive and teaches very little, because recognising an answer is not the same as being able to write one.

munotes.in iii

Q. 1

Q1 20 Marks

munotes.in 1

a.Define algorithm. Write algorithm to calculate Fibonacci series.[4]

Answer

For full marks, cover: a definition of an algorithm with its properties, the Fibonacci series and its rule, the algorithm in numbered steps, and a program that follows the steps, with a sample run.

Algorithm

An algorithm is a finite sequence of clear, unambiguous steps that takes some input and produces the required output. A correct algorithm has a definite start and end, each step can be carried out exactly, and it finishes after a finite number of steps.

Fibonacci series

The series begins 0, 1, and each later term is the sum of the two before it: 0, 1, 1, 2, 3, 5, 8, 13, and so on.

Algorithm to print the first n terms

  1. Start.
  2. Read n, the number of terms.
  3. Set a = 0, b = 1 and i = 1.
  4. If i > n, go to step 9.
munotes.in 2
  1. Print a.
  2. Set c = a + b, then a = b and b = c.
  3. Set i = i + 1.
  4. Go to step 4.
  5. Stop.

Program following the steps

#include <stdio.h>

int main(void)

{

int n, i;

long long a = 0, b = 1, c;

printf("Enter the number of terms (1 to 91): ");

scanf("%d", &n);

if (n < 1 || n > 91) {

printf("The number of terms must be from 1 to 91\n");

return 0;

}

printf("Fibonacci series:");

for (i = 1; i <= n; i++) {

munotes.in 3

printf(" %lld", a);

c = a + b;

a = b;

b = c;

}

printf("\n");

return 0;

}

Sample run

Enter the number of terms (1 to 91): 10

Fibonacci series: 0 1 1 2 3 5 8 13 21 34

For n terms the loop also works out c, the term after the last one printed. Writing F(0) = 0 and F(1) = 1, F(92) = 7540113804746346429 is the largest Fibonacci number that fits in a long long, whose largest value is 9223372036854775807. For 91 terms the last c is F(92); for 92 terms it would be F(93), which does not fit, so the program accepts up to 91 terms.

munotes.in 4

Conclusion. An algorithm is a finite, exact list of steps from input to output. The Fibonacci algorithm keeps the last two terms in a and b, prints a, and moves on with c = a + b, a = b, b = c; for 10 terms it gives 0 1 1 2 3 5 8 13 21 34.

munotes.in 5

The rest of the answers

You have read the paper as it was set and its first answer in full.

Every other answer, in every solved paper of this semester, opens with the solved papers.

Issue

Found an error in this volume? Report it and we will check it against the paper.

Up next · Paper 26 of 38 May 2018 - R16 C Programming Next paper

Tip: use the left and right arrow keys to turn papers.