Académique Documents
Professionnel Documents
Culture Documents
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.
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
II.
Binary search
III.
SORTING
bubble sort
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";
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
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();
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";
}
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";
}
}
*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";
}
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();
}
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
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;
}
}
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();
}
{
cout<<"Error in opening file..!!";
getch();
exit(0);
}
while(ifile.eof()==0)
{
ifile>>s;
cout<<s<<" ";
}
cout<<"\n";
ifile.close();
getch();
}
I.
Alter
Alter
Alter
Alter
Alter
DML COMMANDS
SELECT COMMANDS: to display all the records of the table :
Select * from employee;
Order by Ascending:
select*from employee order by first_name;
Order by Descending:
select*from employee order by first_name desc;