BTech II CS101 PPS Practice Questions and Assignment
BTech II CS101 PPS Practice Questions and Assignment
Module IV [9L]
Functions (including using built in libraries), Parameter passing in functions, call by value, call by
reference. Passing arrays to functions, Recursion (Finding Factorial, Fibonacci series, Ackerman
function etc.).
[
Recap:
Function format: <return type> function_name (<data_type, variable_name>, ….) {<statements>}
Call to function : T=function_name(<variable_name>,….)
or function_name(<variable_name>,….) ///in case of void return type
Call by value: function definition: int add(int a, int b){int c=a+b; return c}
int main() { int p=5,q=10, t; t=add(p,q);…. }
Call by reference: function definition:
Usage I: To provide access to local variables across function boundaries
function definition: void add(int a, int b,int *c){*c=a+b;// no return required}
function call: int main() { int p=5,q=10, t; add(p,q,&t); printf(“%d”, t);}
//Note: use only those variables for call by reference in which you need to change
values from the called function.
For example, here values of p and q are only to be read in add(), so they have been
passed as „value‟ only. But the result of addition has to be stored into t from the called
function add(), across function boundary, so address of t (i.e. &t) has been passed as
„reference‟ from main(), and has been received in „int *‟c in add(). Then from add(), the
location of t has been accessed indirectly through „*c‟.
Usage II: To facilitate functions with multiple return values
function definition: void addmul(int a, int b,int *c, double *d){*c=a+b; *d=a*b;}
function call: int main() { int p=5,q=10,s; double t; addmul(p,q,&s,&t); }
1, function definition: int arraysum(int *x, int size) OR int arraysum(int x[], int size)
{int i, sum=0; for(i=0;i< size ;i++) sum=sum+*(x+i); return sum;}
function call: int main(){int a[5]={10,2,3,4,5},s; s=arraysum(a,5);}
2. Using P++ (which is P=P+1) will replace the value of P with the address of the next
element of the array A
function definition: int arraysum(int *x, int size) OR int arraysum(int x[], int size)
{int i, sum=0; for(i=0;i< size ;i++){ sum=sum+*x; x++} return sum;}
function call: int main(){int a[5]={1,2,3,4,5},s; s=arraysum(a,5); }
Recursive Functions: A function calling itself, usually with a modified argument; the modification
is done in such a way that convergence to base case (termination condition) is ensured.
Direct recursion: function calls itself. Can be single, e.g. in factorial() or multiple calls, e.g. in
Fibonacci().
Indirect recursion: Call to original function made at end of a chain of function calls, e.g. A() calls
B(), B() calls C(), then C() calls A().
Case I: Single recursion :
Function definition: double fact(int n){ if (n==0) return 1; return n*fact(n-1);}
// base case is “n=0” and parameter modification “n-1” ensures that any value of
positive integer n would eventually converge down to 0.
Function call: int main() {printf(“%d! = %G”, 10, fact(10)); …}
Case II: Multiple recursion :
Function definition: double fib(int n){ if (n==0) return 0; if (n==1) return 1; return
fib(n-1)+fib(n-2);}
Function call: int main() {int i=0;for (i=0;i<n;i++) printf (“%G”, fib(5)); …}
Module V [9L]
Structures, Defining structures and Array of Structures
Pointers: Defining pointers, Use of Pointers in self-referential structures, File Handling
PRACTICE QUESTIONS
ASSIGNMENT (5 MARKS)
1. Write general features and utility of following computing technologies: i. Workstation ii. Mainframe
iii. Vector Processor Supercomputer iv. Cluster Computing v. GPGPU vi. dynamic linking and loading
2. Briefly explain: i. different storage classes in C with one example of use for each.
ii. types of segments of memory layout in C?
3. Briefly explain dynamic memory allocation in C? Write program to implement dynamic array in C.
4. Write a program in C to create mini-Employee-Database with menu displaying following operations:
CREATE (initial entries of database), INSERT (add more records), SEARCH, DELETE. Make suitable
uses of the various file opening options –{w, r, a, w+, r+, a+}. In your documentation write the
structure definition and code of the four functions only.
5. Write the recursive function for bisection method for determining roots of an equation.