Vous êtes sur la page 1sur 30

PRACTICAL FILE

COMPUTER SCIENCE
2016-2017

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

INTRODUCTION
This file is based on different types command used in the C++ Programming. This will the reader to
gain knowledge in C++ concepts and increase the ability of the user.
This file contains all type programs, terminology related to C++. 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.

Header Files and Their Purposes


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

IOSTREAM.H-For input & output console


FSTREAM.H-For file handling, cin, cout
PROCESS.H-For exit () function
CTYPE.H-For character handling
CONIO.H-For clrscr () and getche () function
STDIO.H-for standard I/O operations
STIRING.H-For string dandling

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

Linear search

// Taking Input In Array


for(int j=0;j<size;j++){
cout<<"Enter "<<j+1<<" Element: ";
cin>>array[j];
}
clrscr();
cout<<"Your Entered Array is :\n";
//Your Entered Array Is
for(int a=0;a<size;a++){
cout<<"array[ "<<a+1<<" ] = ";
cout<<array[a]<<endl;
}
cout<<"Enter Key To Search in Array";
cin>>key;
for(i=0;i<size;i++)
{
if(key==array[i])
{
break;
}
}
OUTPUT:

II.

Binary search

int bsearch(int ar[],int size,int item)


{
int beg=0,mid,last=size-1;
while(beg<=last)
{
mid=int(beg+size)/2;
if(ar[mid]==item)
return mid;
else
if(item>ar[mid])
beg=mid+1;
else last=mid-1;
}
return -1;
}

III.

SORTING

bubble sort

//Taking input in array


cin>>array[i];
}
cout<<endl;
cout<<"Input array is: "<<endl;
for(int j=0; j<5; j++)
{
//Displaying Array
cout<<"\t\t\tValue at "<<j<<" Index: "<<array[j]<<endl;
}
cout<<endl;
// Bubble Sort Starts Here
int temp;
for(int i2=0; i2<=4; i2++)
{
for(int j=0; j<4; j++)
{
//Swapping element in if statement
if(array[j]>array[j+1])
{
temp=array[j];
array[j]=array[j+1];
array[j+1]=temp;
}
}

Selection sort in an array:


void sel_sort(int arr[],int size)
{
int small,pos,tmp;
for(int i=0;i<size-1;i++)
{
small=arr[i];
pos=i;
for(int j=i+1;j<size;j++)
{
if(arr[j]<small)
{ small=arr[j];
pos=j;
}
}
tmp=arr[i];
arr[i]=arr[pos];
arr[pos]=tmp;
cout<<"\narray after pass-"<<i+1<<"-is:";
for(j=0;j<size;j++)
cout<<arr[j]<<" ";
}
}

INSERTION
void main()
{
clrscr();
int arr[50], size, insert, i, pos;
cout<<"Enter Array Size : ";
cin>>size;
cout<<"Enter array elements : ";
for(i=0; i<size; i++)
{
cin>>arr[i];
}
cout<<"Enter element to be insert : ";
cin>>insert;
cout<<"At which position (Enter index number) ? ";
cin>>pos;

pos=pos;
// now create a space at the required position
for(i=size; i>pos; i--)
{
arr[i]=arr[i-1];
}
arr[pos]=insert;
cout<<"Element inserted successfully..!!\n";
cout<<"Now the new array is : \n";
for(i=0; i<size+1; i++)
{
cout<<arr[i]<<" ";
}
getch();
}

DELETION
main()
{ clrscr();
int LA[] = {1,3,5,7,8};
int k = 0, n = 5;
int i, j;
cout<<"The original array elements are :\n";
for(i = 0; i<n; i++)
{
cout<<"LA["<<i<<"]" << LA[i]<<"\n";
}
cout<<"Enter the index of number be deleted "; cin>>k;
j = k;
while( j < n){
LA[j-1] = LA[j];
j = j + 1;
}
n = n -1;
cout<<"The array elements after deletion :\n";

for(i = 0; i<n; i++) {


cout<<"LA["<<i<<"]"<<LA[i]<<"\n";
}
getche();

MERGING
/* C++ Program - Merge Two Arrays */
void main()
{
clrscr();
int arr1[50], arr2[50], size1, size2, size, i, j, k, merge[100];
cout<<"Enter Array 1 Size : ";
cin>>size1;
cout<<"Enter Array 1 Elements : \n";
for(i=0; i<size1; i++)
{
cin>>arr1[i];
}
cout<<"Enter Array 2 Size : ";
cin>>size2;
cout<<"Enter Array 2 Elements : \n";
for(i=0; i<size2; i++)
{
cin>>arr2[i];
}
for(i=0; i<size1; i++)
{
merge[i]=arr1[i];
}
size=size1+size2;
for(i=0, k=size1; k<size && i<size2; i++, k++)
{
merge[k]=arr2[i];
}

2. 2 D CREATION
C++ Program - Two Dimensional Array Program

void main()
{
cout<< 2-D ARRAY CREATION\n";
int arr[10][10], row, col, i, j;
cout<<"Enter number of row for Array (max 10) : ";
cin>>row;
cout<<"Enter number of column for Array (max 10) : ";
cin>>col;
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
cout<<"Enter element a["<<i<<"]["<<j<<"] ";
cin>>arr[i][j];
}
}
cout<<"The Array is :\n";
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
{
cout<<arr[i][j]<<" ";
}
cout<<"\n";
}

Pushing in Stack-Array

STACKS

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


void display(int [], int);
const int SIZE = 50;
void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key to exit..\n";

getch();
exit(1);

}
cout<<"Element inserted successfully..!!\n";
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;

}
getch();

