Sie sind auf Seite 1von 13

IGNOU MCA MCS-011 Solved Question Paper

Updated on Mon, 12 October 2015 at 12:35 IST


2011
Year:
2011

MCS-011

MCA (Revised)

Term-End Examination

December, 2009

MCS-011 : PROBLEM SOLVING AND PROGRAMMING

Time : 3 hours
Maximum Marks : 100
(Weightage 75%)

Note : Question Number 1 is coumpulsory. Anser any three question from the rest.

Question 1.
(a.) Develop an algorithm and write a program to print the Fibonacci series upto a given number using recursion. (10 Marks)Answer

(b.) What do you understand by function prototype ? Write a program in 'C' to calculate the GCD of three numbers using the function prototype.
(10 Marks)Answer

(c.) List and explain any 5 reserved words of 'C' Language. (10 Marks)Answer

(d.) Differentiate between call by value and call by reference methods of parameters passing to a function giving an example of each. (5 Marks)
Answer

(e.) What will be the output of following 'C' programme ? (10 Marks)Answer

main()

  int i=1,j=1;

  for(;;)

  {

  if(i>5)

  break;

  else

  j+=1;

  printf("In %d",j)

  i+=j;
 }
}
  }

Question 2.
(a.) What do you mean by scope of a variable ? Differentiate between Global and Local variables giving an example of each. (5 Marks)Answer

(b.) Explain the syntax of switch case statement in 'C' language. Also compare the performance of switch case with if else statement. (5 Marks)
Answer

(c.) Draw a flowchart and write a program in 'C' to convert a decimal number to its octal equivalent. (10 Marks)Answer

Question 3.
(a.) What are the precautions that must be taken care to use macros in 'C' ? Define a macro to find the factorial of a given number n ? (10 Marks)
Answer

(b.) Write a program in 'C' to find whether a given number is Armstrong number or not. (10 Marks)Answer

Question 4.
(a.) Write program in 'C' for the multiplication of two square matrices. (10 Marks)Answer

(b.) What is pointer variable ? How is a pointer variable declared ? How is the address of a pointer variable determined ? How pointer can be used
to pass an entire array to a function in C? Explain with the help of an example. (10 Marks)Answer

Question 5.
(a.) Explain fprintf ( ) and fscanf ( ) statements with an example of each. (10 Marks)Answer

(b.) Write a program in 'C' :

(i) To revers a string

(ii) To copy str1 to str2 without the use of strcpy function. (10 Marks)Answer

*****

Solutions

Although all the programs given in this solution are checked, compiled and run successfully under following environment :

File Version : 4.9.9.2

Description : Dev-C++ IDE

Copyright : Copyright Bloodshed Software

Compiler : gcc

Operating System : Microsof Windows XP Professional Version 2002 Service Pack 2, v.2096

Computer : Intel(R) Celeron(R) CPU 2.40GHz, 256 MB RAM

But there is always chances of errors for a human-being so nobody claim to be completely correct and doesn't take any responsibility.

Thanks

Answer 1(a). : (Go to this question)

Algorithm For Fibonacci Series

STEP 1 : Start

STEP 2 : Read NUMSTEP

STEP 3 : Set PREV=0, CURR=1,COUNT=1

STEP 4 : Print CURR

STEP 6 : NEXT = PREV + CURR

STEP 7 : PREV = CURR

STEP 8 : CURR = NEXT, COUNT = COUNT+1

STEP 9 : IF COUNT <= NUMSTEP GOTO STEP 4

STEP 9 : End

Program in C using recursion for Fibonacci series


#include <stdio.h>

