Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Last Lecture
Sorting Techniques Insert an element into a Sorted Array Remove an element from a Sorted Array Enumerated data types Introduction to user defined data types
Structures Union
Todays Agenda
Problem Solving Technique
Top Down Design
Functions
Content
User-Defined Functions
Functions with no arguments and no return value Functions with arguments and no return value Functions with arguments and return value Functions with no arguments but returns a value
Top-down Design
Design Approach Principle
Divide and Conquer
Steps
Divide the problem into sub-problems Solve the sub-problems Combine the (sub-)solutions
Top-down Design
How do you solve the sub-problem? Divide-and-conquer again! Steps
1. Divide the problem into sub-problems 2. Repeat step 1 for each sub-problem until the problem is small enough to have an atomic solution. 3. Combine the solutions.
Task
Subtask1
Subtask2
Subtask3
Subtask1.1
Subtask1.2
User-Defined Functions
Functions
A function is a named, independent section of C code that performs a specific task and optionally returns a value to the calling program/function. A C program consists of one or more function definitions, including exactly one that must be called main().
It includes the return type and the list of parameters. Syntax: return_type function_name( arg-typ name-1,...,arg-typ name-n);
Example: int hex(int val);
Function Parameters
There are two types of function parameters: Formal parameters:
Appear in a declaration or a definition of a function.
Actual parameters:
Appear in a call to the function.
Examples: int f(int x); here x is a formal parameter i = f(3); here 3 is the actual parameter corresponding to the formal parameter.
What will be the output??? int Addnum(int, int); int main() { int a = 10,b = 20; printf("%d %d %d",a,b,Addnum(a,b)); return; } int Addnum(int a, int b) { int c; c = a+b; return c; }
Functions Definition void displayline(char ch) { int j; for(j=0;j<40;j++) printf(%c,ch); } void Sum(float x, float y) { printf(Sum = %f ,(x+y)); }
7/12/2013 16
Function Definition
int Add(int a,int b) { return(a+b); } int Sub(int a,int b) { return(a-b); } int Div(int a,int b) { return(a/b); }
7/12/2013 18
int fact(int ); Function for computing Factorial of a number int main() { int n=5; printf("factorial of %d = %d",n,fact(n)); } int fact(int n) { int f=1,i=2; while(i<=n) { f = f * i; i++; } return(f); }
7/12/2013 19
No arguments but returns a value int ReadNumber(void); void main() { int num = ReadNumber(); printf(%d,num); } int ReadNumber(void) { int number; scanf(%d,&number); return(number); } Example: getchar()
7/12/2013 20
Compute sine series : x x3/3! + x5/5! - ..n terms Write a function power(x,a) and fact(a).
7/12/2013
21
ax2 + bx + c = 0
Write a function quad(a,b,c,sign) to find roots of a quadratic equation and call this function in main().
7/12/2013
22
float quad(int a,int b,int c,int sign); int main() { int a,b,c; float root1,root2; int flag=0; while(!flag) { printf("enter a,b,c \n"); scanf("%d %d %d",&a,&b,&c); if((b*b)>(4*a*c) && a != 0) break; else printf("Invalid entries\n"); } root1 = quad(a,b,c,1); root2 = quad(a,b,c,0); printf("root1 = %f root2 = %f\n",root1,root2); }
7/12/2013 23
float quad(int a,int b,int c,int sign) { float disc,r; disc = b*b - 4*a*c; if(sign) { r =(-b + sqrt(disc))/(2*a); return(r); } else { r =(-b - sqrt(disc))/(2*a); return(r); } }
7/12/2013 24
the array.
2. In the function definition, the formal parameter
must be an array type; the size of the array does not need to be specified.
3. The function prototype must show that the
argument is an array.
7/12/2013
26
Write a program using functions to sort elements in an 1D array. Write a function to read an array, print an array and sort an array.
Extend the above program by adding few more functions to search an element in an array, find maximum in an array, find sum of elements in an array and return the sum. return the results found in each function to the calling function.
7/12/2013 27
void readArr(int a[],int size); void printArr(int a[],int size); void sortArr(int a[],int size); int main() { int a[5]; printf("Enter elements in an array\n"); readArr(a,5); printf("Display elements in Array before sorting\n"); printArr(a,5); sortArr(a,5); printf("Display elements in Array after sorting\n"); printArr(a,5); } 7/12/2013
28
Programming Exercise
Write a function to calculate the gross salary of a employee if his basic salary is input through the keyboard. if basic salary is less than Rs.1500 then HRA = Rs. 500 and DA = 90% of basic. If salary is equal or greater than 1500 then HRA = 10% and DA = 95% of basic salary. Basic salary is passed to the function and gs salary is returned.
START
INPUT bs
YES
is bs < 1500
NO
Gs = bs + hra + da PRINT GS
STOP
Any Doubts ?