Professional Documents
Culture Documents
Structured Programming
• It makes use of the control structures :
– sequence, selection and repetition
• Structured programming does not use GO TO
commands.
• Structured Design
– program should be designed from the top-down or
bottom-up as a hierarchical series of modules.
• A logical way of partitioning or subdividing a program
so that each module performs one or a small number of
related tasks.
Top-Down Design
• The process of breaking the overall procedure into
modules and then subdivide each component module
until the lowest level of detail has been reached.
• The payroll system of a company can contain the
following modules or tasks:
– Master file
– Earnings
– Deductions
– Taxing
– Net earning
– Print reports
Stepwise Refinement
• Top-down design strategy.
• The program is developed by successively refining
levels of procedural detail.
• This successive refinement of specifications terminates
when all instructions are expressed in terms of any
underlying computer or programming language.
• Every refinement step implies some design decisions.
It is important that the programmer is aware of the
underlying criteria.
Type Conversions in C
Type Conversion
Function
#include<conio.h>
void main()
{
int a=10,b=20;
int sum();
int c=sum(); /*actual arguments
printf(“sum is %d”,c);
getch();
}
int sum() /*formal arguments
{
int x=10,y=30;
return(x+y); /*return value
}
Argument passing techniques
• Call By Value
• Call By Reference
Call By Value
• It is a default mechanism for argument passing.
• When an argument is passed by value then the copy of
argument is made know as formal arguments which is
stored at separate memory location
• Any changes made in the formal argument are not
reflected back to actual argument, rather they remain
local to the block which are lost once the control is
returned back to calling program
Example
void main()
{
int a=10,b=20;
void swap(int,int);
printf(“before function calling %d %d”,a,b);
swap(a,b);
printf(“ after function calling %d %d”,a,b);
getch();
}
void swap(int x, int y)
{
int z;
z=x;
x=y;
y=z;
printf(“value is %d, %d”, x,y);
}
Output:
before function calling a=10 b=20
value of x=20 and y= 10
after function calling a=10 b=20
Call By Reference
Void main()
{
int a=10 ,b=25;
void swap(int *,int *);
printf(“before function calling %d %d”,a,b);
swap(&a,&b);
printf(“after function calling %d %d”,a,b); getch();
}
void swap(int *x,int *y)
{
int z;
z=*x;
*x=*y;
*y=z;
Printf(“value is %d %d”,*x,*y);
}
Output:
42
Recursion
• Recursion is a technique that solves a problem by
solving a smaller problem of the same type.
• A recursive function is a function invoking itself, either
directly or indirectly.
• Recursion can be used as an alternative to iteration.
• Recursion is an important and powerful tool in problem
solving And programming.
• Recursion is a programming technique that naturally
implementthe divide-and-conquer problem solving
methodology.
43
The Nature of Recursion
1. One or more simple cases of the problem (called the
stopping cases or base case) have a simple non-recursive
solution.
2. The other cases of the problem can be reduced (using
recursion) to problems that are closer to stopping cases.
3. Eventually the problem can be reduced to stopping cases
only, which are relatively easy to solve.
In general:
if (stopping case)
solve it
else
44
reduce the problem using recursion
Four Criteria of A Recursive Solution
1. A recursive function calls itself.
– This action is what makes the solution recursive.
3. A test for the base case enables the recursive calls to stop.
– There must be a case of the problem (known as base case or stopping case) that is
handled differently from the other cases (without recursively calling itself.)
– In the base case, the recursive calls stop and the problem is solved directly.
1. Linear Recursion
2. Binary Recursion
3. Tail Recursion
4. Mutual Recursion
5. Nested Recursion
MCQs
1. What error would the following function give on compilation ?
f(int a, int b)
{
int a;
a = 20;
return a;
}
1. Missing parentheses in return statement
2. Function should be defined as int f(int a, int b)
3. Re-declaration of ‘a’
4. No error
1. Call by value
2. Call by function
3. Both of these
4. None of these