c++ ppt2

Upload: sheeba-dhuruvaraj

Post on 05-Apr-2018

215 views

Category:

Documents


0 download

TRANSCRIPT

  • 7/31/2019 C++ ppt2

    1/52

  • 7/31/2019 C++ ppt2

    2/52

    Integer array of 4 elements

    1000 1002 1004 1006

    ARRAYSAn array is a collection of variables ofsimilardatatype.

    A specific element in an array is accessed by an index.

    Arrays may have from one to several dimensions.

    In C/C++, all arrays consist of contiguous memorylocations.

    The lowest address corresponds to the first elementand the highest address to the last element.

  • 7/31/2019 C++ ppt2

    3/52

    Single dimension Arrays The general form declaring an array is

    [size];

    Eg:

    int marks[5];

    The values are accessed by the indexes.

    Eg:

    marks[0]=90;marks[1]=95;

    marks[2]=98;

    marks[3]=85;

    marks[4]=80;

  • 7/31/2019 C++ ppt2

    4/52

    Example/* Program to use arrays */

    #include

    #include

    void main(){

    int arr[5]; //declaring array variable of size 5

    clrscr();

    for(int i=0;i

  • 7/31/2019 C++ ppt2

    5/52

    Memory mapping

    arr[0] arr[1] arr[2] arr[3] arr[4]

    1 2 3 4 5

  • 7/31/2019 C++ ppt2

    6/52

    Two dimensional Array C/C++ supports multidimensional arrays.

    The simplest form of the multidimensional array is the

    two-dimensional array. The declaration syntax is

    [size][size];

    LeftIndex Right Index

  • 7/31/2019 C++ ppt2

    7/52

    Example/* Program for two dimensional array */

    #include

    #include

    void main()

    { int t, i, num[3][4];

    for(t=0; t

  • 7/31/2019 C++ ppt2

    8/52

  • 7/31/2019 C++ ppt2

    9/52

    Multi dimensional Array

    C/C++ allows arrays of more than two dimensions.

    The exact limit, if any, is determined by the compiler.

    The general form of a multidimensional arraydeclaration is

    [size1][size2][size3][sizen];

  • 7/31/2019 C++ ppt2

    10/52

    Example/* Matrix Addition */#include

    #include

    void main()

    {

    int mat1[3][3];

    int mat2[3][3];

    int i,j;

    cout

  • 7/31/2019 C++ ppt2

    11/52

    for(i=0;imat2[i][j];

    }

    int mat[3][3],k,l;

    cout

  • 7/31/2019 C++ ppt2

    12/52

    Array Initialization C/C++ allows the initialization of arrays at the time of

    their declaration.

    Syntax

    [size1]. . .[sizeN] = {value_list};

    Eg:

    int i[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

    Thevalue_list is a comma-separated list of values whose

    type is compatible with data type.

  • 7/31/2019 C++ ppt2

    13/52

    Array InitializationWe can initialize the multi dimensional arrays also.

    Eg:int sqr[3][2] = {1,1,2,4,3,9};

    Another way

    int sqr[3][2] = {{1,1},{2,4},{3,9}};

    This is called Sub aggregate Grouping

  • 7/31/2019 C++ ppt2

    14/52

  • 7/31/2019 C++ ppt2

    15/52

    Pointers

    Pointers provide the means by which functions can

    modify their calling arguments.

    Pointers support dynamic allocation.

    Pointers can improve the efficiency of certain routines.

  • 7/31/2019 C++ ppt2

    16/52

    What are pointers?

    Apointeris a variable that holds a memory address.

    This address is the location of another object (typicallyanother variable) in memory.

    Memory Address

    1000

    1001

    1002

    1003

    1004

    Variable in memory

    1003

    .

    .

    .Memory

  • 7/31/2019 C++ ppt2

    17/52

    Declaration of Pointer

    Syntax:

    *;

    The base type of the pointer defines what type ofvariables the pointer can point to.

  • 7/31/2019 C++ ppt2

    18/52

    Pointer Operators There are two special pointer operators: *and &. The &(Referencing Operator) is a unary operator that

    returns the memoryaddress of its operand.

    m=&count; * (De-referencing Operator) is the complement of&.

    It is a unary operator that returns thevalue located atthe address that follows.

    q=*m;

    count m q

    2000

    1002

    1002 2000

  • 7/31/2019 C++ ppt2

    19/52

    Example/* Program for pointers */

    #include

    #include

    void main()

    {

    clrscr();

    int a,*p;

    couta;

    p=&a;

    cout

  • 7/31/2019 C++ ppt2

    20/52

    The void type of pointer is a special type of pointer.

    In C++, void represents the absence of type.

    So void pointers are pointers that point to a valuethat has no type.

    Void Pointers

  • 7/31/2019 C++ ppt2

    21/52

    int a;

    int v1;

    void *ptr;

    a=100;

    ptr=&a;

    v1=*(int*)ptr; //defining the type

  • 7/31/2019 C++ ppt2

    22/52

    Example-1

    int a,v1;char b,v2;

    void *ptr;

    a=100;ptr=&a;

    v1=*(int*)ptr; //now it s pointing to integer

    cout

  • 7/31/2019 C++ ppt2

    23/52

    Example-2

    #include#include

    void display(int x,void *y)

    {

    int a;

    char b;if(x==1)

    {

    a=*(int*)y;

    cout

  • 7/31/2019 C++ ppt2

    24/52

    void main(){

    int s1;

    char s2;

    void *ptr1;

    clrscr();s1=300;

    ptr1=&s1;

    display(1,ptr1); //function call with int value

    s2='J';

    ptr1=&s2;display(2,ptr1);//function call with char value

    getch();

    }

  • 7/31/2019 C++ ppt2

    25/52

    Pointer ExpressionAssignment

    int var,*p1,*p2;

    p1=&var;

    p2=p1;

    In this pointers p1 and p2pointsto the samevariablevar.

    Arithmeticint var,*p;

    p=&var;

    p++; // or p--;

    In this pointer moves to the next memory location

  • 7/31/2019 C++ ppt2

    26/52

    Pointer with Array Syntax:

    int *ptr;

    int sample[5];ptr=sample;

    It can also use & operator to specify the address of

    1st element.ptr=&sample;

    Note: Pointers can be arrayed like any other data type.

  • 7/31/2019 C++ ppt2

    27/52

    Multiple Indirection Multipleindirection or pointerstopointers is a pointer

    point to another pointer that points to the target value.

    Eg:

    float bal;

    float *balance,**newbalance;

    balance=&bal;

    newbalance=&balance;

    Multiple indirection can be carried on to whateverextent required.

    More than a pointer to a pointer is rarely needed.

  • 7/31/2019 C++ ppt2

    28/52

    Functions

  • 7/31/2019 C++ ppt2

    29/52

    Function

    Function is basic requirement of modularprogramming.

    The process of dividing a large program into small subprograms and manipulating them independently isknown as modular programming.

    A function is a named, independent, block ofstatements that perform well defined, specific taskand may return a value.

  • 7/31/2019 C++ ppt2

    30/52

    Types of Function Pre-defined functions User defined functions

    Pre-defined functions

    The functions, which has special type ofmeaning which isalreadydefined and stored, is called built-in functions orpre-defined functions.

    Eg:

    int a=4;x=pow(a,2);

    User defined functions

    Can be createdbytheuseror programmer for performing

    specific task.

  • 7/31/2019 C++ ppt2

    31/52

    Any C++ program contains at least one function.

    If a program contains only one function, it must be main( ). If a C++ program contains more than one function, then one (and

    only one) of these functions must be main( ), because program

    execution always begins with main( ).

    There is no limit on the number of functions that might bepresent in a C++ program.

    Each function in a program is called in the sequence specified bythe function calls in main( ).

    After each function has done its thing, control returns to main().

    When main( ) runs out of function calls, the program ends.

  • 7/31/2019 C++ ppt2

    32/52

    Structure of Function(arg 1,

    arg 2,,arg n)

    {

    declaration part;

    statements;

    return;}

  • 7/31/2019 C++ ppt2

    33/52

    Exampleint sum(int x,int y)

    {

    int result;

    result = x + y;

    return (result);

    }

  • 7/31/2019 C++ ppt2

    34/52

    Properties

    Every function has a uniquename. This name is used tocall the function from main() function.

    A function can be calledfrom within anotherfunction.

    A function is independent and it can perform its taskwithout intervention from or interfering with other partsof the program.

    A function returns a value to the calling program. This isoptional and depends upon the task.

  • 7/31/2019 C++ ppt2

    35/52

    Example#include

    void main()

    {

    cout

  • 7/31/2019 C++ ppt2

    36/52

    brazil()

    {

    cout

  • 7/31/2019 C++ ppt2

    37/52

    Rules A function gets called when the function name is followed

    by a semicolon.main()

    {

    func();

    }

    A function is defined when function name is followed by apair of braces in which one or more statements may bepresent.

    func(){

    statement 1 ;

    statement 2 ;

    statement 3 ;

    }

  • 7/31/2019 C++ ppt2

    38/52

    Rules

    A function can be called any number of times.

    main()

    {display() ;

    display() ;

    }

    display()

    {

    cout

  • 7/31/2019 C++ ppt2

    39/52

    Rules

    The order in which the functions are defined in a programand the order in which they get called need not necessarilybe same.main()

    {

    display1() ;display2() ;}

    display2(){

    cout

  • 7/31/2019 C++ ppt2

    40/52

    Rules

    A function can call itself. This is known as Recursion.

    int fact(int n)

    {

    if(n==0)

    return(1);

    return(n*fact(n-1));

    }Note: This function is known as RecursiveFunction.

  • 7/31/2019 C++ ppt2

    41/52

    Parts of the Function

    Function prototype declaration

    Definition of a function

    Function call

    Actual and formal arguments

    The return statement

  • 7/31/2019 C++ ppt2

    42/52

    Parts of the Function Function prototype:

    It helps the compiler to check the return and argumenttypes of the function.

    Function prototype declaration consists of Function return type

    Name

    Arguments list

    Ex:void show(void);

    int sum(int,int);

    float sum(float,int);

  • 7/31/2019 C++ ppt2

    43/52

    Parts of the Function

    Function Definition:

    The block of statement followed by functiondeclarator is called as function definition.

    The declarator and function prototypedeclaration should match each other.

    Function Call:A function is called by its name followed by

    arguments list enclosed in parenthesis andterminated by semi-colon.

  • 7/31/2019 C++ ppt2

    44/52

    Parts of the FunctionActual and formal arguments:

    The arguments declared in calling function arecalled actual arguments.

    The arguments declared in function declaratorare called formal arguments.

    The return statement:

    The return statement is used to return the value tocaller function.

    The return statement returns only one value at atime.

    Syntax : return (variable name);

  • 7/31/2019 C++ ppt2

    45/52

    void main()

    {

    int sum(int,int);

    int a=10,b=20,c;

    c=sum(a,b);

    }

    int sum(int x,int y)

    {

    return(x+y);

    }

    Function Prototype

    Function call

    Actual arguments

    Function declarator

    Function definition

    Return statement

    Formal arguments

    Parts of the Function

  • 7/31/2019 C++ ppt2

    46/52

    Types of Function

    A function may belong to any one of the followingcategories:

    1. Functions with no arguments and no return values.

    2. Functions with arguments and no return values.

    3. Functions with arguments and return values.

    4. Function with no arguments and return values.

  • 7/31/2019 C++ ppt2

    47/52

    Functions with no arguments and no return values This is the simplest function ,it does not receive any

    arguments from the called function and does notreturn any value from calling function.

    Types of Function

  • 7/31/2019 C++ ppt2

    48/52

    Functions with arguments and no return values This function receives arguments from calling

    function and does not return value to the callingfunction.

    Types of Function

  • 7/31/2019 C++ ppt2

    49/52

    Functions with arguments and return values This function receives arguments from calling

    function and returns the computed value back to thecalling function.

    Types of Function

  • 7/31/2019 C++ ppt2

    50/52

    Functions with no arguments and return values

    Types of Function

  • 7/31/2019 C++ ppt2

    51/52

    Function Call

    There are two ways to call a function.

    1. Call byValue

    2. Call byReference

  • 7/31/2019 C++ ppt2

    52/52