int push(int stack[], int &top, int elem)


{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}
void display(int stack[], int top)
{
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}

Pushing in Linked-Stack
struct node
{
int info;
node *next;
} *top, *newptr, *save, *ptr;

node *create_new_node(int);
void push(node *);
void display(node *);
void main()
{
clrscr();
int inf;
char ch='y';
top=NULL;
while(ch=='y' || ch=='Y')
{
cout<<"Enter information for the new node.. ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{
cout<<"\nSorry..!!..Cannot create new node..!!..Aborting..!!\n";
cout<<"Press any key to exit..\n";
getch();
}
push(newptr);
cout<<"\nNow the linked-stack is:\n";
display(top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
getch();
}

node *create_new_node(int x)
{
ptr = new node;
ptr->info = x;
ptr->next = NULL;
return ptr;
}
void push(node *n)
{
if(top==NULL)
{
top=n;
}
else
{
save = top;
top = n;
n->next = save;
}
}
void display(node *n)
{
while(n != NULL)
{
cout<<n->info<<" -> ";
n = n->next;
}
cout<<"!!\n";
}

Here is the sample output of this C++ program:

Popping
Popping from Array-Stack
void main()
{
clrscr();
int stack[SIZE], item, top=-1, res;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
res = push(stack, top, item);
if(res == -1)
{
cout<<"Overflow..!!..Aborting..Press a key to exit..\n";
getch();
exit(1);
}
cout<<"\nThe Stack now is:\n";
display(stack, top);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
cout<<"Now the deletion of elements starts..\n";
ch='y';
while(ch=='y' || ch=='Y')
{
res = pop(stack, top);
if(res==-1)
{
cout<<"\nUnderflow..!!..Aborting..!!..Press a key to exit..\n";
getch();
exit(2);
}
else
{
cout<<"\nElement deleted is: "<<res<<endl;
cout<<"\nThe Stack now is:\n";
display(stack, top);

}
cout<<"Want to delete more ? (y/n).. ";
cin>>ch;
}
getch();
}
int push(int stack[], int &top, int elem)
{
if(top == SIZE-1)
{
return -1;
}
else
{
top++;
stack[top] = elem;
}
return 0;
}
int pop(int stack[], int &top)
{
int ret;
if(top==-1)
{
return -1;
}
else
{
ret=stack[top];
top--;
}
return ret;
}
void display(int stack[], int top)
{
if(top==-1)
{
return;
}
cout<<stack[top]<<" <-- "<<"\n";
for(int i=top-1; i>=0; i--)
{
cout<<stack[i]<<"\n";
}
}

Popping from a Linked-Stack


struct node
{
int info;
node *next;
} *top, *newptr, *save, *ptr;
node
void
void
void

*create_new_node(int);
push(node *);
pop();
display(node *);

void main()
{
clrscr();
int inf;
char ch='y';
top=NULL;
while(ch=='y' || ch=='Y')
{
cout<<"Enter information for the new node.. ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{
cout<<"\nSorry..!!..Cannot create new node..!!..Aborting..!!\n";
cout<<"Press any key to exit..\n";
getch();
exit(1);
}
push(newptr);
cout<<"\nWant to enter more ? (y/n).. ";
cin>>ch;
}
clrscr();
do
{
cout<<"The Stack now is: \n";
display(top);
cout<<"\nWant to pop an element ? (y/n).. ";

cin>>ch;
if(ch=='y' || ch=='Y')
{
pop();
}
cout<<"\n";
}while(ch=='y' || ch=='Y');
getch();
}
node *create_new_node(int x)
{
ptr = new node;
ptr->info = x;
ptr->next = NULL;
return ptr;
}
void push(node *n)
{
if(top==NULL)
{
top=n;
}
else
{
save = top;
top = n;
n->next = save;
}
}
void pop()
{
if(top==NULL)
{
cout<<"\nUnderflow..!!..Press any key to exit..\n";
getch();
exit(2);
}
else
{
ptr = top;
top = top->next;
delete ptr;
}
}
void display(node *n)
{
while(n != NULL)
{
cout<<n->info<<" -> ";
n = n->next;
}
cout<<"!!\n";
}

