Comp Science Practical File 12th class

download Comp Science Practical File 12th class

of 55

Transcript of Comp Science Practical File 12th class

  • 8/13/2019 Comp Science Practical File 12th class

    1/55

    COMPUTER SCIENCE

    PRACTICAL

    FILE

    By:-Jayant Chandra

    Class :-> XII-A

    Roll no. :-> 10

  • 8/13/2019 Comp Science Practical File 12th class

    2/55

    1

    INDEX

    1.Binary searching in an array

    2. Bubble sort

    3. Selection sort

    4. Insertion sort

    5. Count number of alphabets and digits

    in a text file.

    6. Count number of words in a text file.

    7. Count number of lines in a text file.

    8. Count number of lines starting with

  • 8/13/2019 Comp Science Practical File 12th class

    3/55

    2

    t/ T in a text file.

    9. Merging two given arrays (ascending

    order) into a single array(ascending

    order).

    10. Deletion of a record from binary file.

    11. Modifying a record from a binary file.

    12. Program for stack as a linked-list.

    13. Program for stack as an array.

    14. Program for queue an a linked-list.

    15. SQL

  • 8/13/2019 Comp Science Practical File 12th class

    4/55

    3

    binary searching

    #include

    #include

    int bsearch(int [ ],int,int);

    void main( )

    {

    clrscr( );

    int ar[50];

    int n;

    int item;

    int index;

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    5/55

    4

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    6/55

    5

    {

    last=mid-1;

    }

    }

    return -1;

    }

    Output

  • 8/13/2019 Comp Science Practical File 12th class

    7/55

    6

    bubble sort

    #include

    #include

    void bubblesort(int [ ],int);

    void main( )

    {

    clrscr( );

    int ar[50],n;

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    8/55

    7

    void bubblesort(int ar[ ],int size)

    {

    int tmp;

    for(int i=0;i

  • 8/13/2019 Comp Science Practical File 12th class

    9/55

    8

    selection sort

    #include#include

    void selsort(int [ ],int);

    void main( )

    { clrscr( );

    int ar[50]n;

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    10/55

    9

    int small,tmp;

    for(int i=0;i

  • 8/13/2019 Comp Science Practical File 12th class

    11/55

    10

    insertion sort

    #include

    #include

    #include

    void insertion(int [],int);

    void main()

    { clrscr();

    int ar[50],item ,n ,index;

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    12/55

    11

    void insertion(int ar[],int size)

    {

    int tmp, j;

    ar[0]=INT_MIN;;

    for(int i=1;i

  • 8/13/2019 Comp Science Practical File 12th class

    13/55

    12

    Count number of alphabets and digits

    in a text file.#include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    char ch;

    int count=0;

    int count1=0;

    ifstream fil("honey.txt");

    while(!fil.eof())

    {

    fil.get(ch);

    if(isalpha(ch))

    {

    count++;

    }

    else if(isdigit(ch))

    {

    count1++;

  • 8/13/2019 Comp Science Practical File 12th class

    14/55

    13

    }

    }

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    15/55

    14

    Count number of words in a text file.

    #include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    char word;

    int count=0;

    ifstream fil("student.txt");

    while(!fil.eof())

    {

    fil.get(word);

    if(word==' ')

    {

    count++;

    }

    }

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    16/55

    15

    Text file contents

    It is a lovely weather today?.

    Output

  • 8/13/2019 Comp Science Practical File 12th class

    17/55

    16

    Count number of lines in a text file.

    #include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    char word[20];

    int count=0;

    ifstream fil("countline.txt");

    while(!fil.eof())

    {

    fil.getline(word,20);

    count++;

    }

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    18/55

    17

    Text file contents

    It is

    A lovely

    Weather today?.

    Output

  • 8/13/2019 Comp Science Practical File 12th class

    19/55

    18

    Count number of lines starting with

    T in a text file.

    #include

    #include

    #include

    #include

    #include

    void main()

    {

    clrscr();

    int count=0;

    char ch[20];

    ifstream fil("student.txt");

    while(!fil.eof())

    {

    fil.getline(ch,20);

    if((ch[0]=='t')||ch[0]=='T'))

    {

    count++;

    }

    }

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    20/55

    19

    }

    Text file contents

    my name id

    the best paret to the vert

    the to where i m

    mu the The

    the bets

    Output

  • 8/13/2019 Comp Science Practical File 12th class

    21/55

    20

    Merging two given arrays (ascending

    order) into a single array(ascending

    order).

    #include

    #include

    void merge(int Z[],int z,int X[],int x,int Y[],int y)

    {

    y=z+x;

    for(int d=0,e=0,f=0;d

  • 8/13/2019 Comp Science Practical File 12th class

    22/55

    21

    Y[f++]=Z[d++];

    }

    }

    else if(e

  • 8/13/2019 Comp Science Practical File 12th class

    23/55

    22

    for(int j=0;j>B[j];

    }

    c=a+b;

    merge(A,a,B,b,C,c);

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    24/55

    23

    Deletion of a record from binary file.

    #include

    #include

    #include

    #include

    #include

    class student

    {

    int id;

    char name[20];

    public:

    void input()

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    25/55

    24

    int getno()

    {

    return id;

    }

    };

    void main()

    {

    clrscr();

    student s;

    int rno;

    char ans[5];

    fstream fil("apple.dat",iso::binary|ios::in|ios::out|ios::app);

    do

    {

    s.input();

    fil.write((char*)&s,sizeof(s));

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    26/55

    25

    fout.seekg(0);

    while(!fout.eof())

    {

    fout.read((char*)&s,sizeof(s));

    if(s.getno()==rno)

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    27/55

    26

    while(!from.eof())

    {

    from.read((char*)&s,sizeof(s));

    s.display();

    }

    from.close();

    getch();

    }

  • 8/13/2019 Comp Science Practical File 12th class

    28/55

    27

    Modifying a record from a binary file.

    #include

    #include

    #include

    #include

    #include

    class student

    {

    int id;

    char name[20];

    public:

    void input()

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    29/55

    28

    void modify()

    {

    char nm[20];

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    30/55

  • 8/13/2019 Comp Science Practical File 12th class

    31/55

    30

    fout.read((char*)&s,sizeof(s));

    s.display();

    }

    fout.close();

    getch();

    }

    Output

  • 8/13/2019 Comp Science Practical File 12th class

    32/55

    31

    Program for stack as a linked-list.

    #include

    #include

    #include

    #include

    #include

    #include

    struct node

    {

    int info;

    node*next;

    }*top,*newptr,*save,*ptr;

    node*create_new_node(int);

    void push(node*);

    void display(node*);

    void pop();

    void main()

    { clrscr();

    int n_info;

    char ans[5],ans1[5];

    top=NULL;

    do {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    33/55

    32

    newptr=create_new_node(n_info);

    if(newptr==NULL)

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    34/55

    33

    {

    ptr=new node;

    ptr->info=n;

    ptr->next=NULL;

    return ptr;

    }

    void push(node*np)

    {

    if(top==NULL)

    {

    top=np;

    }

    else {

    save=top;

    top=np;

    np->next=save;

    }}

    void display(node*np)

    {

    while(np!=NULL)

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    35/55

    34

    }

    void pop()

    { if(top==NULL)

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    36/55

    35

    Program for stack as an array.

    #include

    #include

    #include

    #include

    #include

    int pop(int [],int&);

    int push(int [],int&,int);

    void display(int [],int);

    const int size=50;

    void main()

    { clrscr();

    int stack [size],top=-1,res,item;

    char ans[5], ans1[5];

    do {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    37/55

    36

    display(stack,top);

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    38/55

    37

    {

    if(top==size-1)

    {

    return -1;

    }

    else {

    top++;

    stack[top]=ele;

    }

    return 0;

    }

    int pop(int stack[],int&top)

    {

    int ret;

    if(top==-1)

    {

    return -1;

    }

    else {

    ret=stack[top];

    top--;

    }

    return ret;

    }

  • 8/13/2019 Comp Science Practical File 12th class

    39/55

    38

    void display(int stack[],int top)

    {

    if(top==-1)

    {

    return;

    }

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    40/55

    39

    Program for queue an a linked-list.

    #include

    #include

    #include

    #include

    #include

    #include

    struct node

    {

    int info;

    node*next;

    }*front,*newptr,*save,*ptr,*rear;

    node*create_new_node(int);

    void insert(node*);

    void display(node*);

    void delete_node();

    void main()

    { clrscr();

    int n_info;

    char ans[5], ans1[5];

    front=rear=NULL;

    do {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    41/55

    40

    newptr=create_new_node(n_info);

    if(newptr==NULL)

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    42/55

    41

    node*create_new_node(int n)

    {

    ptr=new node;

    ptr->info=n;

    ptr->next=NULL;

    return ptr;

    }

    void insert(node*np)

    {

    if(front==NULL)

    {

    front=rear=np;

    }

    else {

    rear->next=np;

    rear=np;

    } }

    void display(node*np)

    {

    while(np!=NULL)

    {

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    43/55

    42

    cout

  • 8/13/2019 Comp Science Practical File 12th class

    44/55

    43

    SQL

    Consider the tables given below and answerthe questions that follow:

    Table: Student

    No Name fees Zone Age Grade Comp

    1 Tushar 20000 West 18 A 10

    2 Aditya 35000 Centre 20 A 10

    3 Amit 32000 West 20 B 20

    4 Jayant 38000 North 17 C 20

    5 Anshul 32000 East 17 B 20

    6 Ratul 37000 South 17 B 10

    7 Devavrat 36000 North 20 A 20

  • 8/13/2019 Comp Science Practical File 12th class

    45/55

    44

    SQL commands to:-

    Create table student and competition

    Table: Competition

    Comp CName Minprize Maxprize Now

    10 Music 25000 32000 1

    20 Drama 20000 50000 5

    20 Debate 25000 40000 7

  • 8/13/2019 Comp Science Practical File 12th class

    46/55

    45

    1.Create table student(S.no integer, namechar(20),fees integer, zone char(7),ageinteger,grade char(2),comp integer);

    Create table competition (comp integer,cname

    char(10), minprize integer, maxprize integer,Now

    integer);

    Simple select

    2.Display the details of all the student

    Select * from student;

    3.Display the fees, Zone, and Grade of all the

    students

    select fees,zone,grade from student;

    Conditional Select using Where Clause

    4.Display the details of all the student who arebelow 20 years of age

  • 8/13/2019 Comp Science Practical File 12th class

    47/55

    46

    select * from student

    where age>20;

    5.Display the names of all the student workingin west zone

    select name from student

    where zone=west;

    6.Display the fees of all the student ofdepartment 10

    select fees from student

    where comp=10;

    Using NULL

    7.Display details of student whose grade is NULL

    select * from student

    where grade is NULL;

    8.Display details of student whose grade is not

    NULL

  • 8/13/2019 Comp Science Practical File 12th class

    48/55

    47

    select * from student

    where grade is NOT NULL;

    Using DISTINCT clause

    9. Display the names of various zones from the table

    student

    select distinct(zone) from student;

    10. Display the various competition numbers from

    the table student

    select distinct(comp) from student;

    Using Logical Operators (NOT, AND, OR)

    11.Display the details of all the students of

    department 10 who are above 20 years of age

    select * from student

  • 8/13/2019 Comp Science Practical File 12th class

    49/55

    48

    where comp=10 AND age>20;

    12. Display the details of all the student who are

    paying a fee of more than 35000 in the department

    20

    select * from student

    where fees=35000 AND comp=20;

    13.Display the names and fees of all the student

    who are working in West zone or in Centre zone

    select name,fees from student

    Where zone=west or zone=centre;

    Using IN operator

    14.Display the names of all the student who are

    working in department 20 or 30

    select name from student

    where comp IN(20,30);

  • 8/13/2019 Comp Science Practical File 12th class

    50/55

    49

    Using BETWEEN Operator

    15.Display the details of all the student whose fees

    is between 32000 and 38000

    select * from student

    Where fees BETWEEN 32000 AND 38000;

    Using LIKE Operator

    16.Display the name, fees, and age of all the

    student whose names start with M

    select name,fees,age from student

    where name like M%;

    17.Display the name, fees, and age of all the

    students whose names end with a

    select name,fees,age from student

    where name like %a;

    18.Display the details of all the students whose

    names contain a as the second character

    select name,fees,age from student

  • 8/13/2019 Comp Science Practical File 12th class

    51/55

    50

    where name _a%;

    Using Aggregate functions

    19.Display the sum and average of the salaries of all

    the students

    select sum(fees),avg(fees) from student;

    20.Display the highest and the lowest salaries being

    paid in department 10

    select max(fees),min(fees) from student

    where comp=10;

    21.Display the number of students working in

    department 10

    select count(*) from studentwhere comp=10;

    Using ORDER BY clause

    22.Display the details of all the students in the

    ascending order of their salaries

  • 8/13/2019 Comp Science Practical File 12th class

    52/55

    51

    select * from student

    order by fees ASC;

    23.Display the details of all the students in the

    descending order of their names

    select * from student

    order by name DESC;

    Using GROUP BY clause

    24.Display the total number of students in each

    department

    select comp,count(*) from student

    group by comp;

    25.Display the highest fees, lowest fees, and

    average fees of each zone

    select zone,max(fees),min(fees),avg(fees)

    from student

    group by zone;

    Using UPDATE, DELETE, ALTER TABLE

  • 8/13/2019 Comp Science Practical File 12th class

    53/55

    52

    26.Put the grade B for all those whose grade is

    NULL

    update student

    set grade=B

    where grade is NULL;

    27.Increase the fees of all the students above 20

    years of age by 10%.

    update student

    set fees=fees+(0.1*fees)

    where age>20;

    28.Delete the records of all the students whose

    grade is C and fees is below 20000

    delete from student

    where grade=C AND fees

  • 8/13/2019 Comp Science Practical File 12th class

    54/55

    53

    29.Add another column HireDate of type integer in

    the Student table

    alter table student

    add (HireDate integer);

    JOIN of two tables

    20.Display the details of all the students who work

    in Drama competition

    select * from student, competition

    where student.comp=competition.comp AND

    cname=Drama;

    31.Display the Name and Competition Name of all

    the students

  • 8/13/2019 Comp Science Practical File 12th class

    55/55

    select name, cname from student, competition

    where student.comp=competition.comp;

    DROP TABLE

    32.Drop the tables Student

    drop table student;

    33. Drop the table competition

    drop table competition;