c++lab manual_my

Upload: chandrabhan-pradhan

Post on 08-Apr-2018

229 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/7/2019 C++Lab Manual_my

    1/19

    EXPERIMENT NO. -1

    Aim:- . Write a Program to check whether number is prime or not.

    Objective:-

    After this experiment you should be able to

    i) Explain about for loop.ii) Explore the difference between a division & modular division.iii) Know about input and output streams.iv) Differentiate between a C & C++ files.

    Brief Theory:-a) C & C++

    Actually c is a procedural programming language which cann't face the real world problem. It hassome drawback like a global data is shared by all function and if in a large program it is find outdifficult that which function uses which data.

    On the other hand c++ is an object oriented programming language which eliminate some pit fall ofconventional or procedural programming language. It is a concept or approach for designing a newsoftware. It is nothing to do with any programming language although a programming languagewhich support the oops concept to make it easier to implement.

    b) We should know that the prime number is a number which is divisible by 1 or itsel f .

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    i) Difference between procedural & Object oriented programming.ii) Basic concepts of OOPs.iii) Good Link to understand OOPs concept:-

    a. http://www.exforsys.com/tutorials/c-plus-plus/basic-concepts-of-oops-and-structure-of-c-program.html

    b. http://www.cplusplus.com/reference/iostream/

    Experiment:-

    Procedures:-

    i) Ask User to input any random number.ii) Put the condition cheeking Logic on the inputted number .iii) Find out the Prime number and output it.

    Comments:- (Write your new observation/ learning during this practical).

  • 8/7/2019 C++Lab Manual_my

    2/19

    Output:-

    Lab- Assignment:-

    i) Now take a range of numbers and find out the prime numbers betweenthose.ii) Also count the number of prime numbers.

    Viva Questions:-

    iii)iv) Explain the local and global variable?v) Explian the concept of if-else statement.vi) Explain Nested if-else.vii) When should we use local and global variable?viii) Differentiate between the Procedural and Object Oriented Programming

    language?

  • 8/7/2019 C++Lab Manual_my

    3/19

    EXPERIMENT NO. -2

    Aim:- . Write a Program to read number and to display the largest value between:

    A. Two number B. Three Numbers C. Four number by using switch-case statements.

    Objective:-

    After this experiment you should be able to

    i) Explore the concept of switch case.ii) Explain the cascaded input.iii) Know about conditional operators.iv) Create good user interface by providing multiple options to user.

    Brief Theory:-

    a)Two numbersx=(a>b)?a:b

    b)Three numberx=(a>b)?a:b;x=(x>c)?x:c;

    c)Four numberx=(a>b)?a:b;x=(x>c)?x:c;x=(x>d)?x:d;

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    i) Structure of a switch case.ii) Ternary operator.iii) Link for switch case example-

    http://www.cprogramming.com/tutorial/lesson5.html

    Experiment:-

    Procedures:-

    i) Provide user to choose an operation of selecting largest number among one,two three, or four..

    ii) Take the number of inputs accordingly.iii) Put the logic given above and find out the largest number.

    Comments:- (Write your new observation/ learning during this practical).

  • 8/7/2019 C++Lab Manual_my

    4/19

    Output:-

    Lab- Assignment:-

    i) Try the same thing in getting out the smallest number.ii) Use if conditions instead of ternary operators.

    Viva Questions:-

    i) Give the concept of ternary operator.ii) Why do we return a value when the prototype is not present there with main. (nor even a

    void).

  • 8/7/2019 C++Lab Manual_my

    5/19

    EXPERIMENT NO. -3

    Aim:- . Write a Program to find sum offirst natural numbers : sum= 1+2+3+4+. 100 by using a.

    for loop b. while loop c. do-while loop.

    Objective:-

    After this experiment you should be able to

    i) Understand the concepts of loops.ii) Differentiate between While and Do..while loop.iii) Assignment to the same number which is being manipulated.

    Brief Theory:-

    a) for loopFor(initialization; condition; increment/decrement){

    Statements}

    b) while loopWhile(condition){Statementsincrement/decrement}

    c) do-while loop.Do{

    Statements

    increment/decrement

    }

    While(condition)

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    i) Structure offor, while and do..while loops.ii) Link - http://www.dreamincode.net/forums/topic/13919-understanding-loops-in-

    c/

    iii) Link- http://icrontic.com/forum/showthread.php?t=6730Experiment:-

  • 8/7/2019 C++Lab Manual_my

    6/19

    Procedures:-

    i) Use for loop to sum up the numbers and display by showing that- this resultis generated by using for loop.

    ii) Use While loop to sum up the numbers and display by showing that- thisresult is generated by using for loop.

    iii) Use Do..While loop to sum up the numbers and display by showing that-this result is generated by using for loop.

    Comments:- (Write your new observation/ learning during this practical).

    Output:-

    Lab- Assignment:-

    i) Use comma operator in for loop to take multiple variables simultaneously.ii) Check the Outputs of While and Do..While loops.

    Viva Questions:-

    i) Explain (a) for loop (b)while loop (c)do-while loop.ii) Give the difference between for loop,while loop & do-while loop.iii) What are the advantages of(a) for loop (b)while loop (c)do-while

    loop.

  • 8/7/2019 C++Lab Manual_my

    7/19

    EXPERIMENT NO. -4

    Aim:- . Write a Program to find sum of the following series using function declaration.

    Sum= x-(x)3/3!+(x)5/5!-..(x)n/n!.

    Objective:-

    After this experiment you should be able to

    i) Understand the concepts offunction declaration function call & function definition.ii) Get the concept of recursive function.iii) Know the power function.

    Brief Theory:-

    a) Function Declaration is only necessary while you are calling it before function definition.b) Recursion is a programming technique that allows the programmer to express operations in

    terms of themselves. In C++, this takes the form of a function that calls itself.c) Power function- double pow(double b, double p);

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    i) Recursion in programming.ii) Power function.iii) Link for Recursive function- http://www.daniweb.com/forums/thread1734.html

    Experiment:-

    Procedures:-

    i) Use recursion to derive the factorial.ii) Use power function to get the power of X.

    Comments:- (Write your new observation/ learning during this practical).

    Output:-

  • 8/7/2019 C++Lab Manual_my

    8/19

    Lab- Assignment:-

    i) Write a programme to derive Fibonacci series using recursion.

    Viva Questions:-

    i) What is recursion in programming ?ii) Why do we declare a function ?iii) What does a main function return while it is defined with no

    prototype?

  • 8/7/2019 C++Lab Manual_my

    9/19

    EXPERIMENT NO. -5

    Aim:- Write a Program to read the element of the given two matrix & to perform the matrixmultiplication.

    Objective:-

    i) Creation of One, Two and multiple dimensional arrays.ii) Matrix input and output in the C++ program.iii) Addition of Matrixiv) Perform Matrix multiplication.

    Brief Theory:-

    a) An array is a series of elements of the same type placed in contiguous memory locations thatcan be individually referenced by adding an index to a unique identifier.

    b) Multidimensional arrays can be described as "arrays of arrays". For example, abidimensional array can be imagined as a bidimensional table made of elements, all of themof a same uniform data type.

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    i) Array representation in C++(one and Multidimensional).iii) Link for Array- http://www.cplusplus.com/doc/tutorial/arrays/

    Experiment:-

    Procedures:-

    i) Declare a Two/Three Dimensional Array.ii) Ask the user to declare its dimensions like in form of 3x3 matrix.iii) Enter the elements value of the Array.iv) Display the constructed Array.v) After getting two different arrays apply the logic of array multiplication.vi) Show the Result.

    Comments:- (Write your new observation/ learning during this practical).

    Output:-

  • 8/7/2019 C++Lab Manual_my

    10/19

    Lab- Assignment:-

    i) Create a programme to create a three dimensional array and display it.

    Viva Questions:-

    i) What is an Array ?ii) Do you find any difference in the declaration of arrays in C and C++?iii) How many initialization methods there can be in an array ?

  • 8/7/2019 C++Lab Manual_my

    11/19

    EXPERIMENT NO. -6

    Aim:- Write a Program to exchange the contents of two variable by using(a) call by value (b) Call by reference.

    Objective:-

    i) Know about a reference variable.ii) Call by value.iii) Call by reference.iv) Difference between the two.

    Brief Theory:-

    (a) Call by Reference

    swap(int *x,int *y)

    {

    int temp;

    temp=*x;

    *x=*y;

    *y=temp;

    }

    (b)Call by value

    swap(int &x,int &y)

    {

    int temp;

    temp=x;

    x=y;

    y=temp;

    }

    Pre- Lab:-

  • 8/7/2019 C++Lab Manual_my

    12/19

    Before going ahead with this experiment you need to know about:-

    j) Reference variables.ii) Pointers.

    Experiment:-

    Procedures:-

    i) Create a function to interchange the values by call by value.ii) Create another function to interchange the values by call by reference.

    Comments:- (Write your new observation/ learning during this practical).

    Output:-

    Lab- Assignment:-

    i) Swap two numbers without using the third (temp) variable.

    Viva Questions:-

    i)

    Explain pointers.

    ii) Give the difference between Call by value & Call by referenceiii) Explain Header files?iv) How Swapping is done?v) Is the Swapping possible without considering the third variable?

  • 8/7/2019 C++Lab Manual_my

    13/19

    EXPERIMENT NO. -7

    Aim:- Write a Program to perform the following arithmetic operations of a complex number using astructure (a). Addition of the two complex number

    (b). Subtraction of the two complex number(c). Multiplication of the two complex number(d). Division of the two complex number.

    Objective:-

    i) Explanation of structure.ii) Structure & Class.iii) Structure variable and class variable.iv) Arithmetic related to complex numbers.

    Brief Theory:-

    Structure declaration-

    struct structure-name

    {

    Members

    }variables;

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    i) Structure in C++ii) Difference between the class and structure.

    Experiment:-

    Procedures:-

    i) Create a structure with name complex.ii) Declare imaginary and real variables.iii) Declare variables to hold the calculated results.iv) Create multiple structure variables for inputting data and one to hold output

    result.

    Comments:- (Write your new observation/ learning during this practical).

  • 8/7/2019 C++Lab Manual_my

    14/19

    Output:-

    Lab- Assignment:-

    i) Perform the same operation using classes instead of structure.

    Viva Questions:-

    i) Why should we use class instead of structure ?ii) Explain structure ?iii) What is the use of structure ?iv) What are the advantages of structure ?

  • 8/7/2019 C++Lab Manual_my

    15/19

    EXPERIMENT NO. -8

    Aim:- Write a Program to generate a series of Fibonacci Nos. using the constructor where theconstructormember function had been defines

    (a). is the scope of class definition itself(b). out of the class definitions using the scope resolutions operator.(c). Also make this program with the help of the copyconstructor.

    Objective:-

    i) Scope resolution operator.ii) Function definition in a class.iii) Constructor and a normal function.iv) Copy constructor.

    Brief Theory:-

    a) The ::(scope resolution) operator is used to qualify hidden names so that you can still usethem. You can use the unary scope operator if a namespace scope or global scope name ishidden by an explicit declaration of the same name in a block or class. For example:

    int count = 0;

    int main(void) {

    int count = 0;

    ::count = 1; // set global count to 1

    count = 2; // set local count to 2

    return 0;

    }

    b) Acopyconstructor is a special constructor in the C++ programming language used to createa new object as a copy of an existing object. The first argument of such a constructor is areference to an object of the same type as is being constructed (const or non-const), whichmight be followed by parameters of any type (all having default values).

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    ii) Structure in C++ii) Difference between the class and structure.

    Experiment:-

  • 8/7/2019 C++Lab Manual_my

    16/19

    Procedures:-

    v) Create a structure with name complex.vi) Declare imaginary and real variables.vii) Declare variables to hold the calculated results.viii) Create multiple structure variables for inputting data and one to hold output

    result.

    Comments:- (Write your new observation/ learning during this practical).

    Output:-

    Lab- Assignment:-

    ii) Perform the same operation using classes instead of structure.

    Viva Questions:-

    i) What is constructor ?ii) Define default constructor.iii) What is parameterized constructor ?iv) What is the use of a constructor ?v) Define the scope of a class .vi) Explain the scope resolution operator.vii) Elaborate constructor initialization.viii) What is copy constructor ?ix) How each terms are related to its preceding terms in a Fibonacci

    series ?

  • 8/7/2019 C++Lab Manual_my

    17/19

    EXPERIMENT NO. -9

    Aim:- Write a Program to demonstrate how ambiguity is avoided using scope resolution operator inthe following inheritance (a). Single inheritance (b). Multiple inheritance

    Objective:-

    This experiment tells about-

    i) Scope resolution operator & its use.ii) Inheritanceiii) Single Inheritanceiv) Multiple Inheritancev) Use of protected access specifier.

    Brief Theory:-

    a) Scope ResolutionOperator: Refer part a in Brief theory of Experiment no. 9.b) Inheritance is a mechanism of reusing and extending existing classes without modifying

    them, thus producing hierarchical relationships between them.c) SingleInheritance: One base class is inherited by a derived class.d) MultipleInheritance: Multiple base classes are inherited by a single derived class.

    Pre- Lab:-

    Before going ahead with this experiment you need to know about:-

    i) Inheritance in C++ii) Difference between single, multiple, multilevel, hierarchal Inheritance.

    Experiment:-

    Procedures:-

    I. Create a programme for single inheritance.II. Like Declare a variable in Base class then use set and get methods to assign values to

    it.

    III. Again declare a variable in Derived class and assign it a value and retrieve it usingset and get methods respectively.

    IV. Now remove the ambiguity using scope resolution operator.

    Comments:- (Write your new observation/ learning during this practical).

    Output:-

  • 8/7/2019 C++Lab Manual_my

    18/19

    Lab- Assignment:-

    I. WAP to maintain records of employee and customers by using Person(contains age and name properties common for both the above classes) as a

    base class. Get values of employee according to its retirement age and

    monthly salary also get the value of customer by its annual purchase cost and

    living age. Also find the total company value according to the customer andemployee.

    Viva Questions:-

    I. What is inheritance?II. Define scope resolution operator ?

    III. Write down different kinds of inheritance possible in C++.IV. What is function overloading ?V. What is function overriding ?

    VI. How will you access the base class members if it gets override by thederived class ?

    VII. Differentiate between multiple inheritance and multilevelinheritance.

  • 8/7/2019 C++Lab Manual_my

    19/19

    EXPERIMENT NO. -10

    Aim:- Write a Program to perform the swapping of two data items of integer, floating point numberand character type with the help offunction overloading.