Insertion in Array-Queue
int insert_in_queue(int [], int);
void display(int [], int, int);
const int SIZE = 50;
int queue[SIZE];
int front=-1;
int rear=-1;
void main()
{
clrscr();
int item, check;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
check = insert_in_queue(queue, item);
if(check == -1)
{
cout<<"\nOverflow..!!..Aborting..!!..Press a key to
exit..\n";
getch();
exit(1);
}
cout<<"Item inserted successfully..!!\n";
cout<<"\nNow the Queue (Front...to...Rear) is:\n";
display(queue, front, rear);
cout<<"\nWant to insert more ? (y/n).. ";
cin>>ch;
}
getch();
}
int insert_in_queue(int queue[], int elem)
{
if(rear == SIZE-1)
{
return -1;
}
else if(rear == -1)
{
front = rear = 0;
queue[rear] = elem;
}
else
{
rear++;
queue[rear] = elem;
}
return 0;
}
void display(int queue[], int front, int rear)

{
if(front == -1)
{
return;
}
for(int i=front; i<rear; i++)
{
cout<<queue[i]<<" <- ";
}
cout<<queue[rear]<<"\n";
}

int delete_from_queue(int []);


int insert_in_queue(int [], int);
void display(int [], int, int);
const int SIZE = 50;
int queue[SIZE];
int front=-1;
int rear=-1;
void main()
{
clrscr();
int item, check;
char ch='y';
while(ch=='y' || ch=='Y')
{
cout<<"Enter item for insertion: ";
cin>>item;
check = insert_in_queue(queue, item);
if(check == -1)
{
cout<<"\nOverflow..!!..Aborting..!!..Press a key to exit..\n";
getch();
exit(1);
}
cout<<"Item inserted successfully..!!\n";
cout<<"\nNow the Queue (Front...to...Rear) is:\n";
display(queue, front, rear);
cout<<"\nWant to insert more ? (y/n).. ";
cin>>ch;
}

clrscr();
cout<<"Now deletion of elements starts...\n";
ch='y';
while(ch=='y' || ch=='Y')
{
check = delete_from_queue(queue);
if(check == -1)
{
cout<<"\nUnderflow..!!..Aborting..!!..Pres a key to exit..\n";
getch();
exit(2);
}
else
{
cout<<"\nElement deleted is: "<<check<<"\n";
cout<<"Now the Queue (Front...to...Rear) is:\n";
display(queue, front, rear);
}
cout<<"\nWant to delete more ? (y/n)... ";
cin>>ch;
}
}

getch();

int insert_in_queue(int queue[], int elem)


