CS 101B C++ Lab Manual

Embed Size (px)

Citation preview

  • 8/12/2019 CS 101B C++ Lab Manual

    1/74

    1

    Yarmouk University

    Computer Science Department

    C++ Programming

    Lab manual

    Prepared by:

    Dr. Iyad Abu Douh, Ahmed Bani-Hani, Fatima Abu-Hawas,

    Siba Shaher, Asma Alhami, Enas Abdelrazak,

    Reham Abdelqader, Maisaa Khazaleh, Nahed Mansour,

    Wafaa Qarqaz, Amal Abu Naser, Ikdam Alhami

  • 8/12/2019 CS 101B C++ Lab Manual

    2/74

    2

    Table of Contents

    L 1: V D C....... 3

    L 2: / .... 11

    L 3 C .............................................................. 18

    L 4 S

    ...................................................................................... 30

    L 5 M ( )....... 36

    Lab 6 C S: , L................... 46

    Lab 7 F.................................................................... 50

    Lab 8 , , A............ 55

    Lab 9 F ....................................... 60

    L 10 A................................................................................ 65

    L 11 ..................................................................... 70

    Lab 12 .......................................................................... 73

  • 8/12/2019 CS 101B C++ Lab Manual

    3/74

    .

    L O:

    L , ++ 6.0 , .

    F .

    , ,,.

    .

    B:

    ++ , ,

    :

    #

    ;

    ()

    //

    :

    :

    :

    :

    : .

    :

    // .

    (/*) , (*/)

    .

    :

    L ++

  • 8/12/2019 CS 101B C++ Lab Manual

    4/74

    4

    L .

    .

    A :

    , .

    L A:

    Lab assignment-1Learn the student how to open Microsoft visual c++ 6.0 : Click on the Start button.

    Choose all programs.

    Then choose Microsoft visual c++ 6.0 as shown in figure 1.

  • 8/12/2019 CS 101B C++ Lab Manual

    5/74

    5

    F 1

    2.

  • 8/12/2019 CS 101B C++ Lab Manual

    6/74

    6

    F 2

    F , 3.

  • 8/12/2019 CS 101B C++ Lab Manual

    7/74

    7

    F 3

    , 32 , ,

    .

    F 4.

  • 8/12/2019 CS 101B C++ Lab Manual

    8/74

    8

    F 4

    A , , ,

    ++ , 5.

    F 5

    L 2

    M ++ 6.0 .

    //

    #

    ;

    ()

  • 8/12/2019 CS 101B C++ Lab Manual

    9/74

    I , 6.

    Figure 6

    , , . T , .

    , ,

    7.

    Lab assignment-3What is/are the output for the following code:

    #

    ;

    ()

  • 8/12/2019 CS 101B C++ Lab Manual

    10/74

    10

  • 8/12/2019 CS 101B C++ Lab Manual

    11/74

    11

    .

    L O:

    T

    , .

    T .

    L , / , .

    A / ,

    .

    ++ .

    B:

    T ++ :

    T ++ :

  • 8/12/2019 CS 101B C++ Lab Manual

    12/74

    12

    D : E :

    :

    o C

    E C

    o

    I ,

    I ,

    I( C )

    ();

  • 8/12/2019 CS 101B C++ Lab Manual

    13/74

    1

    F1:

    :

    ++ , ,

    :

    #

    ;

    ()

    //

    ++.

    A :

  • 8/12/2019 CS 101B C++ Lab Manual

    14/74

    14

    , .

    L A:

    Lab assignment-1

    Rewrite the following in C++ Code and print it.

    I , + + + + / 5,

    :

    L 2

    (3 )

    . 364 3 6 4

    4 6 3

    : U .

    #

    ;

  • 8/12/2019 CS 101B C++ Lab Manual

    15/74

    15

    ( )

    ;

    >>;

    (>=100 &&

  • 8/12/2019 CS 101B C++ Lab Manual

    16/74

    16

    cin>>y;

    intz;

    cin>>z;

    intmax=x;

    if(max

  • 8/12/2019 CS 101B C++ Lab Manual

    17/74

    17

    }

    }

    }

    Lab Problem -3

    (BMI) BMI :

    C BMI

    .

    P BMI 1

    N I 1 2

    O I 2 2

    O I

  • 8/12/2019 CS 101B C++ Lab Manual

    18/74

    18

    Lab 3

    Class Postcard

    L O

    A , :

    D

    C C++ . C ,

    , , .

    P O

    C

    .

    T .

    B:

    N .

    :

    1. R .

    2. Q , , .

    L

    OE:P

    P

    :

    ()

  • 8/12/2019 CS 101B C++ Lab Manual

    19/74

    1

    :

    1. D "" " " .

    2.

    A ""

    " " .

    :

    .

    Classes: Class Rectangle

    B:

    A 4 90.

    H :

    T

    .

    6 4 ,

    6*424

    :

    1. .

    2. .

    3. A.

    4. 11

    L

    The following is a class called Rectangle which contains two private data elements ,

    Width and Height of type double and a number of member functions. Fill in theblanks to completethe class definition.

    #include

    ------------------

    -----------Rectangle

    {

    :--------------

    double Width;

    double Height;

  • 8/12/2019 CS 101B C++ Lab Manual

    20/74

    20

    public:

    //write a constructor that calls set_height and set_width functions to initialize

    data members

    //write a function set_height that sets the Height if it is positive, otherwise

    Height will be zero.

    //Function set_width that sets the Width if it is positive, otherwise Width willbe zero.

    //write the header of a function area that returns the area of a rectangle

    -----------------------------------------------------

    {

    return Width * Height;

    }

    void print()

    {

    //complete function print to print the data members

    }

    };

    void main()

    {

    //create an object T1 from class Rectangle of width 4 and height 5

    //call function area

    //call function print

    }

  • 8/12/2019 CS 101B C++ Lab Manual

    21/74

    21

    Class Calculator

    B:

    :

    A (+,,*,/)

    L

    :

    Completethe class definition and

    Answer the questions associated to this exercise********************************************************************

    #

    ;

    S

    :

    //

    //

    ( , )

    ;

    ( , )

    *;

    /*// */

  • 8/12/2019 CS 101B C++ Lab Manual

    22/74

    22

    ;

    ()

    =10.0;

    = 2.5;

    /* */

    /* SC*/

    /* & SC ;

    ""*/

  • 8/12/2019 CS 101B C++ Lab Manual

    23/74

    23

    2. C?

    :

    1. M C

    . A , .

    2. A A

    .

    3. A, C

    0.

    :

    1. M C . T

    .

    2. C , 0

    .

    3. .

    4. F, :

    S ; //

    .();

    .();

  • 8/12/2019 CS 101B C++ Lab Manual

    24/74

    24

    Class Triangle

    B:

    Triangles are classified by the number of equal sides they have to: scalene - all 3 sides have different lengths

    isosceles - 2 sides have equal lengths

    equilateral - all 3 sides are equal .

    I 3 ,

    ' (

    ' ) .

    ' ( +

    + ).

    A ' ( '')

    :

    S = ( + + ) 2

    Example: A triangle has side a = 4, side b = 5 and side c = 6. What is its area?The perimeter = 4 + 5 + 6 = 15.

    The semi-perimeter is one half of this or 7.5

    Using Heron's formula,

    area = square root (s (s - 4) (s - 5) (s - 6))

    area = square root (7.5 (7.5 - 4) (7.5 - 5) (7.5 - 6))

    area = square root (7.5 (3.5) (2.5) (1.5))

    area = square root (98.4375)

    area = 9.921567416...

    :

    1. .

    2. .3. W T A.

    4. T .

    5. C : 0.5, 1.5 3.

    L:

    1: C :

    public:

    T(double, double,double); //constructor

    double Area(); // calculate the area of triangle

    double perimeter (); // calculate the perimeter of triangle

  • 8/12/2019 CS 101B C++ Lab Manual

    25/74

    25

    double get_Side1 ();//return the value of the first side of triangle

    double get_Side2 ();//return the value of the second side of triangle

    double get_Side3 ();//return the value of the third side of triangle

    /*set values for the triangle sides values*/

    void set_Sides(double, double,double);

    string check_Type ();// decide the type of the triangle

    void print ();// print all information about the triangle

    private:

    double Side1;

    double Side2;

    double Side3;

    };

    2:.

    .

  • 8/12/2019 CS 101B C++ Lab Manual

    26/74

    Class Students

    B:

    T

    . I

    W

    34, 43, 81, 106, 106

    W

    34 + 43 + 81 + 1

    6

    :

    6. 7. W

    8.

    L

    1

    A

    26

    ,

    115

    06 + 106 + 115

    0

    4,12,16,225,

    1, 2,

    ,

    1, 2,

    , 6

    0

  • 8/12/2019 CS 101B C++ Lab Manual

    27/74

    27

    SETDATA SN,1,2, 3 .

    GETDATA .

    ESLT "ASS"

    50 "FAL" .

    2. I :

    S S1.

    SETDATA SN,1,2, 3

    "",22,21, 40 .

    GETDATA .

    .

    ESLT S1.

  • 8/12/2019 CS 101B C++ Lab Manual

    28/74

    28

    Class Bank Account

    B:

    T , :

    - T (, M, L).

    - ( : A, G,

    ID N)

    T

    - T

    (AB=AB+DA).

    T

    - T . I ,

    :

    AB=ABWA

    , .

    :

    9. ( , , ).

    10

    11

    12.W .L:

    1: A

    :

    D :

    1. N

    2. ID .

    3. N .

    4. B .

    M :

    1. E ( ) .

    2. .

    3. B F B.

  • 8/12/2019 CS 101B C++ Lab Manual

    29/74

    29

    4. F

    ' .

    5. F

    .

    2: :

    1. C A1 .2. C A2

    : "A A", "190129876","1234567", 120.

    3. B A2.

    3: E

    .

  • 8/12/2019 CS 101B C++ Lab Manual

    30/74

    30

    Lab 4

    .

    Lab Objectives:

    - Learn how to engineer a class to separate its interface from itsimplementation and encourage reuse.

    - Learn how to validate data members in set functions, to ensure that data inan object adheres to a particular format or is in proper value range.

    Background:

    Separating class interface from its implementation:

    Interface of the class:- Specifies what services the class permits users to use and how to requestthose services, but not specify how the services are implemented.

    - Contains the prototype of the member functions and the declaration of thedata members.

    - We place class interface in header file.Define member functions outside the class definition, in a separate source-code file:

    - In source-code file for a classo Use binary scope resolution operator (::) to tie each member

    function to the class definition

    o Implementation details are hidden

    o Client code does not need to know the implementationDriver files (source file)

    o Program used to test software (such as classes).o Contains a main function so it can be executed.

    #include preprocessor directive

    - Used to include header files- Instructs C++ preprocessor to replace directive with a copy of the contents

    of the specified file

    - Quotes indicate user-defined header files- Angle brackets indicate C++ Standard Library

    Validating Data withsetFunctions

    setfunctions can validate data Known as validity checking Keeps object in a consistent state

    The data member contains a valid value Can return values indicating that attempts were made to assign invalid

    data.

    string member functions length returns the number of characters in the string. substr returns specified substring within the string.

    Pre- Lab:

    The student must know how to:- Define a class and use it to create object.

  • 8/12/2019 CS 101B C++ Lab Manual

    31/74

    31

    - Define member functions in a class to implement the class's behaviors.- Declare data members in a class to implement class's attributes.- Call a member function of an object to make that member function

    performs its task.

    - Use a constructor to ensure that an object's data is initialized when the

    object is created.Learning Outcomes:You will:

    - Be able to write a full documented and separated interface C++ programthat deals with classes.

    - Be able to validate data members in set functions, to ensure that data in anobject adheres to a particular format or is in proper value range.

    Assessment tools:

    - Notes will be taken during the lab time.- Quizzes.

    Lab Exercises:Exercise #1:

    W ++

    Point that contains the data members x and y of type integers and the following

    member functions:

    1- Parameterized constructor that initializes x and y to any given values.2- setX function to assign new value to x between 10 and 50.3- setY function to assign new value to y, the value must be greater than or equal

    to 0.

    4- getX to return the value of x.5- print to print any point as ordered pair.Write a main function declare object p1 with the values 12 and 30 to x and y

    respectively, and test the member functions.

    The solution:

    To create a full documented and separated interface ++

    Point:

    1- Create new project of type Win32Console Application of any name.2- then add a header file name e.g. point.h to write the class interface as

    follows://file name: point.hclass Point

    {

    private:

    int x,y;

    public:

    Point(int, int);

    void setX(int);

    void setY(int);

    int getX();

    void print();

    };

  • 8/12/2019 CS 101B C++ Lab Manual

    32/74

    32

    3- then add a source file e.g. implementation.cpp to write the implementation foreach member function as follows:

    //file name: implementation.cpp

    #include

    using namespace std;

    #include "point.h"

    Point::Point(int a, int b)

    {

    setX(a);

    setY(b);

    }

    void Point::setX(int a)

    {

    x = (a>= 10 && a= 0)

    y = b;

    else y = 0;

    }

    int Point::getX()

    {

    return x;

    }

    void Point::print()

    {

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    33/74

    33

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    34/74

    4

    #include "student.h"

    Student::Student()

    {

    setName("ali");ID = 2012066034;

    setGrade(65.7);

    }

    void Student::setName(string n)

    {

    if(n.length()= 60 ? gr: 60;

    }

    void Student::print()

    {

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    35/74

    5

    3) Mul to return the result of multiplying x by y4) Div to return the result of dividing x by y5) Mod to return the result of x % y6) Constructors to initialize object data members x and

    Write the main function and define an object of type Calculator to test all the member

    functions.

    Assignment #2: Write a full documented and separated interface C++ program that

    implements classBook that contains the following:1) One data member that contains book title.2) One data member that contains book author3) One data member that contains book publisher.4) One data member that contains book ISBN5) Member function to set all data member values6) Get functions to retrieve ISBN and author name value for a given book.7) Member function to print a summary of information for a given book.

    Define a main function with objects of type Book to test the class operations.

    Assignment # 3:

    Write a full documented and separated interface C++ program that implements class

    Account that a bank might use to represent customers' bank accounts.

    - Your class should include one data member of type double to represent the account

    balance.

    - Your class should provide a constructor that receives an initial balance and uses it to

    initialize the data member. The constructor should validate the initial balance to

    ensure that it is greater than or equal to 0. If not, the balance should be set to 0 and the

    constructor should display an error message, indicating that the initial balance was

    invalid.The class should provide three member functions:

    - Function credit should add an amount to the current balance.

    - Member function debit should withdraw money from the account and should

    ensure that the debit amount does not exceed the account's balance. If it does,

    the balance should be left unchanged and the function should print a message

    indicating "Debit amount exceeded account balance."

    - Member function getBalance should return the current balance.

    Create a program that creates two Account objects and test the member functions of

    class Account.

  • 8/12/2019 CS 101B C++ Lab Manual

    36/74

    36

    (

    ).

    L O:

    T , ,

    .

    T .

    H .

    B:

    D M: E :

    I/ S:

    o C

    E

    C

    o

    I ,

    I ,

    I( C )

  • 8/12/2019 CS 101B C++ Lab Manual

    37/74

    37

    ();

    E

    ();

    F1:T /

    S:

    o

    I

    C

    B

    o C

    I

    ,

    I ,

    ( , , )

    1E .1

    2

    1

  • 8/12/2019 CS 101B C++ Lab Manual

    38/74

    38

    C 1 :

    ();

    B;

    C 2 :

    ();

    B;

    C 3 :

    ();

    B;

    .

    .

    .

    D:

    ();

    B;

    2

    3

    4

    1 I , ( ).

    2 .

    3 C .

    4 D .

  • 8/12/2019 CS 101B C++ Lab Manual

    39/74

    9

    F2: .

    :

    :

    I( C )

    ();

  • 8/12/2019 CS 101B C++ Lab Manual

    40/74

  • 8/12/2019 CS 101B C++ Lab Manual

    41/74

    41

    charletter;

    cin>>letter;

    if(letter >='A' && letter

  • 8/12/2019 CS 101B C++ Lab Manual

    42/74

    42

    if(op== '+')

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    43/74

    4

    inta;

    couta;

    intb;

    coutb;

    charop;

    coutop;

    switch(op)

    {

    case'+':

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    44/74

    44

    }

    L :

    Lab Problem-1:

    .

    Lab Problem -2

    T ,

    (). , .

    #include

    using namespacestd;

    voidmain()

    {

    intnum;

    coutnum;

    if(num%2!=0)

    if(num>10)

    if(num

  • 8/12/2019 CS 101B C++ Lab Manual

    45/74

    45

    elseif(num

  • 8/12/2019 CS 101B C++ Lab Manual

    46/74

    46

    Lab 6 ,

    Lab Objectives:

    After performing this lab, the students should be able to

    explain the basics of while and for loops and while loops

    apply the syntaxes of loop structures

    design programs using loop structures

    solve (hard) problems of repetitive nature using loop structures

    Background:Loop structures called for loops and while loops are covered as some basic control

    structures. The loop can be used to do repetitive calculations. Though human beings

    can do calculation or processing or repetitive nature by hand, it is much slower and

    tedious for the human being. Good and solid logic of loop structures can be used to

    solve such problems in a very efficient way.

    For example, in order to compute the sum of 1 + 2 + 3 + + 100, we can do easily

    using the following code:

    int sum = 0;

    for (int j = 1; j

  • 8/12/2019 CS 101B C++ Lab Manual

    47/74

  • 8/12/2019 CS 101B C++ Lab Manual

    48/74

    48

    cout 1 is a prime number, theoretically we need to verify that

    for i = 2, 3, 4, 5, .., up to n-1, none of them divides n.

    #include

    using namespace std;

    void main()

    {

    int x;

    cin>>x;

    bool isPrime=true;

    int i=2;

    while(i

  • 8/12/2019 CS 101B C++ Lab Manual

    49/74

    4

    #include

    using namespace std;

    void main()

    {

    int x,y;//x is base , y is power

    cin>>x>>y;int result=1;

    for(int i=1;i

  • 8/12/2019 CS 101B C++ Lab Manual

    50/74

    50

    Lab 7

    Functions.

    Lab Objectives:

    After completing this lab, the students should be able to

    Explain the concepts of functions.

    use common math functions available in the C++ Standard Library.

    Explain what a function prototype is and how that is different from thefunction definition.

    Convert the code processing in the main function to a function calledfrom the main function.

    Create functions with multiple parameters.

    The mechanisms for passing information between functions andreturning results.

    Background:The function is a good mechanism to encapsulate code used repeatedly in a

    program so that it can be called from other parts of the code. A function does not use

    a keyword called function but instead the programmer has to define function

    prototype before the main function and then define the function again later.

    A function has the following format:

    type function_name (optionalparameter list){

    function code;

    return value;

    }

    Return data typeof function is in general the types of C++ variable types including

    int, double, char etc.

    The function does some processing and the calculated value is returned using the

    return value.

    In the main function or the other functions calling this function_name, the value

    returned is used like the instruction:calling_value = function_name (parameters);

    A function does not need to always return a value. A function not returning a value

    can omit the return statement and the function type is void in this case.

    Function prototype has the following format:

    type function_name (list of variable types);

    Examples are:

    Example 1:

    int compute_sum (int);

  • 8/12/2019 CS 101B C++ Lab Manual

    51/74

    51

    Example 2:

    void tryout ();

    Function prototypes differ from the function definitions in two places: there is nocode (no {} with code in between) and the variable names do not follow the types.

    A function prototype can return nothing, in which case void is the type returned; also

    it may have no parameters at all like example 2 above.

    The function prototype is declared before the main function with the function calls

    inside the main function or the other functions calling this function.

    The function definitions are put after the main function.

    C++ Standard Library : is a collection of functions, which are written in the core

    language and part of the C++ ISO Standard itself to provides several generic functions

    that utilize and manipulate these containers, function objects, generic strings andstreams (including interactive and file I/O), support for some language features, and

    everyday functions for tasks such as finding the square root of a number. Using sqrt

    in header file.

    Example 3:

    Pre-labs:

    In order to do this lab, you should know how to write, compile, and run a C++ program, and

    you should understand what functions are and how to invoke them. You should also befamiliar with an editor

    Assessment tools:

    The assessment is according to the student participation in the lab if the student solves the

    exercise, he will get the participation mark for this lab.

  • 8/12/2019 CS 101B C++ Lab Manual

    52/74

    52

    Lab Assignments:

    Lab assignment-1

    Consider the following code that computes the sum of 1 + 2 + 3 + .. + 100 by a for

    loop.

    #include

    using namespace std;

    int main ( ) {

    int sum = 0;

    for (int j = 1; j

  • 8/12/2019 CS 101B C++ Lab Manual

    53/74

    5

    Lab assignment-2:

    .

    Lab assignment-3: .

    .

    Lab Problems:

    Lab Problem-1

    ,

    . ( ) .

  • 8/12/2019 CS 101B C++ Lab Manual

    54/74

    54

    Lab Problem-2

    IC

    , . T

    IC .(

    ).

    Lab Problem-3

    C++ 3

    M . 345 8.

  • 8/12/2019 CS 101B C++ Lab Manual

    55/74

    55

    Lab 8 , ,

    Lab Objectives:

    After performing this lab, the students should be able to:

    Solve problems that needs to generate random numbers

    Realize where an identifier can be referenced in a program and why

    Use default arguments when needed in a function

    Background:

    Rand() is a function generates an unsigned integer between 0 and

    RAND_MAX (a symbolic constant defined in header file )

    If we want a random number in the range [a, b] we can use the formula

    a + rand() % (b a + 1)

    Cube_volume = side_length * side_length * side_length

    If the cylinder has a radius r and length (height)h, then its volume isgiven by

    V = r2h , = 3.14

    Pre-lab:

    The student must know how to

    Write functions and call them

    Trace and try to find output

    Include the necessary header files if needed

    Assessment tools:

    Notes will be taken during the lab time

    Quizzes

    Lab Assignments:

  • 8/12/2019 CS 101B C++ Lab Manual

    56/74

    56

    Lab assignment-1

    100

    51 10 .

    .

    #

    #

    ;

    ()

    ( = 1;

  • 8/12/2019 CS 101B C++ Lab Manual

    57/74

    57

    // F B

    1 = 5;

    2 = 8;

    3 = 4;

    // B

    1 = 6;

    2 = 9;

    // B

    1 = 7;

  • 8/12/2019 CS 101B C++ Lab Manual

    58/74

    58

    C++ 1

    .

    # ;

    C( L =1.0);

    ()

  • 8/12/2019 CS 101B C++ Lab Manual

    59/74

    5

    4. 1 ,

    5.

    :: = :: + 2* ;

  • 8/12/2019 CS 101B C++ Lab Manual

    60/74

    60

    Lab 9

    Lab Objectives:

    After performing this lab, the students should be able to:

    Understand the idea of Overloading Functions and be able to write an

    overloaded function.

    The student should be prepared to understand the idea of operator

    overloading in general

    Understand the idea of recursion and compare it to the iterative paradigm

    Trace a recursive function

    The student should know how to write a recursive function

    Should be able to convert any program written using loops into a recursive

    one

    Background:

    Function signature is both the name and parameter list of that function To overload a function is to write at least two functions at the same

    scope(workspace)with different signatures[i.e. the same name but with

    different parameter list (make the parameters in every list differ in types

    ,number, or appearance) ]

    A recursive function is a function that calls itself.

    Pre-lab:

    The student must know how to

    Write functions and call them

    Trace and try to find output

    Assessment tools:

  • 8/12/2019 CS 101B C++ Lab Manual

    61/74

    61

    Examples will be taken during the lab time

    Quizzes

    Lab Assignments:

    Lab assignment-1

    W

    , , . T .

    #

    ;

    ( )

    **3.14;

    ( )

    *;

    ( , )

    *;

    ()

  • 8/12/2019 CS 101B C++ Lab Manual

    62/74

    62

    (1)*

    ()

  • 8/12/2019 CS 101B C++ Lab Manual

    63/74

    63

  • 8/12/2019 CS 101B C++ Lab Manual

    64/74

    64

    Lab Problems:

    Lab Problem-1

    in Lab assignment-4

    ( ).

    Lab Problem -2

    ++

    13

    .

    Lab Problem -3

    C

    .

    Lab Problem -4

    C .

    * ( , )

  • 8/12/2019 CS 101B C++ Lab Manual

    65/74

  • 8/12/2019 CS 101B C++ Lab Manual

    66/74

    66

    void PrintArray(int Array[], int ArraySize){

    for (int i = 0; i

  • 8/12/2019 CS 101B C++ Lab Manual

    67/74

    67

    Lab Assignments:

    Lab assignment-1:Fill in the blanks

    Complete the following program below by filling in the blanks. (Read

    comments for assistance. )

    // bubble.cpp#include using namespace std;

    void bubbleSort(int array[], int size); // bubble sortvoid swap(int &x, int &y); // helper function of bubbleSortvoid printArray(int array[], int size); // print array

    int main(){

    const int MAX = 10;

    /* Declare an array called value of size MAX with

    elements: 20, 15, 18, 19, 22, 11, 9, 7, 10, 3*/

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    68/74

    68

    Lab assignment-2: Storing quizscores

    Write a program that allows a user to enter 6 quiz scores to calculate and print

    the average for these quizzes.

    #include

    using namespace std;

    int main() {int scores[6];int total = 0;

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    69/74

    6

    Lab Problem -2: search array for a key value

    Write a function that searches an array for a "key value" and returns the array's

    index for that element. If the element is not found, then the function should

    return -1. Write a driver program that correctly tests this function. The drivershould print the array and show the value of the search result.

    Lab Problem -3: Minimum array entry

    With a function that take as parameters a 2D array and the capacity of the

    array.your function has to find the minimum entry in a 2D array .write a driver

    program that reads array values and finds the minimum entry using your

    function.

  • 8/12/2019 CS 101B C++ Lab Manual

    70/74

    70

    Lab Objectives:

    In this lab students will learn:

    1. Memory concept of variables, pointers and how to use variable identifiers andpointers to refer to the variable.

    2. Pointer variable declarations and initialization.3. Direct and indirect referencing a variable using the pointer operators.4. Using * and address (&) operators.

    Background:

    When declaring a variable, it is located at a specific location in memory,the memory

    address. The task of locating variables is automatically performed by the operatingsystem during runtime. In some cases we need to know the address where the variable

    is being stored during runtime.

    Variable which stores a reference to another variable is called a pointer. We can

    directly access the value stored in the variable using a pointer which points to it.

    Syntax:

    1. Pointer Declaration:

    Syntax: Pointer_type *Pointer_name;

    Example: int *Ptr1; double *Ptr2;

    2. Pointer initialization:

    Syntax: Pointer_name=NULL;

    Pointer_name=&variable_name;

    Example: int *Ptr1, var;

    Ptr1=&var;

    Assessment tools:

    A

    M L

  • 8/12/2019 CS 101B C++ Lab Manual

    71/74

    71

    1. Evaluation for lab exercises.

    2. Quizzes.

    Lab Assignments:

    Lab assignment-1:

    Write a c++ program that defines an integer variable var1and a pointer Ptrthat

    points to var1. Assign and print value to var1, then assign and print a new value to

    var1 using Ptr.

    Solution:

    #include

    using namespace std;

    void main()

    {

    int var1,*ptr;

    ptr=&var1;

    var1=10;

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    72/74

    72

    Solution:

    Lab Problems:

    Lab Problem-1: Correct the errors in the following program.

    Lab Problem -2: Write a c++ program that use pointers to swap two

    integer values.

    #include

    using namespace std;

    void main()

    { int n,*ptr=&n;

    cin>>n>>ptr;

    *ptr=n-2;

    if (*ptr>=0) *n++;

    else

    cout

  • 8/12/2019 CS 101B C++ Lab Manual

    73/74

    73

    Lab 12 .

    L O:

    T F I/ fstream ++ F /W

    T ,

    .

    T .

    T

    T

    T

    B:

    :

    S / ++ ++.

    A :

    T

    , .

    L A:

    Lab assignment-1Create an empty text file using c++ program and set its location

    #include

    int main()

    {fstream file_op("c:\\test_file.txt",ios::in);

    file_op.close();

    return 0;

    }

    L 2Assign value to an empty text file using c++ program

    #includeint main()

  • 8/12/2019 CS 101B C++ Lab Manual

    74/74

    {

    ofstream file_op("c:\\test_file.txt",ios::in);

    int x=5;

    file_op ;