You are on page 1of 32

PRACTICAL FILE

COMPUTER SCIENCE
2016-2017

NAME:-HIMANSHU
CLASS:-12th
Roll No:Subject Teacher:-Mr. Pankaj
Shukla

Contents
1 Introduction
2 Acknowledgement
3

ARRAY(1-DIMENSIONAL)
i SEARCHING
ii SORTING
iii

MERGING

iv INSERTION
v DELETION OF ELEMENTS
4

ARRAY (1-DIMENSIONAL)
i

CREATION

ii

INSERTION

iii

TRAVERSAL

CLASSES & OBJECTS

STACKS USING ARRAY & LINK LIST IMPLEMENTATION

QUEUE USING ARRAY & LINK LIST IMPLEMENTATION

FILE (BINARY & TEXT) OPERATION (CREATION, UPDATION, QUERY)

15 SQL COMMANDSALONG WITH THE OUTPUT BASED ON ANY


TABLE/RELATION.

INTRODUCTION
This file is combo of different C++ Programs. This will enhance the readers
knowledge and will provide a basic view about C++ language.
It also contain SQL programs which is used to retain a database of any type of
information like the information of any school management system, Library system
etc.

Acknowledgment
It is my utmost pleasure to express my sincere thanks to my computer teacher,
Mr Pankaj Shukla, for providing a helping hand in this project. His valuable
guidance, support and supervision helped me to reach up to this level.
I thank our principal for his constant guidance and faith. I express my gratitude to the
school management for providing me this opportunity to work this project out. I would
also like to thank my parents as they encouraged me to put forward my project.

Header Files and Their Purposes


1.
2.
3.
4.
5.
6.
7.

IOSTREAM.H
FSTREAM.H
PROCESS.H
CTYPE.H
CONIO.H
STDIO.H
STIRING.H

For input & output console


For file handling, cin, cout
For exit () function
For character handling
For clrscr () and getche () function
for standard I/O operations
For string dandling

C++ PROGRAMS
Array: Array is a collection of variables of the same type that are
referenced by a common name.
I

One dimensional Array program for various functionalities:

C++ code for main functionvoid main()


{clrscr();
int ary[10]={2,4,5,7,8,9,10,12,13,18};

//declaration of array

ary
int arry[10]={5,6,3,17,4,2,8,9,10,11};
int n=10;
int a
cout<<"\n\tCHOOSE THE OPERATION";

//main menu

cout<<"\n\t1.LINEAR SEARCH";
cout<<"\n\t2.BINARY SERACH";
cout<<"\n\t3.INSERT NEW NUMBER";
cout<<"\n\t4.DELETE A NUMBER";
cout<<"\n\t5.ADD THE ELEMENTS";
cout<<"\n\t6.SELECTION SORTING";
cout<<"\n\t7.BUBBLE SORTING";
cout<<"\n\t8.INSERTION SORT";
cin>>a;
switch(a)
{
case 1: linear(ary,n);
break;
case 2: binary(ary,n);

//to ask the choice


//function call

break;
case 3: insert(ary,n);
break;
case 4: delete_item(ary,n);
break;
case 5: merge(ary,arry);
break;
case 6: selection_sort(arry,n);
break;
case 7: bubble_sort(arry,n);
break;
case 8: insert_sort(arry,n);
break;
default: cout<<"wrong choice";
} }
function close

C++ Output -

One dimensional linear searching:


C++ code-

//main body

void linear(int ar[],int&n)


//function definition
{int item;
cout<<"enter the no to be searched for\n";
cin>>item;
//store the value
j=0,k;
char q='n';
for(j; j<n ;j++)
{ if(ar[j]==item)
{ q='f';
k=j; } }
if(q=='f')
cout<<"the number is at ary["<<k<<"]";
else
cout<<"no such number found";
}

//linear search

over

C++ output-

One dimensional binary searching:


C++ codevoid binary(int ar[],int&n)
//function definition
{
int item;
cout<<"enter the number to be searched for\n";
cin>>item;
//store the value
int f=0,l=n-1,m,r=0;
while(f<=l&&r==0)
{
m=(f+l)/2;
if(item==ar[m])
r= ar[m];
else if (item<ar[m])
l=m-1;
else if(item>ar[m])
f=m+1;
}

if(r==0)
cout<<"no such number found";
else
cout<<"the number has been located "<<r;
}
// function binary
search close