{
if(rear == SIZE-1)
{
return -1;
}
else if(rear == -1)
{
front = rear = 0;
queue[rear] = elem;
}
else
{
rear++;
queue[rear] = elem;
}
return 0;
}
int delete_from_queue(int queue[])
{
int retn;
if(front == -1)
{
return -1;
}
else
{
retn = queue[front];
if(front == rear)
{
front = rear = -1;
}
else
{
front++;
}
}
return retn;

}
void display(int queue[], int front, int rear)
{
if(front == -1)
{
return;
}
for(int i=front; i<rear; i++)
{
cout<<queue[i]<<" <- ";
}
cout<<queue[rear]<<"\n";
}

Deletion
struct node
{
int
node
} *front,
node *create_new_node(int);
void insert(node *);
void
void

from Linked Queue

info;
*next;
*newptr, *save, *ptr, *rear;

delete_node_queue();
display(node *);

void main()
{
front
int
int
char

clrscr();
= rear = NULL;
inf;
count=0;
ch='y';

while(ch=='y' || ch=='Y')
{
cout<<"Enter information for the new node.. ";
cin>>inf;
newptr = create_new_node(inf);
if(newptr == NULL)
{
cout<<"\nSorry..!!..Cannot create new node..!!..Aborting..!!\n";
cout<<"Press any key to exit..\n";
getch();
exit(1);
}
insert(newptr);
cout<<"\nNow the Queue (Front...to...Rear) is:\n";
display(front);
cout<<"\nWant to enter more ? (y/n).. ";

cin>>ch;
}
clrscr();
do
{
cout<<"The Linked-Queue now is (Front...to...Rear) is:\n";
display(front);
if(count==0)
{
cout<<"\nWant to delete ? (y/n).. ";
count++;
}
else
{
cout<<"\nWant to delete more ? (y/n).. ";
}
cin>>ch;
if(ch=='y' || ch=='Y')
{
delete_node_queue();
}
cout<<"\n";
}while(ch=='y' || ch=='Y');
}

getch();

node *create_new_node(int x)
{
ptr = new node;
ptr->info = x;
ptr->next = NULL;
return ptr;
}
void insert(node *n)
{
if(front == NULL)
{
front = rear = n;
}
else
{
rear->next = n;
rear = n;
}
}
void delete_node_queue()
{
if(front == NULL)
{
cout<<"\nOverflow..!!..Press a key to exit..\n";
getch();
exit(2);
}
else
{
ptr = front;
front = front->next;
delete ptr;
}
}

void display(node *n)


{
while(n != NULL)
{
cout<<n->info<<" -> ";
n = n->next;
}
cout<<"!!\n";
}

class STUDENT
{
private:
int rollno;
char name[40];
float marks;

OBJEC
TS

char grade;
public:

};

void read()
// mutator
{
cout<<"\nEnter rollno: ";
cin>>rollno;
cout<<"Enter name: ";
gets(name);
cout<<"Enter marks: ";
cin>>marks;
}
void display()
// accessor
{
calculategrade();
cout<<"Roll no.: "<<rollno<<"\n";
cout<<"Name: "<<name<<"\n";
cout<<"Marks: "<<marks<<"\n";
cout<<"Grade: "<<grade<<"\n";
}
int getrollno()
// accessor
{
return rollno;
}
float getmarks()
// accessor
{
return marks;
}
void calculategrade() // mutator
{
if(marks>=80)
{
grade = 'A';
}
else if(marks>=60)
{
grade = 'B';
}
else if(marks>=40)
{
grade = 'C';
}
else
{
grade = 'F';
}
}

class Box
{
public:
double leng;
double brea;
double heig;

// Length of a box
// Breadth of a box
// Height of a box

};
void main()
{
clrscr();
Box box1obj;
// Declared box1obj of type Box
Box box2obj;
// Declared box2obj of type Box
double volume = 0.0;
// Store the volume of a box here
// box 1 specification
box1obj.heig = 5.0;
box1obj.leng = 6.0;
box1obj.brea = 7.0;
// box 2 specification
box2obj.heig = 10.0;
box2obj.leng = 12.0;

box2obj.brea = 13.0;
// calculate volume of box 1
volume = box1obj.heig * box1obj.leng * box1obj.brea;
cout<<"Volume of Box1 = "<<volume<<"\n";
// calculate volume of box 2
volume = box2obj.heig * box2obj.leng * box2obj.brea;
cout<<"Volume of Box2 = "<<volume<<"\n";
getch();
}

