Comp sc Prac File XII

Embed Size (px)

Citation preview

  • 8/13/2019 Comp sc Prac File XII

    1/103

    1 | P a g e

    Computer science

    Practical file

    Prangav Singhal

    XII-d

    Roll no.-03

  • 8/13/2019 Comp sc Prac File XII

    2/103

    2 | P a g e

    ACKNOWLEDGEMENT

    I gratefully acknowledge the valuable contributions of all those who

    helped me to complete this practical file successfully.

    First of all, I would like to thank my Computer Science teacher,

    Mrs. Pooja Gupta, who gave this important topic, her invaluable

    guidance and made this practical file possible.

    Special thanks are due to my parents who helped me to coordinate

    the entire activity and provided necessary assistance.

  • 8/13/2019 Comp sc Prac File XII

    3/103

  • 8/13/2019 Comp sc Prac File XII

    4/103

    4 | P a g e

    INDEX

    1. Structures

    2. Classes And Objects

    3. Constructors And Destructors

    4. Inheritance: Extending Classes

    5. File Handling

    6. Pointers

    7. Arrays

    8. Linked Lists, Stacks And Queues

    9. Structured Query Language

  • 8/13/2019 Comp sc Prac File XII

    5/103

    5 | P a g e

    STRUCTURES

  • 8/13/2019 Comp sc Prac File XII

    6/103

    6 | P a g e

    1. Write a program using a structure Country to store information of three countries

    comprising its Capital Name and Per Capita Income and display the information according

    to Country Name entered by the user.

    #include

    #include

    #include

    #include

    struct Country

    { char countryname[20];

    char capname[20];

    long unsigned int percapitainc;

    };

    void main()

    { clrscr();

    Country c[3];

    for (int i=0 ; i

  • 8/13/2019 Comp sc Prac File XII

    7/103

  • 8/13/2019 Comp sc Prac File XII

    8/103

    8 | P a g e

    2. Write a program in C++ to make a structure Time and to add two variables of structure Time

    and to display them on screen and the total time.

    #include

    #include

    struct time

    { int hours;

    int minutes;

    int seconds;

    };

    void main()

    { clrscr();

    time t1,t2,t3;

    cout

  • 8/13/2019 Comp sc Prac File XII

    9/103

    9 | P a g e

    OUTPUT

  • 8/13/2019 Comp sc Prac File XII

    10/103

    10 | P a g e

    3. Declare the structure having the following data members- code, bill, amount and no.

    of units consumed. Compute the electricity bill such that :

    Unit Consumption Bill Amount

    100 units Rs. 0.8

    Next 200 units Rs. 1.2

    Next 300 units Rs. 2.0

    Beyond 600 units Rs. 3.0

    #include

    #include

    struct elect

    { int code;

    char name[45];

    int unit;

    float bill;

    };

    void main()

    { elect e1;

    coute1.code;

    coute1.unit;

    cout

  • 8/13/2019 Comp sc Prac File XII

    11/103

    11 | P a g e

    OUTPUT:

    ....

  • 8/13/2019 Comp sc Prac File XII

    12/103

    12 | P a g e

    CLASSES

    AND

    OBJECTS

  • 8/13/2019 Comp sc Prac File XII

    13/103

    13 | P a g e

    4. Define a class travelplan in c++ with the following descriptions :

    Private members:

    Plancode long Place string No of trains int No of buses int

    Public:

    a constructor to initialize the value of plan code as 1001, place asagra,no oftrains=5, no of buses=1.

    newplan() - allow user to enter plancode, place, no. of trains, no of buses andassign the value to no. of buses as given:

    no of trains no of buses

    =20 & =40 3

    showplan()to display data members.

    #include

    #include

    class travelplan

    { long plancode;

    char place[78];

    int not;

    int nob;

    public:

    travelplan()

    { plancode=1001;

    strcpy(place,"agra");

    not=5;

    nob=1;

    }

    void newplan()

    { cout

  • 8/13/2019 Comp sc Prac File XII

    14/103

    14 | P a g e

    cin>>plancode>>place>>not>>nob;

    if(not=20&&not=40)

    nob=3;

    }

    void showplan()

    { cout

  • 8/13/2019 Comp sc Prac File XII

    15/103

    15 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    16/103

  • 8/13/2019 Comp sc Prac File XII

    17/103

    17 | P a g e

    cal_penalty();

    }

    void dispdata()

    { cout

  • 8/13/2019 Comp sc Prac File XII

    18/103

    18 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    19/103

    19 | P a g e

    6. Create a class Account that stores Account Number, Customer Name and Balance.

    Include necessary member functions in order to achieve the following tasks:

    to deposit the amount to withdraw the amount such that the minimum balance in the account is Rs. 500 to display the account details

    #include

    #include

    #include

    #include

    class account

    { long unsigned int acc_no;

    char cust_name[20];

    double balance;

    public:

    void get_info()

    { coutacc_no;

    cout

  • 8/13/2019 Comp sc Prac File XII

    20/103

    20 | P a g e

    void withdraw()

    { double amnt;

    cout

  • 8/13/2019 Comp sc Prac File XII

    21/103

  • 8/13/2019 Comp sc Prac File XII

    22/103

    22 | P a g e

    7. Define a class Candidate with the following members:

    Private members :

    Roll no long Score float Name string Remarks string AssignRem() - to assign remarks as per the score obtained by the candidate

    Score Remark

    >=50 selected

    =50)

    strcpy(rem,"selected");

    else

    strcpy(rem,"not selected");}

    public :

    void enter()

    { coutrno;

    cout

  • 8/13/2019 Comp sc Prac File XII

    23/103

    23 | P a g e

    AssignRem();

    }

    void display()

    { cout

  • 8/13/2019 Comp sc Prac File XII

    24/103

  • 8/13/2019 Comp sc Prac File XII

    25/103

    25 | P a g e

    8. Define a class Nutrition having the following members:

    Private members:

    Access no. int Calories int Name of food char[20] Foodtype char[30] Cost float Assignaccess() - to return a random access no. between 10 and 1000.

    Public members:

    Intake() - to input the food name, food type, calories and the cost. Output() - to display all the details if the foodtype is fruit.

    #include

    #include

    #include

    #include

    class Nutrition

    { int accessno, cal;

    char name[20] , foodtype[30];

    float cost;

    int assignaccess()

    { return(10+random(991));}

    public:

    void Intake()

    { coutcal;coutcost;

    cout

  • 8/13/2019 Comp sc Prac File XII

    26/103

    26 | P a g e

    cout

  • 8/13/2019 Comp sc Prac File XII

    27/103

    27 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    28/103

    28 | P a g e

    CONSTRUCTORS AND

    DESTRUCTORS

  • 8/13/2019 Comp sc Prac File XII

    29/103

    29 | P a g e

    9. Illustrate the working of constructors and destructors through a suitable c++ code.

    #include

    #include

    class student{ int admno;

    char name[40];

    public :

    student()

    { cout

  • 8/13/2019 Comp sc Prac File XII

    30/103

    30 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    31/103

  • 8/13/2019 Comp sc Prac File XII

    32/103

    32 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    33/103

    33 | P a g e

    INHERITANCE:

    EXTENDING CLASSES

  • 8/13/2019 Comp sc Prac File XII

    34/103

    34 | P a g e

    11. Write a program in C++ to show the order of constructor and destructor invocation in a class.

    #include

    class A

    { public:

    A()

    { cout

  • 8/13/2019 Comp sc Prac File XII

    35/103

    35 | P a g e

    { cout

  • 8/13/2019 Comp sc Prac File XII

    36/103

    36 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    37/103

  • 8/13/2019 Comp sc Prac File XII

    38/103

    38 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    39/103

    39 | P a g e

    FILE HANDLING

  • 8/13/2019 Comp sc Prac File XII

    40/103

  • 8/13/2019 Comp sc Prac File XII

    41/103

    41 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    42/103

    42 | P a g e

    14. Write a program in c++ to display the name and admission number of students from a

    file.

    #include

    #include

    struct student

    { int admno;

    char name[20];

    };

    void main()

    { student s1;

    ifstream fin("abc.dat",ios::binary);

    fin.read((char *)&s1,sizeof(s1));

    while(!fin.eof())

    { cout

  • 8/13/2019 Comp sc Prac File XII

    43/103

    43 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    44/103

    44 | P a g e

    15. Write a program to read a file character by character and count the number of

    alphabets in the file.

    #include

    #include

    #include

    void main()

    { ofstream fout("loren.txt");

    fout

  • 8/13/2019 Comp sc Prac File XII

    45/103

    45 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    46/103

    46 | P a g e

    16. Write a program to read a file character by character and count the number of blanks

    in the file.

    #include

    #include

    #include

    void main()

    { char ch;

    int ct=0;

    ofstream fout("abc.txt");

    fout

  • 8/13/2019 Comp sc Prac File XII

    47/103

    47 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    48/103

  • 8/13/2019 Comp sc Prac File XII

    49/103

    49 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    50/103

    50 | P a g e

    18. Write a program to read a file word by word and count the number of words starting

    with a vowel.

    #include

    #include

    #include

    void main()

    { char a[20];

    int ct=0;

    ofstream fout(abc.txt);

    fout

  • 8/13/2019 Comp sc Prac File XII

    51/103

    51 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    52/103

    52 | P a g e

    19. Write a program in c++ to read a file and count the number of lines in the file.

    #include

    #include

    #include

    #include

    void main()

    { ofstream fout("loren.txt");

    fout

  • 8/13/2019 Comp sc Prac File XII

    53/103

    53 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    54/103

    54 | P a g e

    20. Write a program to read a file line by line and count the number of lines starting with

    the letter I.

    #include

    #include

    #include

    void main()

    { char a[20];

    int ct=0;

    ofstream fout(abc.txt);

    fout

  • 8/13/2019 Comp sc Prac File XII

    55/103

  • 8/13/2019 Comp sc Prac File XII

    56/103

    56 | P a g e

    21. Write a program in c++ to write, display, search, append and modify in a file.

    #include

    #include

    #include

    #include

    struct student

    { int admno;

    char name[20];

    };

    void create();

    void disp();

    void app();

    void modify();

    void main()

    { int ch;

    while(1)

    { cout

  • 8/13/2019 Comp sc Prac File XII

    57/103

  • 8/13/2019 Comp sc Prac File XII

    58/103

    58 | P a g e

    ofstream fout("stud.dat",ios::binary|ios::app) ;

    student s1;

    do

    { cout

  • 8/13/2019 Comp sc Prac File XII

    59/103

    59 | P a g e

    fin.read((char*)& s1,sizeof(s1));

    while(!fin.eof())

    { cout

  • 8/13/2019 Comp sc Prac File XII

    60/103

    60 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    61/103

  • 8/13/2019 Comp sc Prac File XII

    62/103

    62 | P a g e

    POINTERS

  • 8/13/2019 Comp sc Prac File XII

    63/103

    63 | P a g e

    22. WAP to reverse a string (character array) using pointers.

    #include

    #include

    #include

    void print(char *p)

    {

    int len=strlen(p),i=0;

    char *d=&p[len-1];

    while(i

  • 8/13/2019 Comp sc Prac File XII

    64/103

  • 8/13/2019 Comp sc Prac File XII

    65/103

    65 | P a g e

    23. WAP to find whether a string is a palindrome or not, using pointers.

    #include

    #include

    #include

    int palindrome(char *ps){

    int len=strlen(ps),i=0;

    char rev[80],t;

    strcpy(rev,ps);

    char *pr=rev,*d=&pr[len-1];

    while(i

  • 8/13/2019 Comp sc Prac File XII

    66/103

    66 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    67/103

    67 | P a g e

    24. WAP to accept a string and a character and print the number of times the character

    has occurred in the string.

    #include

    #include

    #include

    void main()

    {

    int ct=0,len,i=0;

    char s[80],c;

    cout

  • 8/13/2019 Comp sc Prac File XII

    68/103

    68 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    69/103

    69 | P a g e

    ARRAYS

  • 8/13/2019 Comp sc Prac File XII

    70/103

    70 | P a g e

    25. Display the values divisible by 2 in the 2d array entered by the user.

    #include

    void dispnten(int b[3][4],int r,int c)

    { for(int i=0;i

  • 8/13/2019 Comp sc Prac File XII

    71/103

    71 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    72/103

    72 | P a g e

    26. Write a function which accepts an integer array and its size as arguments and assigns

    the elements into a 2d array of integers in the following format:

    If the array is : 1 2 3 4 5 6

    The resultant 2d array is: 1 2 3 4 5 6

    1 2 3 4 5 0

    1 2 3 4 0 0

    1 2 3 0 0 0

    1 2 0 0 0 0

    1 0 0 0 0 0

    #include

    void fun(int a[32],int s)

    { int i,j,b[34][23];

    for(i=0;i

  • 8/13/2019 Comp sc Prac File XII

    73/103

    73 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    74/103

    74 | P a g e

    27. Write a function to swap the rows in a 2d array entered by the user.

    #include

    void fun(int a[67][56],int r,int c)

    { int i,temp;

    for(int j=0;j

  • 8/13/2019 Comp sc Prac File XII

    75/103

    75 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    76/103

    76 | P a g e

    28. Write a program in c++ to create, search, sort, insert, delete and display a one

    dimensional integer array.

    #include

    #include

    const int size=50;

    int a[size],n;

    void create()

    { coutn;

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

    }

    void search() //binary search

    { coutnum;

    while(beg

  • 8/13/2019 Comp sc Prac File XII

    77/103

    77 | P a g e

    void sort() //insertion sort

    { int i,j,temp;

    for(i=1;ipos;--i)

    a[i]=a[i-1];

    coutnum;

    a[pos]=num;

    ++n;

    }

    void display()

    { cout

  • 8/13/2019 Comp sc Prac File XII

    78/103

    78 | P a g e

    { if(a[i]==num)

    { pos=i;

    f=1;

    break;

    }

    }

    if(f==1)

    { for(i=pos;i

  • 8/13/2019 Comp sc Prac File XII

    79/103

    79 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    80/103

    80 | P a g e

    29. Assume an array e containing elements of structure employee is required to be

    arranged in descending order of employee number. Write a function in c++ to arrange the

    same using insertion sort.

    #include

    #include

    #include

    struct employee

    { int eno;

    char name[23];

    float salary; };

    void desc(employee a[],int n)

    { employee temp;

    int j;

    for(int i=1;ia[j].eno&&j>=0)

    { a[j+1]=a[j];

    j--;

    }

    a[j+1]=temp;

    }

    for(i=0;i

  • 8/13/2019 Comp sc Prac File XII

    81/103

  • 8/13/2019 Comp sc Prac File XII

    82/103

    82 | P a g e

    Q30. Write a program in C++ to sort an array using Bubble Sort Method. The array and its size to

    be given by the user.

    #include

    void BubbleSort(int a[20], int n)

    { for(int i=0; i

  • 8/13/2019 Comp sc Prac File XII

    83/103

  • 8/13/2019 Comp sc Prac File XII

    84/103

    84 | P a g e

    31. Write a program in C++ to sort an array using Exchange Selection Method. The array and its

    size to be given by the user.

    #include

    void ESelection(int a[20],int n)

    { int i,small,pos,j,temp;

    for(i=0;i

  • 8/13/2019 Comp sc Prac File XII

    85/103

  • 8/13/2019 Comp sc Prac File XII

    86/103

    86 | P a g e

    LINKED LISTS, STACKS

    AND QUEUES

  • 8/13/2019 Comp sc Prac File XII

    87/103

    87 | P a g e

    32. Write a program in c++ to insert, delete and display in a static queue.

    #include

    #include

    #include

    int a[5];

    int top=-1;

    void insert()

    { if(top==4)

    coutn;

    top++;

    a[top]=n;

    }

    }

    void del()

    { if(top==-1)

    cout

  • 8/13/2019 Comp sc Prac File XII

    88/103

    88 | P a g e

    }

    void main()

    { int ch;

    while(1)

    { cout

  • 8/13/2019 Comp sc Prac File XII

    89/103

    89 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    90/103

    90 | P a g e

    33 . Define function stackpush() to delete and display , for a static circular queue of array q[10].

    #include

    const int s=10;

    void stackpush(int q[10],int &f,int &r)

    {

    if(((f==0)&&r==(s-1))||(f==r+1))

    { cout

  • 8/13/2019 Comp sc Prac File XII

    91/103

    91 | P a g e

    else if(f==s-1)

    f=0;

    else

    f=f+1;

    }

    }

    void traversal(int q[10],int &f,int &r)

    {

    if(f==-1)

    cout

  • 8/13/2019 Comp sc Prac File XII

    92/103

    92 | P a g e

    switch(choice)

    { case 1: stackpush(queue,front,rear);break;

    case 2:stackpop(queue,front,rear);break;

    case 3:traversal(queue,front,rear);break;

    }

    coutch;

    }while(ch=='y');

    }

  • 8/13/2019 Comp sc Prac File XII

    93/103

    93 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    94/103

    94 | P a g e

    34. Write a program in c++ to push, pop, delete in a dynamic stack.

    #include

    #include

    #include

    struct stud

    { int admno;

    char name[20];

    stud * link;

    } * top=NULL;

    void insert()

    { stud*temp=new stud;

    cin>>temp->admno;

    gets(temp->name);

    temp->link=NULL;

    if(top==NULL)

    top=temp;

    else

    { temp->link=top;

    top=temp;

    }

    }

    void del()

    { if(top==NULL)

    coutlink=NULL;

    delete temp;

    }

    }

    void display()

  • 8/13/2019 Comp sc Prac File XII

    95/103

    95 | P a g e

    { if(top==NULL)

    cout

  • 8/13/2019 Comp sc Prac File XII

    96/103

    96 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    97/103

    97 | P a g e

    35. Write a program in c++ to insert, delete and display in a dynamic queue.

    #include

    #include

    #include

    struct stud

    { int admno;

    char name[20];

    stud * link;

    } * f=NULL, * r=NULL;

    void insert()

    { stud*temp=new stud;

    cin>>temp->admno;

    gets(temp->name);

    temp->link=NULL;

    if(f==NULL)

    f=r=temp;

    else

    { r->link=temp;

    r=temp;

    }

    }

    void del()

    { if(f==NULL)

    coutlink=NULL;

  • 8/13/2019 Comp sc Prac File XII

    98/103

    98 | P a g e

    delete temp;

    }

    }

    void display()

    { if(f==NULL)

    cout

  • 8/13/2019 Comp sc Prac File XII

    99/103

    99 | P a g e

    OUTPUT:

  • 8/13/2019 Comp sc Prac File XII

    100/103

  • 8/13/2019 Comp sc Prac File XII

    101/103

    101 | P a g e

    Employees

    Empid First name Last name address City

    010 George Smith 83 first

    street

    Howard

    105 Mary Jones 842 vine

    ave

    Losantiville

    152 Sam Tones 33 elm st Paris

    215 Sarah Ackerman 440 us 110 Upton

    335 Henry Williams 12 moore

    street

    boston

    441 Peter Thompson 11red read Paris

    300 Robert Samuel 9 fifth cross Washington

    Empsalary

    empid salary benefits designation

    010 75000 15000 Manager

    105 65000 15000 Manager

    152 80000 25000 Director

    215 75000 12500 Manager

    441 28000 7500 Salesman

    300 45000 10000 Clerk

    335 40000 10000 clerk

  • 8/13/2019 Comp sc Prac File XII

    102/103

    102 | P a g e

    WRITE SQL COMMANDS

    Q1. To display first name, last name, address and city of all employees living

    in paris from table employees.

    Select first name, last name , address, city from employees where city=paris;

    First name Last name address city

    Sam tones 33 elm st paris

    peter Thompson 11 red road paris

    Q2. To display empid and first name in descending order of empid from table

    employees.

    Select empid, first name from employees order by empid desc;

    Empid First name

    441 Peter

    335 Henry

    300 Robert

    215 Sarah

    152 Sam

    105 Mary

    010 george

    Q3. To display distinct designations from table empsalary.

    Select distinct designation from empsalary;

    Designation

    Manager

    Director

    Salesman

    clerk

    Q4. To display details of employees whose last name is like_ones.

  • 8/13/2019 Comp sc Prac File XII

    103/103

    Select * from employees where last name like _ones;

    empid First name Last name address City

    105 Mary Jones 842 vine ave Losantiville

    152 sam Tones 33 elm st Paris

    Q5. To display empid, first name and salary whose salary lies between 25000

    and 50000.

    Select empid, first name,salary from employees, empsalary where

    employees.empid=empsalary.empid and salary between 25000 and 50000;empid First name Salary

    441 Peter 28000

    300 Robert 45000

    335 Henry 40000

    Q6.To display maximum salary among manager and clerk from the table

    empsalary.

    Select max(salary) from empsalary group by designation having designation in

    (manager, clerk);

    max(salary)