int fib(int n) {

  if(n<2)

    return n;

  else

  return fib(n-1)+fib(n-2);

int main (void) {

  int ns,i; //ns is number of steps

  printf("\nEnter the number of steps = ");

  scanf("%d",&ns);

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

  printf("%d\n",fib(i));

  }

  getch();

  return 0;

(Go to this question)

Answer 1(b). : (Go to this question)

Function Prototype

Function prototype is a declration that tells the compiler what the function's name, how many argument and what type of arguments it will take,
and what type of value it will return. Function prototypes doesn't include function's definition body. In prototyp declaration argument names are
optional but type is necessary. Also the order of arguments must be same in prototype, definition and calling.

When program containing a function is compiled, the compiler checks that function's prototype whether the function calling is in accordance with
it's prototype or not. If prototype declaration and function calling is unmatched then compiler stops compiling and show error.

'C' program to for GCD of three numbers

#include <stdio.h>

int gcd_fun(int, int, int); //prototype

int main(void) {

  int a,b,c;

  int r;

  printf("\nEnter the three numbers = ");

  scanf("%d%d%d",&a,&b,&c);

  r = gcd_fun(a,b,c);

  printf("\n\nGCD of %d, %d, %d is = %d",a,b,c,r);

  getch();

  return 0;

/*function defintion starts here */

int gcd_fun(int a,int b, int c) {

  int gcd=1,div=2;

  int ra,rb,rc;

  int temp;

  temp = (gcd*div);

  while(a>=temp||b>=temp||c>=temp) {

  ra = a%(gcd*div);

  rb = b%(gcd*div);
  rc = c%(gcd*div);

  if(ra+rb+rc)

  div = div+1;

  else

  gcd = gcd*div;

  temp = (gcd*div);

  }

  return gcd;

/*function defintion ends here */

(Go to this question)

Answer 1(c). : (Go to this question)

Some Five reserved words in 'C' Language

1.) struct

The keyword struct is used to declare structure in a C program. The following example show the use of struct keyword,

struct employee {

  int ecode;

  char ename[30];

};

In above code struct is a keyword, after that name of structure is followed, employee is structure name in the code. Note that at the end of
structure the semicolor (;) is necessary.

2.) while

The keyword while is used to perform a loop for a specific code set till a particular condition is setisfies. The following example show the use of
while keyword,

i=1;

while (i>=10) {

...<code is here >...

  .........

the code with in the loop body is execute till the value of i is reached at 10.

3.) int

The keyword int is used to declare an integer variable. For example,

int age;

here int is data type to declare the variable age as integer.

4.) float

The keyword float is used to declare a float or a real variable. For example,

float temprature;

here float is data type to declare the variable temprature as float.

5.) char

The keyword char is used to declare an character variable. For example,

char grade;

here char is data type to declare the variable grade as character variable.

(Go to this question)

Answer 1(d). : (Go to this question)

Difference between Call by Value and Call by Reference


Parameter passing to functions in C can be done in two ways, one is Call by Value and other is Call by Reference. In Call by Value method, when
parameters are passed to a function, onlty the copy of original parameters is passed not the actual argument. It means if the called function
modify it's own copy of parameters, which it gets from another function's parameteres, there will be no change in actual parameters and they will
remain in their original values. For example,