class ITEM
{
int itemno;
float price;
public:
void getdata(int i, float f)
{
itemno=i;
price=f;
}
void putdata(void)
{
cout<<"Itemno: "<<itemno<<"\t";
cout<<"Price: "<<price<<"\n\n";
}
};
ITEM order[5];
void main()
{
clrscr();
int ino;
float cost;
for(int z=0; z<5; z++)
{
cout<<"Enter itemno and itemprice for item "<<z+1<<"\n";
cin>>ino>>cost;
order[z].getdata(ino, cost);
}
cout<<"\n";
for(z=0; z<5; z++)
{
cout<<"Item "<<z+1<<"\n";
order[z].putdata();
}
getch();
}

Now after entering the details, press ENTER and here is the output:

FILE
1. TEXT FILE
WRITING void main()
{
clrscr();
ofstream fp;
char s[100], fname[20];
cout<<"Enter a file name with extension (like file.txt) to create a file :
";
gets(fname);
fp.open(fname);
if(!fp)
{
cout<<"Error in opening file..!!";

getch();
exit(1);
}
cout<<"Enter few lines of text :\n";
while(strlen(gets(s))>0)
{
fp<<s;
fp<<"\n";
}
fp.close();
getch();
}

READING/* C++ Program - Read a File */


void main()
{
clrscr();
char c[1000];
ifstream ifile;
ifile.open("filename.txt") ;
if(!ifile)
{
cout<<"Error in opening file..!!";
getch();
exit(1);
}
cout<<"Data in file = ";
while(ifile.eof()==0)
{
ifile>>c;
cout<<c<<" ";
}
ifile.close();
getch();
}

/* C++ Program - Read and Display File */


void main()
{
clrscr();
ifstream ifile;
char s[100], fname[20];
cout<<"Enter file name to read and display its content (like file.txt) : ";
cin>>fname;
ifile.open(fname);
if(!ifile)

{
cout<<"Error in opening file..!!";
getch();
exit(0);
}
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
}
cout<<"\n";
ifile.close();
getch();
}

SQL: PROJECT FILE WITH COMMANDS AND OUTPUT


DDL (Data Definition Language) ------ provides a set of definitions to specify the storage
structure and access
Methods used by the database system.
1. TO CREATE A TABLE
To create a table in the MySQL:
create table employee(emp_id integer primary key,
first_name varchar(25) not null,
last_name varchar(25),
date_join date,
basic_sal decimal(8,2) check(basic_sal>=2000),
dept_id integer default(20));
To get the description of the table:
Command: desc employee;

I.
Alter
Alter
Alter
Alter
Alter

Alter table commands:

Add a column in the table:


employee add month int;
Drop the existing column:
employee drop month;
Changing a column definition or name:
employee modify basic_sal int;
: Effect of alter table on NULL and Default value attributes
employee modify date_join not null default 01-01-2016;
Changing a columns default value:
employee dept_id default(10);

DML COMMANDS
SELECT COMMANDS: to display all the records of the table :
Select * from employee;

Commands using where clause


select * from employee where first_name not lik

select * from employee where first_name like 's%';

Select*from employee where basic_sal!=5000;

Insert commands:To insert a record in the database:


Insert into employee values (12345681,'Hritik','ambdkr',"2011-12-1","8000", default);

Update the data existing in the table


update employee set basic_sal="11000"where first_name='sumit';
AFTER UPDATE TABLE IS LIKE THIS:-

Order by Ascending:
select*from employee order by first_name;

Order by Descending:
select*from employee order by first_name desc;

To delete the record:


delete from employee where first_name="sumit";

the record of sumit is deleted from the table:

Some more commands in mysql:


Group by commands

SELECT COUNT(*) FROM EMPLOYEES WHERE FIRSTAME LIKE 'S%';

select distinct first_name from employee where basic_sal<=5000;

select dept_id, count(*) from employee group by dept_id ;

Vous aimerez peut-être aussi