0% found this document useful (0 votes)
12 views

BTech II CS101 PPS Practice Questions and Assignment

The document provides an overview of functions, parameter passing, recursion, structures and pointers in C programming. It discusses call by value, call by reference, passing arrays and 2D arrays to functions. Examples of recursive functions like factorial and Fibonacci series are provided. The document also lists practice questions related to C programming concepts.

Uploaded by

yashosw10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

BTech II CS101 PPS Practice Questions and Assignment

The document provides an overview of functions, parameter passing, recursion, structures and pointers in C programming. It discusses call by value, call by reference, passing arrays and 2D arrays to functions. Examples of recursive functions like factorial and Fibonacci series are provided. The document also lists practice questions related to C programming concepts.

Uploaded by

yashosw10
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Tutorial and Practice Set of BTech II CS101 Programming for Problem Solving

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); }

Usage III: To access Array values across function boundary


[ conceptually: name of array (say a) is equivalent to address of the first element
of array a (i.e. &a[0])
function definition: int arraysum(int x[], int size)
{int i, sum=0; for(i=0;i< size ;i++) sum=sum+x[i]; return sum;}
// Alternative definition: int arraysum(int *x, int size){}
function call: int main(){int a[5]={1,2,3,4,5},s; s=arraysum(a,5); }// a== &a[0]

Special Rule for passing 2D array


function definition: int matsum(int x[][5], int r, int c) {int i, j, sum=0;
for(i=0;i<row;i++) for(j=0;j<col;j++) sum=sum+x[i][j]; return sum;}
// Only first dimension can be left unspecified. Second dimension must be
specified in the function definition.
// Alternative definition: int matsum(int *x[5], int r, int c){…}
function call: int main(){int a[2][3]={{1,2,3},{4,5,6}},s; s=matsum(a,3,5);}

Pointer arithmetic to access Array values across function boundary


[ conceptually: As per Pointer arithmetic:
Consider a pointer P, keeping address A, then P+1 returns A+size of data type of P.
A[2000, 2004,2008,2012…]
A={5, 2, 8,,….}
int*P=A =2000 *P-> *(2000) ->5
P+1 ==2000+size(int) = 2000+4=2004 = &a[1] (*(P+1)) = (*(2004))= a[1]=2

So, if int array A has been received in „int *P‟ then


P will refer to &a[0], i.e. address of a[1], and *P will refer to value of a[0];
Similarly, (P+1) will refer to &a[1], i.e. address of a[1] and *(P+1) will refer to a[1], , so
on…. So, to process all elements in sequence, using loop ( *P+1 = 5+1 =6)

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)); …}

Recursive function corresponding to an Iterative method:

a. Iterative Function definition of factorial function:


double iterfact(int n){double fact; for(int i=n; i>=0;i--)fact=fact*i; return fact;}
Recursive Function definition:
double fact(int n){ if (n==0) return 1; return n*fact(n-1);}
// Note the correspondence of “i>=0” and “n==0”,: the iterative method begins
with n and runs only upto “i==0”, reducing by i--. Same sequence is followed by
beginning with n upto “n==0”, modifying as “n-1” at each call in the recursive
definition.
b. Iterative Function definition for array sum:
double iterarsum(int a[], int n)
{ int i; double sum; for (i=0;i<n;i++)sum=sum+a[i]; return sum;}
Function call: int main() {int a[]=(1,2,3);printf(“%d! = %G”, 10, iterarsum (a, 3));}
Recursive Function definition:
double recarsum(int a[], int n)
{ if (n==0) return(a[0]); return (a[n]+ recarsum(a, n-1));}
Function call: int main() {int a[]=(1,2,3);printf(“%d! = %G”, 10, iterarsum (a, 3));}

Module V [9L]
Structures, Defining structures and Array of Structures
Pointers: Defining pointers, Use of Pointers in self-referential structures, File Handling

PRACTICE QUESTIONS

1. Compare the performance and applications of different generations of computers.


