ta zc142-l9

Upload: rajpd28

Post on 03-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/28/2019 TA ZC142-L9

    1/31

    TA ZC142 Computer ProgrammingLecture 9

    Date: 06/01/2013

  • 7/28/2019 TA ZC142-L9

    2/31

    Last Lecture

    Sorting TechniquesInsert an element into a Sorted Array

    Remove an element from a Sorted ArrayEnumerated data typesIntroduction to user defined data types

    Structures Union

  • 7/28/2019 TA ZC142-L9

    3/31

    Todays Agenda

    Problem Solving Technique Top Down Design

    Functions

  • 7/28/2019 TA ZC142-L9

    4/31

    4

    Content

    User-Defined FunctionsFunctions with no arguments and no returnvalueFunctions with arguments and no return valueFunctions with arguments and return valueFunctions with no arguments but returns a value

    Passing Array as an argument to a functionOne dimensionalStrings

  • 7/28/2019 TA ZC142-L9

    5/31

  • 7/28/2019 TA ZC142-L9

    6/31

    Top-down Design

    How do you solve the sub-problem?Divide-and-conquer again!

    Steps1. Divide the problem into sub-problems2. Repeat step 1 for each sub-problem

    until the problem is small enough to have anatomic solution.3. Combine the solutions.

  • 7/28/2019 TA ZC142-L9

    7/31

  • 7/28/2019 TA ZC142-L9

    8/31

    User-Defined Functions

  • 7/28/2019 TA ZC142-L9

    9/31

    Functions

    A function is a named, independentsection of C code that performs a specifictask and optionally returns a value to thecalling program/function.

    A C program consists of one or morefunction definitions, including exactly onethat must be called main().

  • 7/28/2019 TA ZC142-L9

    10/31

    Function Declaration and Definition

    A declaration merely provides a function prototype .

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

  • 7/28/2019 TA ZC142-L9

    11/31

    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 .

  • 7/28/2019 TA ZC142-L9

    12/31

    Complete C Program Using Function

    long int cube(int); /* Function declaration */int main(){ long int c;

    c = cube(5) ; /* Function call */printf(Cube of 5 = %ld,c);

    return;} /* Function definition starts from here */long int cube(int num){ long int cb; /*Local variable declaration */

    cb = num*num*num;return cb; /* Return value */

    }

    Actual parameter

    Formal parameter

  • 7/28/2019 TA ZC142-L9

    13/31

    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;

    }

  • 7/28/2019 TA ZC142-L9

    14/31

    7/12/2013 14

    Functions with no arguments and no returnvalue

    void displayline(void);void sum(void);void main(){ displayline();

    sum();}

    void displayline(){ int j;

    for(j=0;j

  • 7/28/2019 TA ZC142-L9

    15/31

    7/12/2013 15

    Functions with arguments and no return valuevoid displayline(char);void Sum(float, float);void main(){ char ch;

    float a,b; printf (Enter character to print in line \ n);

    scanf (%c,&ch );displayline(ch);

    printf (Enter value of a and b \ n); scanf (%f %f,&a,&b );Sum(a,b);

    }

  • 7/28/2019 TA ZC142-L9

    16/31

    7/12/2013 16

    Functions Definition

    void displayline(char ch){ int j;

    for(j=0;j

  • 7/28/2019 TA ZC142-L9

    17/31

    7/12/2013 17

    Functions with arguments and return value

    int Add(int, int);int Sub(int, int);int Div(int, int); /* Function Prototype */int main(){ int x,y;

    printf (Enter two values: \ n); scanf (%d %d,&x,&y );

    printf (Sum =%d, Add(x,y ) ); /*Function Call*/ printf (Subtraction = %d, Sub(x,y) ); printf (Division = %d, Div(x,y) );

    return;}

  • 7/28/2019 TA ZC142-L9

    18/31

    7/12/2013 18

    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/28/2019 TA ZC142-L9

    19/31

    7/12/2013 19

    int fact(int );int main(){

    int n=5; printf("factorial of %d = %d",n,fact(n));

    }int fact(int n)

    {int f=1,i=2;

    while(i

  • 7/28/2019 TA ZC142-L9

    20/31

    7/12/2013 20

    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/28/2019 TA ZC142-L9

    21/31

    7/12/2013 21

    Compute sine series :x x 3 /3! + x 5 /5! - ..n terms

    Write a function power(x,a) and fact(a).

  • 7/28/2019 TA ZC142-L9

    22/31

    7/12/2013 22

    Compute the roots of quadraticequation.

    ax 2 + bx + c = 0 Write a function

    quad(a,b,c,sign) tofind roots of a quadratic

    equation and call thisfunction in main().

  • 7/28/2019 TA ZC142-L9

    23/31

    7/12/2013 23

    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;

    elseprintf("Invalid entries\n");

    }root1 = quad(a,b,c,1);root2 = quad(a,b,c,0);printf("root1 = %f root2 = %f\n",root1,root2);

    }

  • 7/28/2019 TA ZC142-L9

    24/31

    7/12/2013 24

    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/28/2019 TA ZC142-L9

    25/31

    7/12/2013 25

    Passing Arrays To Functions (Pass by reference)

    One- dimensional Array:int Maximum(int a[], int size);

    main(){ int val[5]={3,5,2,7,4};

    printf (%d \ n,Maximum (val,5));}int Maximum(int a[], int size)

    { int i, max = a[0];for(i=0; i

  • 7/28/2019 TA ZC142-L9

    26/31

    7/12/2013 26

    Rules to pass an Array to a function

    1. The function must be called by passing name of the array.

    2. In the function definition, the formal parameter must be an array type; the size of the array doesnot need to be specified.

    3. The function prototype must show that theargument is an array.

  • 7/28/2019 TA ZC142-L9

    27/31

    7/12/2013 27

    Write a program using functions to sortelements in an 1D array. Write a functionto read an array, print an array and sort anarray.

    Extend the above program by adding fewmore functions to search an element in anarray, find maximum in an array, find sum

    of elements in an array and return thesum. return the results found in eachfunction to the calling function.

    void readArr(int a[] int size);

  • 7/28/2019 TA ZC142-L9

    28/31

    7/12/2013 28

    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/28/2019 TA ZC142-L9

    29/31

    Write a function to calculate the grosssalary of a employee if his basic salary isinput through the keyboard. if basicsalary is less than Rs.1500 then HRA =Rs. 500 and DA = 90% of basic. If salaryis equal or greater than 1500 then HRA =10% and DA = 95% of basic salary. Basic

    salary is passed to the function and gssalary is returned.

    Programming Exercise

  • 7/28/2019 TA ZC142-L9

    30/31

  • 7/28/2019 TA ZC142-L9

    31/31

    Any Doubts ?