ds practicals

Upload: shama-khan

Post on 05-Apr-2018

239 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/2/2019 Ds Practicals

    1/31

    [1]

    //Linear Search

    #include

    #include

    void main()

    {

    int i, item, loc=0;

    static int a[10];

    clrscr();

    for (i = 0; i a[i];

    }

    cout > item;

    for (i = 0; i

  • 8/2/2019 Ds Practicals

    2/31

    [2]

    //Binary Search

    #include

    #include

    const int N = 10;

    void main()

    {

    int beg, end, mid, item,i;

    int loc = 0;

    static int a[N]={10,14,19,23,30,40,50,60,70,80};

    clrscr();

    cout

  • 8/2/2019 Ds Practicals

    3/31

    [3]

    //Insertion in array

    #include

    #include

    class array

    {

    private:

    int *a;

    public:

    array(int);

    void getArray(int);

    void showArray(int);

    void insert(int);

    };

    array :: array(int n)

    {

    a = new int[n];

    }

    void array :: getArray(int n)

    {

    cout

  • 8/2/2019 Ds Practicals

    4/31

    void main()

    {

    int n;

    clrscr();

    coutn;

    array aa(n);

    aa.getArray(n);

    aa.showArray(n);

    aa.insert(n);

    cout

  • 8/2/2019 Ds Practicals

    5/31

    [4]

    //Deletion from array

    #include

    #include

    const int size = 50;

    class array

    {

    private:

    int *a;

    public:

    array(int);

    void getArray(int);

    void showArray(int);

    void del(int);

    };

    array :: array(int n)

    {

    a = new int[n];

    }

    void array :: getArray(int n)

    {

    cout

  • 8/2/2019 Ds Practicals

    6/31

    void main()

    {

    int n;

    clrscr();

    coutn;

    array aa(n);

    aa.getArray(n);

    aa.showArray(n);

    aa.del(n);

    cout

  • 8/2/2019 Ds Practicals

    7/31

    [5]

    //Largest and Smallest in Array

    #include

    #include

    void main()

    {

    int i,a[50],n,max,min;

    clrscr();

    coutn;

    cout

  • 8/2/2019 Ds Practicals

    8/31

    [6]

    //Merging of Array

    #include

    #include

    const int M = 3;

    const int N = 5;

    void main()

    {

    void merging(int *, int, int *, int, int *);

    int A[M] = {22, 66,88};

    int B[N] = {11, 33, 44, 77, 99};

    int C[M+N], i;

    clrscr();

    cout

  • 8/2/2019 Ds Practicals

    9/31

    }

    if ( na>= r)

    {

    for (int k = 0; k= s)

    {

    for (int k = 0; k

  • 8/2/2019 Ds Practicals

    10/31

    [7]

    //Bubble Sort

    #include

    #include

    const N = 5;

    void main(){

    int i, j, temp;

    static int a[N];

    clrscr();

    cout

  • 8/2/2019 Ds Practicals

    11/31

    OUTPUT

    Enter The 5 Element :

    1 3 5 2 4

    Original Array Is :1 3 5 2 4

    Sorted Array is :

    1 2 3 4 5

  • 8/2/2019 Ds Practicals

    12/31

    [8]

    //Insertion Sort

    #include

    #include

    const int m = 20;

    class sort

    {

    int a[m], k, n, minval, loc;

    public:

    void getdata();

    void putdata();

    void inssort();

    };

    void sort :: getdata()

    {

    coutn;

    cout a[k];

    }

    void sort :: putdata()

    {

    cout

  • 8/2/2019 Ds Practicals

    13/31

    void main()

    {

    sort ins;

    clrscr();

    ins.getdata();

    ins.inssort();

    ins.putdata();

    getch();

    }

    OUTPUT

    Enter Number Of Elements In Array : 5

    Enter Element In Array :

    1 3 5 2 4

    After Sorting :

    Elements In Array Are :

    1 2 3 4 5

  • 8/2/2019 Ds Practicals

    14/31

    [9]

    //Selection Sort

    #include

    #include

    const m = 20;

    class sort

    {

    int a[m], k, n, minval, loc;

    public:

    void getdata();

    void putdata();

    void selsort();

    int min(int k);

    };

    void sort :: getdata()

    {

    coutn;

    cout

  • 8/2/2019 Ds Practicals

    15/31

    void sort :: selsort()

    {

    for (k=0; k

  • 8/2/2019 Ds Practicals

    16/31

    [10]

    //Transpose Of Matrix

    #include

    #include

    #include

    main()

    {

    int a[3][3], i, j;

    clrscr();

    cout

  • 8/2/2019 Ds Practicals

    17/31

    OUTPUT

    Enter The Element Of Matrix :

    1 2 3

    4 5 6

    7 8 9

    Transpose Of Matrix is :

    1 4 7

    2 5 8

    3 6 9

  • 8/2/2019 Ds Practicals

    18/31

    [11]

    //Largest and Smallest from Matrix

    #include

    #include

    #include

    void main()

    {

    int i,a[3][3],j,max,min;

    clrscr();

    cout

  • 8/2/2019 Ds Practicals

    19/31

    [12]

    //Sum of each row and column

    #include

    #include

    const int M = 3;

    const int N = 3;

    void main()

    {

    static int i, j, rsum[M], csum[N];

    static int a[M][N];

    clrscr();

    for (i = 0; i

  • 8/2/2019 Ds Practicals

    20/31

    OUTPUT

    Enter The Element : 9

    Enter The Element : 8

    Enter The Element : 7

    Enter The Element : 6

    Enter The Element : 5

    Enter The Element : 4

    Enter The Element : 3

    Enter The Element : 2

    Enter The Element : 1

    Matrix is :

    9 8 7

    6 5 4

    3 2 1

    Sum Of Column Of Matrix Is :

    Sum Of Column 0 Is 18

    Sum Of Column 1 Is 15

    Sum Of Column 2 Is 12

    Sum Of Row Of Matrix Is :

    Sum Of Row Elements 0 Is 24

    Sum Of Row Elements 1 Is 15

    Sum Of Row Elements 2 Is 6

  • 8/2/2019 Ds Practicals

    21/31

    [13]

    //LCM & GCD

    #include

    #include

    class number

    {

    private:

    int a, b, lcm, gcd;

    public:

    void getdata(void);

    void showdata(void);

    int fun_gcd(int x, int y);

    };

    void number :: getdata(void)

    {

    cout a;

    coutb;

    }

    void number :: showdata(void)

    {

    gcd = fun_gcd(a,b);

    cout

  • 8/2/2019 Ds Practicals

    22/31

    OUTPUT

    Enter The 1st Number : 5

    Enter The 2nd Number : 15

    GCD = 5

    LCM = 15

  • 8/2/2019 Ds Practicals

    23/31

    [14]

    //PUSH and POP operation for stack

    #include

    #include

    void main()

    {int i,n,a[50];

    clrscr();

    coutn;

    for(i=1;i

  • 8/2/2019 Ds Practicals

    24/31

    OUTPUT

    Enter the range of stack : 5

    Enter element 1 : 4557

    Enter element 2 : 55

    Enter element 3 : 24

    Enter element 4 : 789

    Enter element 5 : 100

    Elements in stack : 4557 55 24 789 100

    Deleted element : 100

    Deleted element : 789

    Deleted element : 24

    Deleted element : 55

    Deleted element : 4557

    All elements deleted. Now stack is empty.

  • 8/2/2019 Ds Practicals

    25/31

    [15]

    //Insert and Delete element in Queue

    #include

    #include

    int MAX= 20;

    #include

    int queue[20];

    int front = -1;

    int rear = -1 ;

    void push(int data)

    {

    if (rear == MAX-1)

    {

    cout

  • 8/2/2019 Ds Practicals

    26/31

    }

    return(data);

    }

    void main(void)

    {

    int choice;

    int x;

    int data;

    while(1)

    {

    clrscr();

    cout

  • 8/2/2019 Ds Practicals

    27/31

    OUTPUT

    1. TO Insert Data

    2. To Delete Data

    3. To Exit

    Enter your Choice

    1

    Enter Data

    20

    1. TO Insert Data

    2. To Delete Data

    3. To Exit

    Enter your Choice

    1

    Enter Data

    30

    1. TO Insert Data

    2. To Delete Data

    3. To Exit

    Enter your Choice

    2

    Data Poped Is :

    Data Removed is 20

  • 8/2/2019 Ds Practicals

    28/31

    1. TO Insert Data

    2. To Delete Data

    3. To Exit

    Enter your Choice

    2

    Data Poped Is :

    Data Removed is 30

    1. TO Insert Data

    2. To Delete Data

    3. To Exit

    Enter your Choice

    2

    Queue Is Empty, Data Cannot Be Removed.

  • 8/2/2019 Ds Practicals

    29/31

    [16]

    //Factorial using Recursion

    #include

    #include

    void main()

    {

    int g, n, fact(int);

    clrscr();

    cout

  • 8/2/2019 Ds Practicals

    30/31

    [17]

    //Fibonacci using Recursion

    #include

    #include

    class fibonacci

    {

    private:

    int n;

    public:

    void getfibo()

    {

    cout

  • 8/2/2019 Ds Practicals

    31/31

    [18]

    //Product of 2 numbers using Recursion

    #include

    #include

    void main()

    {

    int n1, n2 , mul(int,int);

    clrscr();

    coutn1;

    coutn2;

    int n = mul(n1,n2);

    cout