2. Explain the essential components of a computing system with neat block diagram.
3. What do you mean by RAM, ROM?
4. What do you mean by memory hierarchy? Explain: i. Registers ii. RAM iii. Cache memory iv.
Secondary Storage
5. **(optional) Explore the concepts of storage technologies: RAID (Redundant Array of Inexpensive
Disks or Drives, or Redundant Array of Independent Disks), Storage area network (SAN).
6. Explain the different types of storage devices with regard to their storage medium, capacity, access
speed, and utility.
7. Write about the program’s execution cycle.
8. Write about the language processors involved during a program development
9. Write the function of Compiler. Distinguish with interpreters with regard to function, speed, and
applicability. Write name of 3 programming language of each type.
10. Write about different types of linking and loading schemes. Particularly distinguish among: absolute
loading, direct linking and loading, dynamic linking and loading.
11. Using suitable example highlight the importance of macro methods in comparison to functions.
What is limitation of macro methods? (What is importance of functions then?)
12. Distinguish between the content of .obj and .exe files.
13. Write and explain four keywords of assembly language code.
14. What are types of identifiers in C? Explain the terms: declaration, definition, initialization in terms of
identifiers in C.
15. Distinguish between: i. const float pi=3.14 ii. #define PI 3.14
16. What is macro method? Give an example. How macro methods are executed?
17. What are steps involved during execution of a function call? (What is overhead of a function call)
18. Highlight the importance of macro methods in comparison to functions. What is limitation of macro
methods? (What is importance of functions then?)
19. Write one example to show utility of each of the bitwise operators.
20. Write program to determine largest of 3 numbers using ternary operators. Extend it for 4 numbers.
21. What do you mean by implicit type casting and explicit type casting? Explain in the context of { … int
a=5,b=2; float c= a/b, d=(float)a/b; printf(“%d%d”,c,d);
22. What is output of {…a=2;b=2;printf(“%d%d%d%d%d%d”, a, b,++a+b--,a++ - ++a, a, ++b);…}. Explore
and write rules related to processing of expressions having preincrement and postincrement.
23. What is string in C? Explain the function of following string handling functions of C- strlen(), strcat(),
strcpy(), strcmp(), substr().
24. What do you mean by column major and row major memory representations of 2D array? Write
expressions to access (i,j)th element of array X[R][C] assuming base address X. Which approach has
been implemented in C? Explore one programming language in which the other method is applied.
25. Consider pointer p to integer array A. Write expressions to access A[0],A[1],A[i] using p.
26. Consider pointer p to integer 2D array A[3][5]. Write expressions to access A[0][0],A[1][3],A[i][j]
using p.
27. Write flowchart and algorithm for – i. bubble sort ii. Selection sort iii. Insertion Sort
28. Write flowchart and algorithm for – i. linear search ii. binary search (works over sorted array)
29. What is function prototype in C? What do you mean by formal argument and actual argument of
function in C?
30. What do you mean by modularity? What is its benefit? How it is facilitated in C?
31. What is usage of void return type? Give example.
32. How many values a function in C can return?
33. Write one example function for each of the following combinations: i) <no input, no output> ii)<no
input, one output> iii)<one input, no output> iv)<one input, one output> v)<many input, one output>
34. Explain the concept of local scope and global scope of data with suitable examples.
35. Explain the utility of: auto, static, extern.
36. Explain the need for call by value and call by reference for functions in C.
37. Write the function that returns <integer> sum of two integer values using i) call by value ii) call by
reference
38. Write the function that returns <double> sum of two float values using i) call by value ii) call by
reference
39. Write the function that returns <void> but stores the sum of two integers in third variable whose
address is passed as argument to the function
40. Write the function that returns <double> sum of array of real numbers.
41. Write the function that returns <double> sum of matrix of real numbers.
42. Write the function that returns <double> sum of 3D array of real numbers.
43. Write the function that returns <void> but stores the transpose of a matrix in another 2d-array
variable whose address is passed as argument to the function
44. Write the function that returns <void> but adds two matrices and stores result in third 2d-array
variable.
45. Write the function that returns <void> but accepts a matrix X and makes following replacements in
X: all diagonal=0, lower triangle=-1, upper triangle=1
46. Explain the overhead of recursive implementation of a method. [Hint: Steps involved in function call
and return]. What is benefit of recursion?
47. Write recursive functions for factorial, Fibonacci and show their trace. [hint: will involve using stack
and tree respectively]
48. Why recursion is used? What is its overhead? Write recursive function for suitable problem and
show its trace.
49. Write a recursive function to find sum of squared integers in range [1,n]. Show its trace.
50. What is Ackerman function? Write recursive definition and show trace.
51. Implement recursive function for linear search.
52. Implement recursive function for binary search.
[ Hint: rbin(a, l,u, val){ <?base case;> m=(l+u)/2; if a[m]==val return m; if a[m]<val then
rbin(a,?left_of_mid,val) else rbin(a,?right_of_mid,val) } // find expressions for ‘?’.
53. Implement recursive function to find roots of an equation using bisection method. {Refer Text Book]
54. What do you mean by static memory allocation and dynamic memory allocation? Write about
following functions- malloc(), calloc(), realloc().
55. What do you mean by dynamic arrays? Write program to show their implementation. [hint: use
calloc() and realloc()]
56. Distinguish between structure and union.
57. Write the concept, utility and implementation of self referential structures.
58. Explain various options of file open.
59. Explain utility of fwrite(), fprintf(), fscanf(), fread(),
60. What is utility of binary files? How they are implemented?
61. <Solve the Questions of MODULE IV given in lab manual>
62. <Solve “find output” and “find errors”-type questions from reference books like “Let Us C by
Kanetkar” or “Programming in C by Balaguruswami”- for programming skills and comfort with
syntax of C>

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.

You might also like