C++ output-

Inserting a new number in the array:


C++ codevoid insert(int ar[],int &n)

//function definition

{clrscr();
int item;
cout<<"enter the number to be inserted";
cin>>item;

//store the value

int j=n-1;
while(j>=0&&ar[j]>item)
{ar[j+1]=ar[j];

//shifting the values

j--; }
ar[j+1]=item;
number
n++;
numbers

//inserting the new


//increase the

cout<<"\nthe new array will look as folllows:";


for(int k=0;k<n;k++)
{ cout<<"\narray["<<k<<"]= "<<ar[k];
}

//new array

//insert

function close
C++ output-

Deleting a number from the existing


array:
C++ codevoid delete_item(int ar[],int &n)

//function definition

{int item;
cout<<"enter the number to be deleted";
cin>>item;
number

//store the

int j=0;
while(j<n&&ar[j]<item)
j++;
if(ar[j]==item)
value
{ while(j<n)

//searching for the

{ ar[j]=ar[j+1];
value

//replacing the

j++; }
cout<<"\nelement is deleted\n";
n--;
numbers

//decreasing the

cout<<"\nthe new array will look as folllows:";


for(int k=0;k<n;k++)
cout<<"\narray["<<k<<"]= "<<ar[k]; }

//new array

else cout<<"\nelement not found";


}
closed

//delete function

C++ output-

Merging of two different arrays:


C++ codevoid merge(int a[],int b[])
{
clrscr();
int newa[10];
for(int k=0;k<10;k++)
newa[k]=a[k]+b[k];
cout<<"\t\tnew array would be:\n";
for(int l=0;l<10;l++)

{
cout<<"\t\tnew["<<l<<"]= "<<newa[l]<<"\n";
}//for
getche();
}//merging two array

C++ output-

selection shorting in one dimensional


array:
C++ codevoid selection_sort(int ar[],int&n)
{
int i,j,p,small;
for(i=0;i<n-1;i++)
{
small=ar[i];
p=i;
for(j=i+1;j<n;j++)
{
if(ar[j]<small)
{ small=ar[j];
//replacing the
value
p=j;
}//if
}//forj
ar[p]=ar[i];
ar[i]=small;
} // for i
clrscr();
cout<<"\t\t array after complete sorting:\n";

for(int k=0;k<n; k++)


cout<<"\n\t\t\t array["<<k<<"]="<<ar[k];
}
//selection sorting function
close

C++ output-

Bubble shorting in one dimensional array:


C++ codevoid bubble_sort(int ar[],int&n)
{ int sml,i,j;
for(i=0;i<n;i++)
{ for(j=0;j<(n-1)-i;j++)
{if(ar[j]>ar[j+1])
{ sml=ar[j+1];
values

//replacing the

ar[j+1]=ar[j];
ar[j]=sml; }//if
}//for j }//for i
cout<<"\t\tarray after complete sorting:\n";
for(int k=0;k<n;k++)
cout<<"\n\t\t\tarray["<<k<<"]="<<ar[k]; }
function close

//bubble sorting

C++ output-

Insertion sorting in one dimensional array:


C++ codevoid insert_sort(int ar[],int&n)
{int i,j,p;
for(i=0;i<n;i++)
{for(j=i;j>=0;j--)
{ while(ar[i]<ar[j])
{ p=ar[j];
ar[j]=ar[i];
//replacing the value
ar[i]=p;
}
//while
}
//for j
} //for i
cout<<"\t\tarray after complete sorting:\n";
for(int k=0;k<n;k++)
cout<<"\n\t\t\tarray["<<k<<"]="<<ar[k];
}
//insertion sorting function
close

C++ output-

II Two dimensional program for various


functionalities:
C++ code for creation , initiation and traversal of
array:
#include <iostream.h>
# include<conio.h>
int main ()
{
columns.

// an array with 5 rows and 2

int a[5][2] = { {0,0}, {1,2}, {2,4}, {3,6},{4,8}};


for ( int i = 0; i < 5; i++ )
value

// output each array element's

for ( int j = 0; j < 2; j++ )


{ cout << "a[" << i << "][" << j << "]: ";
cout << a[i][j]<< endl }
return 0;
}

C++ output-

III Classes and objects:


Declaration of class, its object and
constructor :
C++ code-

class student
{ private:

//class with name student


//access operator

int rno;

//data members

float mrk;
public:
student()

//constructor

{rno=1;
mrk=90; }
void display()

//member function

{cout<<"rno ="<<rno;
cout<<"\nmrk ="<<mrk; }
};

//end of class student

void main()

//main function

{ student S1;

//creating class object S1

S1.display();

//function from class using object

C++ output-

Inheritance in class :
C++ codeint area();
class shape
{ public:
int length ,width;
private:

//function prototype
//class(I) declaration

int setl(int i)
{ length=i;
return(0); }
int setw(int i)
{ width=i;
return(0); } };
class price

//class(II) declaration

{ public:
int setprc(int i)
{ int price=i*100;
return price; } };
class rectangle:public shape,public price //class(III)declaration and
inheritance
{ public:
int area()
{ int i;
i=length*width;
return(i); } };
void main()
{ clrscr();
int a,b,c,d;
cout<<"enter the length ";
cin>>a;
cout<<"enter the width ";
cin>>b;
rectangle rect;
rect.setl(a);
rect.setw(b);
c=rect.area();

cout<<"area of shape = "<<c;


d=rect.setprc(c);
cout<<"\nprice = "<<d; }

C++ output-

IV STACKS :
Stack using array:
C++ code# include<iostream.h>
# include<conio.h>
void PUSH();
void POP();
class stack
{ private:
int ar[5];
int top;
public:
stack()
{ top=-1; }
void push(int item)
{ if(top==-1||top<4)
{ ar[top]=item;
top++;
cout<<"\nar["<<top<<"] ="<<item; }
else

cout<<"the stak is already full";


getche(); }
void pop()
{ if(top==-1)
cout<<"stsck is empty" ;
else
{ cout<<"the array is ar["<<top<<"]="<<ar[top];
top--; } } }S1;
void main()
{ cout<<"\n\t\tMAIN MENU";
cout<<"\n\n\t\t1.PUSH OPERATION";
cout<<"\n\t\t2.POP OPERATION";
cout<<"\nenter your choice";
int a;
cin>>a;
switch(a)
{ case 1: PUSH();
break;
case 2: POP();
break;
default: cout<<"\nwrong choice"; }
void PUSH()
{ cout<<"push:\n";
S1.push(4);
S1.push(8);
S1.push(5);
S1.push(7);
S1.push(6);
S1.push(9);

getche(); }
void POP()
{ cout<<"pop:\n";
for(int i=0;i<6;i++)
{ S1.pop(); } }

C++ output-

Stack using linked list:


C++ code# include<iostream.h>
# include<conio.h>
# include<process.h>
struct node
{ int data;
node*next; };
class stack
{ node*top;
public:
stack()
{ top=NULL; }//constructor
void push();

void pop();
void display();
~stack()
{}//destructor
}st;//class stack
void stack::push()
{node*temp;
temp=new node;
cout<<"enter value";
cin>>temp->data;
temp->next=top;
top=temp;}
void stack::pop()
{ if(top!=NULL)
{ node*temp=top;
top=top->next;
delete temp;
cout<<"\nNEW STACK:";
st.display(); }//if
else cout<<"the stack is empty";}
void stack::display()
{ if(top==NULL)
cout<<"the stack is empty";
node*temp=top;
while(temp!=NULL)
{ cout<<"\nthe values are";
cout<<"\n"<<temp->data;
temp=temp->next;
}

}
void main()
{
char ch;
do{ cout<<"\n\t\tStack option";
cout<<"\n\tP:for push";
cout<<"\n\tO:for pop";
cout<<"\n\tD:for display";
cout<<"\n\tQ:for quit";
cin>>ch;
switch(ch)
{
case'P':st.push();
break;
case'O':st.pop();
break;
case'D':st.display();
break;
case'Q':exit(0);
break;
default:cout<<"\nWrong choice";
}
}
while(ch!='Q');//do while
}

C++ output-

V QUEUE USING ARRAY & LINK LIST


IMPLEMENTATION
QUEUE USING ARRAY:
C++ code#include <iostream>
using namespace std;
int main ()
{int a[5][2]={ {0,0}, {1,2}, {2,4}, {3,6},{4,8}};// an array with 5 rows
and 2 column
for ( int i = 0; i < 5; i++ )
for ( int j = 0; j < 2; j++ )
{ cout << "a[" << i << "][" << j << "]: "; // output each array
element's value
cout << a[i][j]<< endl;
return 0;}

C++ output-

QUEUE USING LINK LIST


IMPLEMENTATION:

C++ code#include<conio.h>
#include<iostream.h>
#include<process.h>
#include<malloc.h>
struct node
// Creating a NODE Structure
{ int data;
struct node *next; };
class queue
// Creating a class QUEUE
{ struct node *frnt,*rear;
public:
queue() // constructure
{ frnt=rear=NULL; }
void insert(); // to insert an element
void del(); // to delete an element
void show(); // to show the stack };
void queue::insert()
// Insertion
{ int value;
struct node *ptr;
cout<<"\nInsertion\n";
cout<<"Enter a number to insert: ";

cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(frnt==NULL)
frnt=ptr;
else rear->next=ptr;
rear=ptr;
cout<<"\nNew item is inserted to the Queue!!!";
getch(); }
void queue::del()
// Deletion
{ if(frnt==NULL)
{ cout<<"\nQueue is empty!!";
getch();
return; }
struct node *temp;
temp=frnt;
frnt=frnt->next;
cout<<"\nDeletion Operation........\nDeleted value is "<<temp->data;
delete temp;
getch(); }
void queue::show()
// Show Queue
{ struct node *ptr1=frnt;
if(frnt==NULL)
{ cout<<"The Queue is empty!!";
getch();
return; }
cout<<"\nThe Queue is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next; }
cout<<"END";
getch();}
int main()
{ clrscr();
queue q;
int choice;
while(1)
{cout<<"\n-----------------------------------------------------------";
cout<<"\n\t\tQUEUE USING LINKED LIST\n\n";
cout<<"1:INSERTION\n2:DELETION\n3:DISPLAY QUEUE\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";

cin>>choice;
switch(choice)
{
case 1: q.insert();
break;
case 2: q.del();
break;
case 3:q.show();
break;
case 4: exit(0);
break;
default: cout<<"Please enter correct choice(1-4)!!";
getch();
break; }}return 0;}

C++ output-

VI FILE (BINARY & TEXT) OPERATION (CREATION,


UPDATION, QUERY):
Binary file creation, updating ,Query:
C++ code#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
class Student
{ int admno;
char name[50];
public:
void setData()
{cout << "\nEnter admission no. ";
cin >> admno;
cout << "Enter name of student ";
gets(name); }
void showData()
{ cout << "\nAdmission no. : " << admno;
cout << "\nStudent Name : " << name; }
int retAdmno()
{ return admno; }};
void write_record()
// function to write in a binary file.
{ ofstream outFile;
outFile.open("studentA.dat", ios::binary | ios::app);
Student obj;
obj.setData();
outFile.write((char*)&obj, sizeof(obj));
outFile.close();}
void display()
// function to display records of file
{ ifstream inFile;
inFile.open("studentA.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{ obj.showData(); }
inFile.close(); }
void search(int n)
// function to search and display from binary file
{ ifstream inFile;

inFile.open("studentA.dat", ios::binary);
Student obj;
while(inFile.read((char*)&obj, sizeof(obj)))
{ if(obj.retAdmno() == n)
{

obj.showData();} }

inFile.close(); }
void delete_record(int n)
// function to delete a record
{ Student obj;
ifstream inFile;
inFile.open("studentA.dat", ios::binary);
ofstream outFile;
outFile.open("temp.dat", ios::out | ios::binary);
while(inFile.read((char*)&obj, sizeof(obj)))
{ if(obj.retAdmno() != n)
{

outFile.write((char*)&obj, sizeof(obj)); }}

inFile.close();
outFile.close();
remove("studentA.dat");
rename("temp.dat", "studentA.dat"); }
void modify_record(int n)

//function to modify a record

{ fstream file;
file.open("studentA.dat",ios::in | ios::out);
Student obj;
while(file.read((char*)&obj, sizeof(obj)))
{ if(obj.retAdmno() == n)
{

cout << "\nEnter the new details of student";


obj.setData();

int pos = -1 * sizeof(obj);


file.seekp(pos, ios::cur);
file.write((char*)&obj, sizeof(obj));
file.close();}
int main()
{clrscr();

}}

for(int i = 1; i <= 4; i++)

//Store 4 records in file

write_record();
cout << "\nList of records";

//Display all records

display();
cout << "\nSearch result";
search(100);
delete_record(100);

//Search record
//Delete record

cout << "\nRecord Deleted";


cout << "\nModify Record 101 ";
modify_record(101);

//Modify record

return 0;}

C++ output-

Text file creation, updating ,Query:


C++ code#include <fstream.h>
#include<conio.h>

int main()
{ofstream fout;
fout.open("out.txt");
char str[300] = "If your ship doesn't come,swim your way out
to reach it.";
fout << str; //Write string to the file.
fout.close();
getche();
ifstream ifile;
ifile.open("out.txt");
char ch;
while(!ifile.eof())
{ ifile.get(ch);
cout<<ch;}
ifile.close();
getche();
return 0; }

C++ output-

STRUCTURAL QUERY LANGUAGE


SQL commands:
Before starting with the programming we need to create a
database to store our data in it.
SQL code:
create database <database_name>;
Examplecreate database project;
SQL output: screenshot

To store this data in this database project we give


command to bring it in service.
SQL code:
use <database_name>;
Exampleuse project;
SQL output: screenshot
1 Creating a table:
SQL code:
create table CAR( Ccode integer primary key ,carname varchar(20) NOT
NULL,make varchar(30) NOT NULL,color varchar(10) default 'white,
capacity integer(2) check(capacity<=8),average integer(2)
check(average<=40));
desc CAR;
SQL output:

2 Inserting and display values of the table:


SQL code:
insert into CAR values(10000,'mercedes', 'honda','gray',6,35);
insert into CAR values(10001,'volvo', 'honda','silver',6,40);
insert into CAR values(10002,'indigo', 'maruti','black',6,35);
insert into CAR values(10003,'inova', 'nissan',default,6,35);
insert into CAR values(10004,'kwid', 'tyota','yellow',6,35);
insert into CAR values(10005,'nano', 'tata','red',6,10);
select *from CAR ;
SQL output:

3 Creating two tables and linking them:


SQL code:
create table customer (Ccode integer ,cname varchar(30) NOT
NULL,cust_code integer(5) NOT NULL, foreign key(Ccode) references
CAR(Ccode));
desc customer;
SQL output:

4 To view combined result:


SQL code:
select carname, make, color, capacity, average, cname from CAR A,
customer B where A.Ccode=B.Ccode;
SQL output:

5 To delete a record:
SQL code:
delete from customer where cname like 'anuj';
SQL output:

6 To update a record:
SQL code:
update CAR set color ='white' where color like 'yellow';
select *from CAR ;
SQL output:

7 To group the data by color:


SQL code:
select carname, color,cname from CAR A, customer B where
A.Ccode=B.Ccode group by color;
SQL output:

8 To count the number of records:


SQL code:
select count(*) from customer;
SQL output:

9 To find maximum value:

SQL code:
select max(average) from CAR;
SQL output:

10To find minimum value:


SQL code:
select min(average) from CAR;
SQL output:

11To find average value:


SQL code:
select avg(average) from CAR;
SQL output:

12To add the values:


SQL code:
select sum(average) from CAR;
SQL output:

13To add a new column:

SQL code:
alter table customer add (to_check varchar(3));
SQL output:

14To change the column settings:


SQL code:
alter table customer modify to_check varchar(12);
SQL output:

15To delete a column:


SQL code:
alter table customer drop to_check;
SQL output:

You might also like