Notes for Class11

Embed Size (px)

Citation preview

  • 7/29/2019 Notes for Class11

    1/23

    Q Name the header files that shall be needed for the following code:

    void main( )

    {

    char String[ ] = .Peace.;

    cout

  • 7/29/2019 Notes for Class11

    2/23

    call by reference method, the changes are reflected back to the original values. The call by

    reference method is useful in

    situations where the values of the original variables are to be changed using a function.

    Program to illustrate the call by value method of function invoking:#include

    #include

    int change(int);void main( )

    { clrscr( );

    int orig=10;

    cout

  • 7/29/2019 Notes for Class11

    3/23

    called global variables. These variables are defined outside of any function, so they are

    accessible to all functions. These functions perform various operations on the data. They are also

    known as External Variables.

    Eg: #include

    int a,b;

    void main()

    {float f;

    ---;

    ---;

    }

    In the above program segment, a and b are global variables, we can access a and b from any function. f

    is local variable to function main( ), we can access f from main( ) only.

    Q Why main( ) function is so special. Give two reasons?

    Ans: Execution of the program starts and ends at main( ). The main( ) is the driver function of the

    program. If it is not present in a program, no execution can take place.

    Q What is the difference between #define and const? Explain with suitable example.Ans: While they both serve a similar purpose, #define and const act differently. When using #define the

    identifier gets replaced by the specified value by the compiler, before the code is turned into binary.

    This means that the compiler makes the substitution when you compile the application.

    Eg: #define number 100In this case every instance of .number. will be replaced by the actual number 100 in your code, and

    this means the final compiled program will have the number 100 (in binary).

    #define with different types of data:

    Eg: #define PI 3.14159

    Eg: #define MAX 70

    Before compilation, if the C++ preprocessor finds MAX as one word, in the source code, it replaces it

    with the number 70.

    Eg: #define SQUARE(x) x*x

    Before compilation, if the C++ preprocessor finds SQUARE(x), where x is any value in the source code,

    it replaces it with its square (ie x*x). Here a macro substitutes text only; it does not check for data types.

    On the other hand, when we use const and the application runs, memory is allocated for the constant and

    the value gets replaced when the application is run.

    Syntax: const type variable_name=value;

    Eg: const int a=10;

    The value of a constant is fixed and in the above example, the value for a in entire programis 10 only. You cannot change the value of a, since it is declared as constant.

    Difference between #define and const in declaration?1.#define: #define symbolic_constant value.

    Eg: #define number 100 //No semicolon ,no equal to symbol.

    2.const: const type variable_name=value;

    Eg: const number=100; //Semicolon, equal to symbol.

  • 7/29/2019 Notes for Class11

    4/23

    What is the purpose of using a typedef command in C++? Explain with suitable example.

    Ans: C++ allows you to define explicitly new data type names by using the keyword typedef.

    Using typedef does not actually create a new data class, rather it defines a new name for an

    existing type. This can increase the portability of a program as only the typedef statements would have to

    be changed. Typedef makes your code easier to read and understand. Using typedef can also aid in

    self documenting your code by allowing descriptive names for the standard data types.

    The syntax of the typedef statement istypedef type name;

    Where type is any C++ data type and name is the new name for this type. This defines another name for

    the standard type of C++. For example, you could create a new name for float values

    by using the following statement:

    typedef float amount;

    This statement tells the compiler to recognize amount as an alternative name for float. Now you

    could create float variables using amount. Amount_loan, saving, installment;

    Using typedef does not replace the standard C++ data type name with the new name, rather the new

    name is in addition to the existing name. You still can create float variables using float.

    Once a new name has been defined by typedef, it can be used as a type for another typedef also.

    Eg: typedef amount money;

    Now, this statement tells the compiler to recognize money as another name for amount, which itself isanother name for float. Typedef does not create any new data types rather provides an alternative

    name for standard types. Reference provides an alias name for a Variable and typedef provides

    an alias name for a data type.

    Illustrate the use of #define in C++ to define a macro.

    Ans: The #define preprocessor can be used in the creation of macros (code substitution).

    Eg: #define SQUARE(x) x*x

    Before compilation, if the C++ preprocessor finds SQUARE(x), where x is any value in the source code,

    it replaces it with its square (ie x*x). Here a macro substitutes text only; It does not check for data types.

    #define CUBE(x) SQUARE(x)*x

    In the program,cout

  • 7/29/2019 Notes for Class11

    5/23

    void area(float r)

    { cout

  • 7/29/2019 Notes for Class11

    6/23

    {

    char Temp=Message[C];

    Message[C]=Message[R];

    Message[R]=Temp;

    }

    cout

  • 7/29/2019 Notes for Class11

    7/23

    }while(condition);

    Q Define function and give example.

    Ans A function is a group of statements that is executed when it is called from some point of the program.

    Using functions we can structure our programs in a more modular way, accessing all the potential thatstructured programming can offer to us in C++.

    The following is its format:

    type name ( parameter1, parameter2, ...) { statements }

    where:

    type is the data type specifier of the data returned by the function.

    name is the identifier by which it will be possible to call the function.

    parameters (as many as needed): Each parameter consists of a data type specifier followed by an

    identifier, like any regular variable declaration (for example: int x) and which acts within the function as a

    regular local variable. They allow to pass arguments to the function when it is called. The different

    parameters are separated by commas.statements is the function's body. It is a block of statements surrounded by braces { }.

    Here you have the first function example:

    1

    2

    3

    4

    5

    67

    8

    910

    1112

    13

    14

    15

    16

    17

    18

    // function example

    #include

    usingnamespace std;

    intaddition (inta, intb)

    {

    intr;

    r=a+b;

    return (r);}

    intmain ()

    {

    intz;

    z = addition (5,3);

    cout

  • 7/29/2019 Notes for Class11

    8/23

    int add(int a, int b) // Function Definition.

    {

    return a + b;

    }

    Q Differentiate between break, continue, exit & return

    Ans break - The break statement is used to jump out of loop. After the break statement control passes tothe immediate statement after the loop. Passes control out of the compound statement.

    The break statement causes control to pass to the statement following the innermost enclosing while, do,

    for, or switch statement. The syntax is simply

    break;

    continue - Using continue we can go to the next iteration in loop.

    exit - it is used to exit the execution of program. Exit terminates the entire program. If you just want to

    stop looping, you use break. If you want to stop the current loop iteration and proceed to the next one, you

    use continue.

    note: break and continue are statements, exit is function.return: Exits the function. Return exits immediately from the currently executing function to the calling

    routine, optionally returning a value. The syntax is:

    return [expression];

    For example,

    int sqr (int x) void display(){ {

    return (x*x); cout

  • 7/29/2019 Notes for Class11

    9/23

    Q What is nested if?

    Ans It means one if statement inside another if statement or if in else.

    if( condition )

    {

    if( some other condition)

    {

    }

    else

    {

    }

    }

    else

    {

    if( another condition)

    {

    }}

    Q cWhat are types of exit?

    Ans Exit are of 2 type:- normal & abnormal exit.

    Normal exit takes place when loops test condition fails. In this case\, control is transferred to firstexecutable statement following the loop statement or nest statement of loop statement.

    Eg

    for(int i=0;i

  • 7/29/2019 Notes for Class11

    10/23

    A structure is a collection of variable which can be same or different types. You can refer to a structure as

    a single variable and to its parts as members of that variable by using the dot (.) operator. The power of

    structures lies in the fact that once defined, the structure name becomes a user-defined data type and may

    be used the same way as other built-in data types, such as int, double, char.

    struct STUDENT

    {int rollno, age;

    char name[80];

    float marks;

    };

    void main()

    {

    STUDENT s1, s3; // declare two variables of the new type

    cin>>s1.rollno>>s1.age>>s1.name>>s1.marks; //accessing of data members

    cout

  • 7/29/2019 Notes for Class11

    11/23

    a structure globally, place it BEFORE void main(). The structure variables can then be defined locally in

    main, for example

    struct STUDENT

    {

    int rollno, age;

    char name[80];float marks;

    };

    void main()

    {

    // declare two variables of the new type

    STUDENT s1, s3;

    }

    Alternate method of declaring variables of type struct:struct STUDENT

    {int rollno, age;

    char name[80];

    float marks;

    } s1, s3;

    Accessing of data members

    The accessing of data members is done by using the following format:

    structure variable.member name

    for example

    cin>>s1.rollno>>s1.age>>s1.name>>s1.marks;

    Initialization of structure variableInitialization is done at the time of declaration of a variable. For example

    STUDENT s2 = {100,17,Aniket,92};

    Structure variable in assignment statement

    The statement

    s3=s2;assigns the value of each member of s2 to the corresponding member of s3.

    Note that one structure variable can be assigned to another only when they are of the samestructure type, otherwise complier will give an error eg.

    struct X

    {

    int x,y;

    }X1,X2;

    struct Y

    {

  • 7/29/2019 Notes for Class11

    12/23

    int x,y;

    }Y1,Y2;

    X1=X2; //Correct as the data type is same

    X1=Y1; //invalid or incorrect as the data type is different

    X1=X2; X1Will assign all the data of X2 to X1

    X2

    Nested structure (Structure within structure)It is possible to use a structure to define another structure. This is called nesting of structure.

    Consider the following program

    struct DAY

    {int month, date, year;

    };

    struct STUDENT{

    int rollno, age;

    char name[80];

    day date_of_birth;

    float marks;

    };

    typedefIt is used to define new data type for an existing data type. It provides and alternative name for standard

    data type. It is used for self documenting the code by allowing descriptive name for the standard data

    type.

    The general format is:

    typedef existing datatype new datatype

    for example:

    typedef float real;

    Now, in a program one can use data type real instead of float.

    Therefore, the following statement is valid:

    real amount;

    A RRAY

    10 12

    10 12

  • 7/29/2019 Notes for Class11

    13/23

    An array is a collection of data elements of same data type. It is described by a single name and

    each element of an array is referenced by using array name and its subscript no.

    Declaration of Array

    Type arrayName[numberOfElements];

    For example,

    int Age[5] ;

    Initialization of One Dimensional Array

    An array can be initialized along with declaration. For array initialization it is required to place

    the elements separated by commas enclosed within braces.

    int A[5] = {11,2,23,4,15};

    It is possible to leave the array size open. The compiler will count the array size.

    int B[] = {6,7,8,9,15,12};

    Referring to Array Elements

    In any point of a program in which an array is visible, we can access the value of any of its

    elements individually as if it was a normal variable, thus being able to both read and modify its

    value. The format is as simple as:

    name[index]

    Examples:

    cout

  • 7/29/2019 Notes for Class11

    14/23

    cin>>age[4]; //input element 4

    Using Loop to input an Array from user

    int age [10] ;

    for (i=0 ; i>age[i];

    }

    Arrays as Parameters

    At some moment we may need to pass an array to a function as a parameter. In C++ it is not

    possible to pass a complete block of memory by value as a parameter to a function, but we are

    allowed to pass its address.

    21 www.cppforschool.com

    For example, the following function:

    void print(int A[])

    accepts a parameter of type "array of int" called A.

    In order to pass to this function an array declared as:

    int arr[20];

    we need to write a call like this:

  • 7/29/2019 Notes for Class11

    15/23

    print(arr);

    Here is a complete example:

    #include

    void print(int A[], int length)

    {

    for (int n=0; n

  • 7/29/2019 Notes for Class11

    16/23

    {

    cout

  • 7/29/2019 Notes for Class11

    17/23

    if(A[I]==Data)

    {

    cout

  • 7/29/2019 Notes for Class11

    18/23

    if(Found)

    return(Mid+1); //returning 1ocation, if present

    else

    return(-1); //returning -1,if not present

    }

    Function to Sort the array A by Bubble Sort

    void BSort(int A[], int n)

    {

    int I,J,Temp;

    for(I=0;I

  • 7/29/2019 Notes for Class11

    19/23

    Function to Sort the array ARR by Insertion Sort

    void ISort(int A[], int n)

    {

    int I,J,Temp;

    for(I=1;I

  • 7/29/2019 Notes for Class11

    20/23

    for(I=0;I

  • 7/29/2019 Notes for Class11

    21/23

    {

    cout

  • 7/29/2019 Notes for Class11

    22/23

    Function to multiply two dimensional arrays A and B of order NxL and LxM

    void Multiply(int A[][20], int B[][20], int C[][20],int N, int L, int M)

    {

    for(int R=0;R

  • 7/29/2019 Notes for Class11

    23/23

    int SumR=0;

    for(int C=0;C