int main(void) {

  int a=3,b=5;

  change(a,b);

  printf("\na = %d",a);

  printf("\nb = %d",b);

void change(int x, int y) {

  x = x+1;

  y = y-3;

OUTPUT :

a=3

b=5

As you can see in above code we pass the name of the variables to change() then print their values. Their is no change in the values of variable
while function change() modifies values.

Whereas, in Call by Reference method we pass the address of the variables to a function and in this situation if the function modifies any
variable's value then the modification will reflect at their original place. Because in call by reference method function access a variable through
it's address. For example,

int main(void) {

  int a=3,b=5;

  change(&a,&b);

  printf("\na = %d",a);

  printf("\nb = %d",b);

void change(int *x, int *y) {

*x = *x+1;

*y = *y-3;

OUTPUT :

a=4

b=2

In above code the output is 4 and 2 respectively for a and b and this time change in values reflects at main(). Because we pass the addresses of
a and b.

Using Call by Reference method we can access a variable more fast then Call by Value. One more advantage of passing address is, we overcome
the problem of returning only one value by a function.

(Go to this question)

Answer 1(e). : (Go to this question)

Let's review the code given in question 1(e),

main()     //line 1

{    //line 2

  int i=1,j=1;     //line 3

  for(;;)     //line 4

  {     //line 5

  if(i>5)     //line 6

  break;     //line 7
  else     //line 8

  j+=1;     //line 9

  printf("In %d",j)    //line 10

  i+=j;     //line 11

  }     //line 12

}    //line 13

In the original question paper there is no semicolon (;) at the end of the printf() in line 10, if it is not a printing mistake then this program has this
error and wouldn't be compiled.

If we assume that it is a printing mistake and there is no syntatical mistake in the code then this will print after compiling,

OUTPUT :

In 2In 3

(Go to this question)

Answer 2(a). : (Go to this question)

Scope

Scope implies the region or regions of a program that can access an identifier, it mean how long a variable is alive and what region or block of
code have access to the variable.

A Local variable is a variable that is alive and accessed only by the innermost block where it is declared. As the innermost block, in which the
variable is declared, ends the variable will lost from memory.For example,

main() {

// block of main() starts here

  int a=10;

  { // new inner block in main() starts here

  int a=20;

  printf("\na = %d",a);

  } // new inner block in main() ends here

  printf("\na = %d",a);

} // block of main() ends here

OUTPUT

a = 20

a = 10

The first declaration for a and it's scope is in whole main() block after terminate main() first a variable will lost. Again in main() new inner block is
starts and have another declaration for variable a. This inner a don't have relation and confliction with out a because it has it's new scope within
main() and also it will loose it's value as this inner block ends. These are normally local variable as they belongs only to their local blocks.

Whereas Global variables are available till the termination of whole program and can be accessed from anywhere in the program or among
multiple translation units. Unlike Local variable Global variables are declared out of all the functions and have a default intialisation az zero.
Following code shows the use of global variable,

int a=30; //Global, out of all the functions

main() {

// block of main() starts here

  { // new inner block in main() starts here

  int a=20;

  printf("\na = %d",a);

  } // new inner block in main() ends here

  printf("\na = %d",a); // here is global 'a' is printed

} // block of main() ends here

OUTPUT

a = 20

a = 30
You can see that last printf() prints the value 30 while there is no extra declaration for very owna variable of main().It means it is global variable
accessing is occured.

(Go to this question)

Answer 2(b). : (Go to this question)

switch - case

Syntax for using switch :

switch( <expression> ) {

case <constant> : <code>

case <constant> : <code>

case <constant> : <code>

... ... ...

... ... ...

... ... ...

default <constant> : <code>

Althought there is always an alternate of if-else for every switch-case block. But is hard to use switch-case alternate for every if-else block.
Because switch-case has constant expression for case evaluation while if-else doesn't. As far as the performance is concerned there is no
certain idea which is better but if few numbers of condition is used in a code then if-else may be better and for many choices may be switch-
case will go faster.

(Go to this question)

Answer 2(c). : (Go to this question)

Algorithm (not flowchart) for Decimal to Octal

As much as possible I am trying to avoid having image in the page so here I am going to write algorithm only, you can draw flowchart from this
easily.

STEP 1 : Set roct =0,oct =0,q=0,r=0

STEP 2 : Read NUM

STEP 3 : q = NUM

STEP 4 : Repeat STEP 5, STEP 6, STEP 7 while q>=1

STEP 5 : r = q%8

STEP 6 : roct = roct*10+r

STEP 7 : q = q/8

STEP 8 : Repeat STEP 9, STEP 10, STEP 11 while roct != 0

STEP 9 : r = roct%10

STEP 10 : oct = oct*10+r

STEP 11 : roct = roct/10;

STEP 12 : Print oct

C program for Decimal to Octal

Although the above algorithm describes the general procedure for conveting a Decimal to Octal but it has some drawback when we apply it in C
program. It is unable to convert some particular decimal number correctly in octal like 320 (probably some numbers which have zero as their last
digit).

So I write here code that is not belong to above algorithm but convert a Decimal to Octal correctly. It uses an array variable.

#include <stdio.h>

int main(void) {

  long int num,q,r,i=0;

  int roct[50];

  printf("\nEnter the num = ");

  scanf("%ld",&num);

  q = num;
  while(q>=1) {

  roct[i++] = q%8;

  q = q/8;

  }

i=i-1;

  printf("\n\nThe Decimal number %ld is in Octal = ",num);

while(i>=0)

printf("%d",roct[i--]);

getch();

  return 0;
}

(Go to this question)

Answer 3(a). : (Go to this question)

Program with macro to calculate factorial

The macro name must have no spaces in it, and it must conform to the same rules that C variables follow: Only letters, digits, and the underscore
(_) character can be used, and the first character cannot be a digit.

#include <stdio.h>

#define FACT(x) for(i=x-1;i>=2;i--) x*=i;

int main(void) {

  long int n,i,t;

  printf("\n\nEnter the number = ");

  scanf("%ld",&n);

  t=n;

  FACT(t);

  printf("\n\nFactorial of %ld is = %ld",n,t);

  getch();

  return 0;

(Go to this question)

Answer 3(b). : (Go to this question)

Armstrong Number

In number theory, a Narcissistic number or a Pluperfect Digital Invariant (PPDI) or an Armstrong number or a Plus Perfect Number is a number
that is the sum of its own digits each raised to the power of the number of digits.

See On Wikipedia Narcissistic Number

(NOTE: In most internet articles and books armstrong number described as the number equals to sum of its digits raised to the power of three
means sum of cube of digits. But it is not the completely correct definition of Armstrong Number or Narcissistic number. Here in this answer I
have tried to give a more correct code in accordance with precise definition.)

'C' program to check to be a Armstrong Number

#include <stdio.h>

long int armstrong(long int);

int how_many_digits(long int);

long int raise_power(int,int);

int main(void) {

  long int num,arm;

  printf("\nEnter the number = ");

  scanf("%ld",&num);

  arm = armstrong(num);
  if(num==arm)

    printf("\nThe number %ld is an Armstrong Number",num);

  else

 printf("\nThe number %ld is NOT an Armstrong Number",num);

  getch();

  return 0;

long int armstrong(long int t) {

  int digits,i,arm=0;

  digits = how_many_digits(t);

  for(i=digits;i>=1;i--) {

  arm = arm+raise_power(t%10,digits);

  t=t/10;

  }

  return arm;

int how_many_digits(long int number) {

  int i=1;

  while(number/10!=0) {

  i=i+1;

  number=number/10;

  }

  return i;

long int raise_power(int base, int expo) {

  int i,result=1;

  for(i=expo;i>=1;i--)

    result = result*base;

  return result;

(Go to this question)

Answer 4(a). : (Go to this question)

For two matrices A of (ma x na) dimension and B of (mb x nb) dimension to be multiplied to each other, na (column) of A has to be equal to mb
(rows) of B. Otherwise they are not applicable for multiplication.

Here in the question(4a) given that matrices must be square matrices too. Square matrix is matrix which have the same number of rows and
column. It mean if a matrix A has the dimensions m x n and m is equal to n then A is a square matrix.

See On Wikipedia for more on Matrix Matrix_(mathematics)

Program in 'C' for the multiplication of two (square) matrices.

#include <stdio.h>

  int main(void) {

  int A[10][10],B[10][10],Result[10][10];

  int i,j,k,sum=0,AR,AC,BR,BC;

  printf("\nHow many ROWS in FIRST matrix, enter = ");

  scanf("%d",&AR);

  printf("\nHow many COLUMNS in FIRST matrix, enter = ");

  scanf("%d",&AC);
printf("\nHow many ROWS in SECOND matrix, enter = ");

  scanf("%d",&BR);

  printf("\nHow many COLUMNS in SECOND matrix, enter = ");

  scanf("%d",&BC);

  if(AC!=BR) {

  printf("\nMultiplication can not be done of these matrices");

  getch();

  printf("\nPress any key to exit");

  exit(1);

  } else

    printf("\nMultiplication can be done of these matrices");

  if(AR!=AC||BR!=BC) {

  printf("\nOne of these matrices is not a SQUARE Matrix so can't proceed more");

  getch();

  printf("\nPress any key to exit");

  exit(1);

  }

  printf("\n\nEnter the elements of first matrix :\n");

  for(i=0;i<AR;i++)

  for(j=0;j<AC;j++)

      scanf("%d",&A[i][j]);

  printf("\n\nEnter the elements of second matrix :\n");

  for(i=0;i<BR;i++)

  for(j=0;j<BC;j++)

      scanf("%d",&B[i][j]);

  for(i=0;i<BC;i++) {

  for(j=0;j<AC;j++) {

  sum = 0;

  for(k=0;k<AC;k++) {

  sum = sum + A[i][k]*B[k][j];

  }

  Result[i][j] = sum;

  }

  }

  printf("\nProduct is :\n\n");

  for(i=0;i<AR;i++) {

  for(j=0;j<BC;j++)

  printf("\t%d",Result[i][j]);

  printf("\n\n");

  }

  getch();

  return 0;

(Go to this question)

Answer 4(b). : (Go to this question)

Pointer varibale
Pointer variable is a variable that contains the address of another variable.

Pointer varibale declaration

A pointer variable is declared in the same way as normal variables are declared except they has a asterisk(*) befor their name. For

example, int *pa;

Now here in above declaration pa is the name of a pointer and int is its type. The type of a pointer implies that it can contain an address only of

that type. Here pa can be used to hold address of only an integer variable, it can not be used to hold address of a float or a char type varibales.

We must NOT use asterisk (*) before the pointer name, when we assign an address of a variable to the pointer. For example,

int var,*pv;

pv = &var;

Here first line tells that var is an integer variable and pv is an integer pointer variable. Second line shows how to assign address of a variable to a
pointer variable. To assign an address we use ampersand sign (&) before the varibale name.

Now we can use the pointer varibale pv to manipulate the value of variable var. All we have to do for it is, use the pointer variable pv following a
asterisk (*). For example,

int var,*pv;

pv = &var;

scanf("%d",&var);

printf("\nvar = %d",*pv);

In above code we read the value through var but displayed the value through its pointer that is pv using (*) before it.

Pointer can be used to pass an entire array to a function in C

Pointers can be used to pass an entire array to a function in C, in fact pointer is the only way to pass the entire array to a function.

Actually in C the name of an array is a pointer and it has the address of first element in the array. Subsequent elements are accessed adding one
memory location to this pointer (array name). For example,

int A[5] = {15,-9,1,8,3;}

Here A is an integer array having five elements. Now A is a pointer holding address of the first (0th) subscript value that is, here, 15. Means, A =
&A[0], and *A has the value 15. If we want to access subsequent element then we add one to this pointer, like A+1, this is the address of A[1].
And its value can be retrieve using *(A+1).

So it is clear that,

A[0] = *(A+0) = *A

A[1] = *(A+1)

A[2] = *(A+2)

A[3] = *(A+3)

A[4] = *(A+4)

Now it is clear, that we can get the address of a particular index number variable by adding it to the name of array and value by dereferencing it
(getting value by adding the (*) to a pointer is known as dereferencing).This is the fact that works in passing array to a function.One extra thing to
be done is to pass the length of the array too, because compiler can't automatically manage where to stop the loop. Following example shows it.

#include <stdio.h>

void show_array(int *,int);

int main(void) {

  int A[5] = {15,-9,1,8,3};

  show_array(A,5);

  getch();

void show_array(int *t,int l) {

  int i;

  for(i=0;i<l;i++)

  printf("\nA[%d] = %d",i,*(t+i));

Note that here we have passed the length of the array A too in function as 5.

(Go to this question)


Answer 5(a). : (Go to this question)

fprintf() and fscanf()

fprintf() and fscanf() are file I/O functions defined in stdio.h. Prototypes of these functions are as,

int fprintf(FILE * restrict stream, const char * restrict format, ...);

int fscanf(FILE * restrict stream, const char * restrict format, ...);

fprintf() works almost in the same way as printf() works. It is used to give an output to a file. Suppose we want to print a line "Bob is cool guy" in
a file named cool.lt. Then we use fprintf() as,

#include <stdio.h>

int main(void) {

  FILE *fp;

  int num;

  printf("\nEnter a number = ");

  scanf("%d",&num);

  fp = fopen("cool.lt","w");

  fprintf(fp,"%d",num);

  fclose(fp);

In the above code we get a number from user and prinf it in file named cool.lt

fscanf() works almost in the same way as scanf() works. It is used to take an input from a file. Suppose we want to retreive the text from the file
we have just created in above code cool.lt. Then we use fscanf() as,

#include <stdio.h>

int main(void) {

  FILE *fp;

  int num;

  fp = fopen("cool.lt","r");

  fscanf(fp,"%d",&num);

  printf("\nText of file\n %d",num);

  fclose(fp);

This code retrieve the data of cool.lt and outputs on console.

(Go to this question)

Answer 5(b). : (Go to this question)

Program to reverse a string

Because question 5(b)(i) don't demands use customize function so we are free to choose built-in library function strrev() to reverse a string, as
follows :

#include <stdio.h>

int main(void) {

  char st[50];

  printf("\nEnter a string = ");

  gets(st);

  strrev(st);

  printf("\n rev string is : %s",st);

Although we can make alternate function too and use as,

#include <stdio.h>

void xstrrev(char *);

int main(void) {
  char st[50];

  printf("\nEnter a string = ");

  gets(st);

  xstrrev(st);

  printf("\n rev string is : %s",st);


}

void xstrrev(char *st) {

  int i,j,l=strlen(st);

  char tch;

  for(i=0,j=l-1;i<l/2;i++,j--) {

  tch = *(st+i);

  *(st+i) = *(st+j);

  *(st+j) = tch;

  }

Program to copy str1 to str2 without the use of strcpy()

#include <stdio.h>

void xstrcpy(char *, char *);

int main(void) {

  char st1[50],st2[50];

  printf("\nEnter a string = ");

  gets(st1);

  xstrcpy(st1,st2);

  printf("\nString st1 is : %s\nString st2 is : %s",st1,st2);

void xstrcpy(char *st1,char *st2) {

  while(*st1!='\0')

*st2++ = *st1++;

*st2 = '\0';