COMPUTER
SCIENCE
PRACTICAL
FILE
(Based on C++ and SQL)
Name
Name
Name
Name ----
Shlok Sah
Shlok Sah
Shlok Sah
Shlok Sah
Class
Class
Class
Class ----
XII Newton
XII Newton
XII Newton
XII Newton
School
School
School
School ----
Manav Rachna International School
Manav Rachna International School
Manav Rachna International School
Manav Rachna International School
Roll No
Roll No
Roll No
Roll No----
INDEX
C++ Programs
o
Data File Handling [4]
o
Arrays [5]
o
Structures [1]
o
Inheritance [2]
o
Stacks [2]
o
Queues [2]
o
Linked Lists [1]
o
Iteration [2]
o
Pointers [2]
o
Function Overloading [2]
SQL Commands
C++
/* Wap to create a text file to input roll no. and marks of five students and display them on screen after reading from the text file using data file handling. */
#include<fstream.h> // Header files declared
#include<conio.h> void main()
{
clrscr(); int a[5]; float b[5];
for(int i=0; i<5; i++) {
cout<<"\n\nEnter the roll no. of student "<<i+1<<" : "; cin>>a[i]; cout<<"\n\tEnter the marks of student "<<i+1<<" => "; cin>>b[i]; }
ofstream student;
student.open("stud.txt");
for(i=0; i<5; i++) // writing to “stud.txt”
{
student<<"\n\nMarks of roll no. "<<a[i]<<" => "<<b[i]; }
student.close(); i=0;
cout<<”\n\nStudent details are :\n\n”; char str[80][80];
ifstream stude;
stude.open("stud.txt",ios::in); stude.seekg(0);
while(!stude.eof())
{stude.getline(str[i],80); //reading from “stud.txt”
cout<<str[i]<<"\n\n"; i++;
}
getch(); }
OUTPUT
/* Program to search an element of an array using Binary search */
#include<iostream.h> // Header files declared
#include<process.h> #include<conio.h>
void main() { clrscr(); int A[50], n,p;
cout<<"Enter the Size of array : "; cin>>n;
cout<<"\n\nEnter the elements : \n\n"; for(int i=0; i<n; i++)
cin>>A[i];
cout<<"\n\nArray formed is : "; for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nEnter the element to be searched : "; cin>>p;
void binary_search(int A[], int n, int p); //declaration binary_search(A,n,p);
getch(); }
void binary_search(int A[], int n, int p) { int L,U,mid; char ch;
lb: L=0; U=n-1;
while(L<=U) //i.e loop will continue if L<=u. if L>U loop will end
{ mid=(L+U)/2; if(A[mid]==p)
{ cout<<"\n\nElement "<<p<<" found. Search Successful."; cout<<"\n\nSubscript = "<<mid<<" \n\nPosition = "<<mid+1; break; } else if(p<=A[mid]) U=mid-1; else L=mid+1;
}//end of while loop if(L>U)
{cout<<"\n\nUnsuccessful search.";
cout<<"\n\n\n\nWant to search again. : "; cin>>ch; if(ch=='y'||ch=='Y')
{cout<<"\n\n\n\nEnter the element to be searched : "; cin>>p; goto lb;} else exit(1); } }
OUTPUT
/* Wap in c++ to create a binary filebinary filebinary filebinary file and write objects of class BOOKclass BOOKclass BOOK to it and display class BOOK them on screen after reading from the file. */
#include<fstream.h> // Header files declared
#include<conio.h> int p; class book { int book_no; char book_title[80]; float price; float total_cost(int n) { float p; p=n*price; return p; } public : void input() {
cout<<"\n\nEnter Book no. : "; cin>>book_no; cout<<"\n\nEnter book title : ";
cin.get();
cin.getline(book_title,80);
cout<<"\n\nEnter book price : "; cin>>price; }
void purchase() {
cout<<"\n\nEnter number of copies to be purchased : "; cin>>p; cout<<"\n\nTotal cost (in Rs ) = "<<total_cost(p)<<"\n\n\n"; }
void display() {
cout<<"\n\nBook title is : "<<book_title; cout<<"\n\nBook number is : "<<book_no; cout<<"\n\nPrice of book is : "<<price;
cout<<"\n\nTotal cost of "<<p<<" books => "<<total_cost(p)<<" Rs\n\n\n\n"; } }; void main() {clrscr(); book b[3],p[3];
fstream bo;
cout<<"---Enter Details---\n"; for(int i=0; i<3; i++)
{
b[i].input(); b[i].purchase();
bo.open("xy.txt",ios::out|ios::binary); // Writing to the binary file
bo.write((char *)&b[i],sizeof(b[i])); bo.close();
bo.open("xy.txt",ios::in |ios::binary); //Reading from the binary file.
bo.read((char *)&p[i], sizeof(p[i])); bo.close();
}
cout<<"\n\n--- Book Details ---“; for(i=0; i<3; i++)
{
p[i].display(); }
getch(); }
/* Program to sort the given array in ascending order using bubble sort methodbubble sort methodbubble sort methodbubble sort method */ #include<iostream.h> // Header files declared
#include<conio.h> void main()
{clrscr(); int A[80],n;
cout<<"Enter desired size of array (<80): "; cin>>n; cout<<"\n\nEnter the array : \n";
for(int i=0; i<n; i++) cin>>A[i];
cout<<"\n\nArray of elements is as shown below : \n\n"; for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nNow Elements will be arranged in ascending order using bubble sort :\n\n"; void bubble_sort(int A[],int n);
bubble_sort(A,n); getch();
}
void bubble_sort (int A[], int n) { int temp; int count=0;
for(int i=0; i<n; i++) {
for(int j=0; j<n-1; j++) //Bubble sorting..
{ if(A[j+1]<A[j]) { count++; temp=A[j+1]; A[j+1]=A[j]; A[j]=temp;
cout<<"\n\nArray for iteration "<<count<<" is : \n\n"; for(int k=0; k<n; k++) cout<<A[k]<<" "; } } } }
/* WAP in c++ to perform basic operations on the two complex numbers complex numbers complex numbers complex numbers using structures
structures structures structures. */
#include<iostream.h> // Header files declared
#include<math.h> #include<conio.h> struct complex {
int i,r; }x,y;
void add(complex a, complex b) {
cout<<"\n\n Resultant of addition of complex numbers = "<<a.r+b.r<<" + ("<<a.i+b.i<<")i ";
}
void subtract(complex a,complex b) {int ch;
l:
cout<<"\n\n\t1. Subtract complex 2 from complex 1 : ";
cout<<"\n\n\t12.Subtract complex 1 from complex 2 : "; cout<<"\n\n\tEnter your choice : "; cin>>ch;
switch(ch) {
case 1 : cout<<"\n\nResultant of subtraction of complex number => "<<"("<<a.r-b.r<<")"<<" + ("<<a.i-b.i<<") i"; break;
case 2 : cout<<"\n\nResultant of subtraction of complex number => "<<"("<<b.r-a.r<<")"<<" + ("<<b.i-a.i<<") i"; break;
default : cout<<"\n\nPlease enter desired keyword. "; goto l; }
}
void multiply(complex a, complex b) {
cout<<"\n\nResultant of multiplication of complex number => "<<"("<<a.r*b.r-a.i*b.i<<")"<<" + ("<<a.r*b.i+a.i*b.r<<") i";
}
void divide(complex a,complex b) {int ch;
l: cout<<"\n\n\t1. Divide complex 2 from complex 1 : "; cout<<"\n\n\t2.Divide complex 1 from complex 2 : "; cout<<"\n\n\tEnter your choice : "; cin>>ch;
switch(ch) {
case 1 : cout<<"\n\nResultant of division of complex number => "<<((a.r*b.r)+(a.i*b.i))/(pow(b.r,2)+pow(b.i,2))
<<" + ("<<((a.i*b.r)-(a.r*b.i))/(pow(b.r,2)+pow(b.i,2))<<") i"; break;
case 2 : cout<<"\n\nResultant of division of complex number => "<<((b.r*a.r)+(b.i*a.i))/(pow(a.r,2)+pow(a.i,2))
<<" + ("<<((b.i*a.r)-(b.r*a.i))/(pow(a.r,2)+pow(a.i,2))<<") i"; break;
default : cout<<"\n\nPlease enter desired keyword. "; goto l; } } void main() { clrscr(); int choice; char ch; cout<<"Complex number 1 : ";
cout<<"\n\n\tEnter the real part : "; cin>>x.r; cout<<"\n\n\tEnter the imaginary part : "; cin>>x.i; cout<<"\n\n";
cout<<"Coplex number 1 => "<<x.r<<"+("<<x.i<<")i"; cout<<"\n\n\n\nComplex number 2 : ";
cout<<"\n\n\tEnter the real part : "; cin>>y.r; cout<<"\n\n\tEnter the imaginary part : "; cin>>y.i; cout<<"\n\n";
cout<<"\n\nComplex number 2 => "<<y.r<<"+("<<y.i<<")i"; do
{cout<<"\n\n\nChoose from the folowing : "; cout<<"\n\n1. Add two complex numbers ";
cout<<"\n\n2. Subtract two complex numbers "; //Main Menu
cout<<"\n\n3. Multiply two complex numbers "; cout<<"\n\n4. Divide two complex numbers "; cout<<"\n\nEnter your choice : "; cin>>choice; switch(choice)
{
case 1:add(x,y); break; case 2:subtract(x,y); break; case 3:multiply(x,y); break; case 4:divide(x,y); break; }
cout<<"\n\nWant to Choose again => "; cin>>ch;
}
while(ch=='y'||ch=='Y'); getch();
OUTPUT
OUTPUT
OUTPUT
OUTPUT
/* Program to search an element of an array using Linear searchLinear searchLinear searchLinear search methodmethodmethod */ method #include<iostream.h> // Header files declared
#include<conio.h> void main()
{ clrscr();
int A[50]; int n; int p; int subscript; /*Note: subscript and index are same.*/ cout<<"Enter the array size : ";
cin>>n; // n=size upto which user wants to insert values in array cout<<"\n\nEnter elements of array : \n";
for(int i=0; i<n; i++) cin>>A[i];
cout<<"\n\nThe array formed = "; for(i=0; i<n; i++)
cout<<A[i]<<" ";
cout<<"\n\nEnter the element to be searched : "; cin>>p; // p=element to be searched
void linear_search ( int A[], int n, int p); //function declaration linear_search(A,n,p);
getch(); }
void linear_search(int A[], int n, int p) { int count=0; int B[50]; int flag=0; for(int i=0; i<n; i++)
{ flag=count; if( A[i]==p)
{
B[count]=i;
count++; //Linear Search..
flag=count; }
}
if(flag==0)
cout<<"\n\nRequested element not found."; else
for(i=0; i<=n; i++) { if(A[i]==p)
{
cout<<"\n\nElement "<<p<<" is found";
cout<<"\n\nSubscript = "<<i<<"\n\nPosition = "<<i+1<<"\n\n\n"; }
} }
OUTPUT
/* Program to define the classes PERSON, GAME and STUDENT & to access the essential data using multiple inheritancemultiple inheritancemultiple inheritancemultiple inheritance.*/
#include<iostream.h> // Header files declared
#include<stdio.h> #include<conio.h>
class person{ char name[21]; int age;
public: void indata()
{cout<<"\n\nEnter the name of Student: " ; gets(name);
cout<<"\n\nEnter the age : "; cin>>age;
}
void outdata(); };
void person::outdata() // since the function contains loop so it is not made inline
{
cout<<"\n\n";
for(int i=0; i<79; i++) cout<<"-";
cout<<"\n\nName of the student is: "<<name; cout<<"\n\nAge of the student is : "<<age; } class game { char game_name[20]; public: void input() {
cout<<"\n\nEnter the game name : "; cin.get();cin.getline(game_name,20); }
void output() {
cout<<"\n\nGame opted by the student is : "<<game_name; }
};
class student: public person, public game { float Tmarks;
int rollno; public:
{if(Tmarks>90) return 'A'; else if(Tmarks>80&&Tmarks<=90) return 'B'; else if(Tmarks>70&&Tmarks<=80) return 'C'; else if(Tmarks>60&&Tmarks<=70) return 'D'; else return 'E'; } void enter() {
indata(); // indata() of class person called here
cout<<"\n\nEnter the roll number: "; cin>>rollno;
input(); // input() of class game called here
cout<<"\n\nEnter total marks (out of 100) : "; cin>>Tmarks;
}
void display() {
outdata();
cout<<"\n\nRoll number : "<<rollno; OUTPUT();
cout<<"\n\nTotal marks are : "<<Tmarks; cout<<"\n\nGrade = "<<calgrade(); } }; void main() { clrscr(); student A; A.enter(); A.display(); getch(); }
OUTPUT
/* Wap in c++ to implement stack as an array */stack as an array */stack as an array */ stack as an array */ #include<iostream.h>
#include<conio.h> // Header files declared
#include<process.h> int pop(int[],int&); int push(int[],int&,int); void display(int[],int); const int size=50; void main() { clrscr(); char m,ch; int k,stack[size],item,top=-1,res; do
{ cout<<"\nChoose from the following : \n\n" <<"\n 1. Push"
<<"\n 2. Pop"
<<"\n 3. Display" //Main Menu
<<"\n 4. Exit"
<<"\n\nEnter your choice : "; cin>>k; switch(k)
{
case 1: ch='y';
while(ch=='y'||ch=='Y')
{ cout<<"\nEnter the element : "; cin>>item;
res=push(stack,top,item); if(res==-1)
{cout<<"\nOverflow !!!!"; exit(1); }
cout<<"\nThe stack formed is : \n\n"; display(stack,top);
cout<<"\n\n\nWant to enter again ?: "; cin>>ch; } break; case 2: ch='y'; while(ch=='y'||ch=='Y') { res=pop(stack,top); if(res==-1) { cout<<"\nUnderflow !!!!"; exit(1); } else {
cout<<"\nThe deleted Element is : "<<res<<endl; cout<<"\nThe resultant stack is : \n\n";
display(stack,top); }
cout<<"\nWant to delete again ? : "; cin>>ch;
} break;
case 3: cout<<"\nThe resultant stack is : "; display(stack,top);
break; case 4: exit(0);
default: cout<<"\nPlease enter desired keyword : "; } // end of switch
cout<<"\n\nChoose from the menu again ? : "; cin>>m;
}while(m=='y'||m=='Y'); // end of do-while loop
getch();
} // end of main()
int push(int stack[],int &top,int el) { if(top==size-1) return -1; else { top++; stack[top]=el; 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) { cout<<stack[top]<<"<--"; for(int i=top-1;i>=0;i--) cout<<stack[i]<<"<--"; }
OUTPUT
/* Wap in C++ to implement queue as an array queue as an array queue as an array queue as an array */
#include<iostream.h> // Header files declared
#include<conio.h> #include<process.h> const int size=50;
int q[size],rear=-1,front=-1; int insert(int q[],int ele)
{ if(rear==size-1) return(-1); else if(rear==-1) { front=rear=0; q[rear]=ele; } else { rear++; q[rear]=ele; } return(0); } int delet(int q[]) { int r ; if(front==-1) return(-1); else { r=q[front]; if(rear==front) front=rear=-1; else front++; } return r; }
void display(int q[],int front,int rear) {
if(front==-1) return; else { for(int i=front;i<=rear;i++) cout<<q[i]<<" "; } } void main() { clrscr(); int n,u,k,m; char ch,ans; do {
cout<<"\nChoose from the menu :\n" <<"\n 1. Insert"
<<"\n 2. Delete" //Main Menu
<<"\n 3. Display"
<<"\n\n Enter your choice : "; cin>>n; switch(n)
{
case 1: ans='y';
while(ans=='y'||ans=='Y') {
cout<<"\n Enter element to be inserted :"; cin>>m; k=insert(q,m);
if(k==-1)
cout<<"\n Overflow !!!!"; cout<<"\n The resultant Queue is : "; display(q,front,rear);
cout<<"\n\n Want to enter again ?: "; cin>>ans; } break; case 2: ans='y'; while(ans=='y'||ans=='Y') { u=delet(q); if(u==-1) {
cout<<"\n Underflow !!!!"; break;
} else
{
cout<<"\n The deleted element is "<<u<<"\n"; cout<<"\n The resultant Queue is : \n\n"; display(q,front,rear);
}
cout<<"\n\n Want to delete again ?: "; cin>>ans;
} break;
case 3: cout<<"\n The Queue is : \n\n"; display(q,front,rear);
break;
default: cout<<"\n Please enter desired keyword : "; }
cout<<"\n Choose from the menu again ? : ";cin>>ch; }while(ch=='y'||ch=='Y');
getch(); }
/* Wap to implement stack as a linked liststack as a linked liststack as a linked liststack as a linked list */
#include<iostream.h> // Header files declared
#include<conio.h> #include<process.h> struct node { int roll; node* next; }*top,*save,*ptr,*newptr,*np; node *create(int a) {
ptr=new node; //To create a new node
ptr->roll=a; ptr->next=NULL; return ptr; } void push(node *np) { if(top==NULL) top=np; else { save=top; top=np; np->next=save; } } void pop() { if(top==NULL) cout<<"\n Underflow!!!!"; else { ptr=top; top=top->next; delete ptr; } } void display(node *np) { while(np!=NULL) { cout<<np->roll<<" -> "; np=np->next; }
} void main() { clrscr(); top=NULL; int n,m; char k,ch; do {
cout<<"\nChoose from the menu :\n" <<"\n 1. Push."
<<"\n 2. Pop."
<<"\n 3. Display." //Main Menu
<<"\n 4. Quit."
<<"\n\nEnter your choice : "; cin>>n; switch(n) { case 1: k='y'; while(k=='y'||k=='Y') {
cout<<"\n Enter element to be inserted ."; cin>>m;
newptr=create(m); if(newptr==NULL)
cout<<"\n Cannot create !!!!"; push(newptr);
cout<<"\n The Stack formed is : "; display(top);
cout<<"\n\n Want to enter again ?: "; cin>>k; } break; case 2: k='y'; while(k=='y'||k=='Y') { pop();
cout<<"\n The Stack formed is : \n\n"; display(top);
cout<<"\n\n Want to delete again ?: "; cin>>k;
} break;
case 3: cout<<"\n The Stack formed is : "; display(top);
break; case 4: exit(0);
break;
default: cout<<"\n Please enter desired keyword : "; }
cout<<"\n Do you want to continue..? : "; cin>>ch;
}while(ch=='y'||ch=='Y'); getch();
}
/* Wap in c++ to implement Queue as a linked listQueue as a linked listQueue as a linked listQueue as a linked list */
#include<iostream.h> // Header files declared
#include<conio.h> struct node { int roll; node* next; }*front,*rear,*ptr,*newptr,*np; node *create(int a) {
ptr=new node; //To create a new node
ptr->roll=a; ptr->next=NULL; return ptr; } void insert(node *np) { if(front==NULL) front=rear=np; else { rear->next=np; rear=np; } } void delet()
{ if(front==NULL) cout<<"\n Underflow!!!!"; else { ptr=front; front=front->next; delete ptr; } } void display(node *np) { while(np!=NULL) { cout<<np->roll<<"-> "; np=np->next; } } void main() { clrscr(); front=rear=NULL; int n,m; char ans,ch; do
{ cout<<"\nChoose from the menu : "
<<"\n 1) Insert." //Main Menu
<<"\n 3) Display."
<<"\n\n Enter your choice : "; cin>>n; switch(n) { case 1: ans='y'; while(ans=='y'||ans=='Y') {
cout<<"\n Enter element to be inserted ."; cin>>m;
newptr=create(m); if(newptr==NULL)
cout<<"\n Cannot create !!!!"; insert(newptr);
cout<<"\n The Queue formed is : "; display(front);
cout<<"\n Want to enter more nodes ?: "; cin>>ans; } break; case 2: ans='y'; while(ans=='y'||ans=='Y') {
delet();
cout<<"\n Queue : "; display(front);
cout<<"\n Want to delete more ?: "; cin>>ans;
} break;
case 3: cout<<"\n Queue : "; display(front);
break;
default: cout<<"\n You entered wrong choice..."; }
cout<<"\n Want to return to main menu ? : "; cin>>ch; }while(ch=='y'||ch=='Y'); getch(); }
OUTPUT
/* Wap in C++ to create a linked list of integerscreate a linked list of integerscreate a linked list of integers and perform some basic operation on it. create a linked list of integers */
#include<iostream.h> // Header files declared
#include<process.h> #include<conio.h> struct node { int info; node*next; }*rear,*start,*newptr,*save,*ptr; void delnode() { if(start==NULL) cout<<"underflow!!"; else { ptr=start; start=start->next; delete ptr;
cout<<"\n\nFirst node deleted"; }
}
void insert_beg(node*np) {
//To insert at the beginning
if(start==NULL) start=np; else { save=start; start=np; np->next=save; } } void display(node*np) { while(np!=NULL) { cout<<np->info<<"->"; np=np->next; } cout<<"\n"; }
node*create_new_node(int n) { ptr=new node; ptr->info=n; ptr->next=NULL; return ptr; } void insert_end(node*np) { if(start==NULL) start=rear=np; else { rear->next=np; rear=np; } } void search(node*np,int a) { int flag=0; while(np!=NULL) { if(np->info==a) { flag=1; break; } else np=np->next; } if(flag) cout<<a<<"\n\nFound"; else cout<<"\n\nNot found"; } void main() { clrscr(); start=rear=NULL; char ans='y',choice; int ch,info,n;
do //Main Menu
{
cout<<"\n\nChoose from the menu :\n1.Insert at the end\n2.Insert at beginning\n3.Searching\n4.Deletion from begining \n5.Display \n"; cout<<"\n\nEnter your choice";
cin>>ch; switch(ch) {
case 1: do {
cout<<"\n\nEnter information for the new node : "; cin>>info;
newptr=create_new_node(info); if(newptr!=NULL)
cout<<"\n\nNode has been created"; else
{
cout<<"\n\nNode cannot be created;"; exit(1);
}
insert_end(newptr);
cout<<"\n\nThe Linked list is : "; display(start);
cout<<"\n\nWant to insert again : "; cin>>choice;
}while(choice=='y'||choice=='Y'); break;
case 2: do {
cout<<"\n\nEnter info for new node: "; cin>>info;
newptr=create_new_node(info); if(newptr!=NULL)
cout<<"\n\nNode has been created "; else
{
cout<<"\n\nNode cannot be created "; exit(1);
}
insert_beg(newptr);
cout<<"\n\nThe Linked list is : "; display(start);
cout<<"\n\nWant to insert again : "; cin>>choice;
break; case 3: do
{
cout<<"\n\nEnter info to be searched : "; cin>>n;
search(start,n);
cout<<"\n\nWant to search again : "; cin>>choice; }while(choice=='y'||choice=='Y'); break; case 4: delnode(); break; case 5: display(start); break; }// end of do switch
cout<<"\n\nWant to choose again : "; cin>>ans;
}
while(ans=='y'||ans=='Y'); getch();
/* Wap in C++ to read file “sports.dat”“sports.dat”“sports.dat” and copy only those records where event name is “sports.dat” ATHLETIC
ATHLETIC ATHLETIC
ATHLETIC using the concept of Data file handlingData file handlingData file handlingData file handling */ #include<fstream.h>
#include<string.h> // Header files declared
#include<conio.h> #include<stdio.h> #include<stdlib.h> struct sports { char event[20]; char participants[10][30]; int no_of_participants; } s[20], s2[20];
void copy(fstream &ob); int i=0; void main() { clrscr(); char choice; fstream ob("sports.dat",ios::binary|ios::in|ios::out); do { if(i>0) cin.get();
cout<<"\n\nEnter the name of Event : "; cin.getline(s[i].event,20);
cout<<"\n\nEnter the total number of participants in Event "<<s[i].event<<" : "; cin>>s[i].no_of_participants;
cout<<"\n\nEnter the name of Praticipants : \n"; cin.get();
for(int j=0; j<s[i].no_of_participants; j++) cin.getline(s[i].participants[j],30);
ob.write((char*)&s[i], sizeof(sports)); //To write into the file
cout<<"\n\n\nWant to Enter Details of Another Event (Y/N) : "; cin>>choice; i++; } while(choice=='y'||choice=='Y'); cout<<"\n\n\n\n\n****************************************************************************** ******************************\n\n"; copy(ob); cout<<"\n\n************************************************************************************ *****************************\n\n\n"; getch(); }
void copy(fstream &o) { sports s[20]; o.seekg(0); ofstream file; file.open("athletic.dat",ios::binary); file.seekp(0); int j,n=0; int c=0;
while(o) { o.read((char*)&s[c], sizeof(sports)); if(strcmp("athletic",s[c].event)==0) { file.write((char*)&s[c], sizeof(sports)); n=1; break; } c++; } if(n==0) { cout<<"\n\nUnsuccessful Search."; getch(); exit(0); } o.close(); file.close(); sports sp; ifstream oo; oo.open("athletic.dat",ios::binary); while(oo) { oo.read((char*)&sp, sizeof(sports));
}
cout<<"\n\nThe Records of file are : \n\n"; cout<<"\n\nEvent = "<<sp.event;
cout<<"\n\n\n\nThe Participants are : \n\n"; for(int i=0; i<sp.no_of_participants; i++) { cout<<sp.participants[i]<<"\n\n"; } oo.close(); remove("athletic.dat"); remove("sports.dat"); }
OUTPUT
/* Wap to print and find the sum of Fibonacci series Fibonacci series Fibonacci series Fibonacci series using recursion.recursion.recursion. */ recursion. #include<iostream.h> // Header files declared
#include<conio.h> int fibonacci(int n); void main() { clrscr(); int n;
cout<<"\n\n Enter the number of terms upto which you want the sum of fibonnaci series : ";
cin>>n;
cout<<"\n\nThe fibonacci series generated is : \n\n"; cout<<"1 1 ";
int s=fibonacci(n);
cout<<"\n\nThe sum of fibonacci series for "<<n<<" terms = "<<s; getch(); } int first=1; int second=1; int third; int i=2; int sum=0; int fibonacci(int n) { if(n==1)
sum=1; else if(n==2) sum=2;
// n = 1 2 3 4 5 else if(n>1 && i!=n) // num = 1 1 2 3 5 { third=first+second; cout<<third<<” “; if(i==2) sum+=first+second+third; else sum+=third; first=second; second=third; ++i; fibonacci(n); } return sum; }
/* Wap in C++ to print and find the sum of even /odd numbers using recursionrecursionrecursionrecursion.*/ #include<iostream.h> // Header files declared
#include<conio.h> int sum_even(int ); int sum_odd(int ); int sum=0; int num=0; void main() { clrscr(); int n;
int s_e, s_o;
cout<<"\n\nEnter the number upto which you want the sum of even/odd numbers : "; cin>>n;
cout<<"\n\n The list of integers is : \n\n"; for(int l=1; l<=n; l++)
cout<<l<<"\n"; s_e=sum_even(n); s_o=sum_odd(n);
cout<<"\n\nThe sum of even numbers upto "<<n<<" = "<<s_e; cout<<"\n\nThe sum of odd numbers upto "<<n<<" = "<<s_o; getch();
int sum_even(int n) {
if(num%2==0) sum=sum+num; if(num!=n && num<n) { ++num; sum_even(n); } return sum; } int sum_2=0; int num_2=0; int sum_odd(int n) { if(num_2%2!=0) sum_2=sum_2+num_2; if(num_2!=n) { num_2++; sum_odd(n); } return sum_2; }
/* Wap in c++ using pointers to find the smallest smallest smallest smallest and the largest largest largest element in a largest dynamically created array
dynamically created array dynamically created array dynamically created array.*/
#include<iostream.h> // Header files declared
#include<conio.h> void main()
{
clrscr();
int *array, smallest, largest, n;
cout<<"\n\nEnter the number of elements : "; cin>>n;
array=new[n];
cout<<"\n\nEnter the elements : \n\n"; for(int i=0; i<n; i++)
cin>>array[i]; i=0;
cout<<"\n\nThe array formed is : \n\n"; while(i!=n) { cout<<array[i]<<" "; i++; } smallest=array[0];
for(i=0; i<n; i++) {
if(array[i]<=smallest)
smallest=array[i]; //Comparing the elements
}
largest=array[0]; for(i=0; i<n; i++) {
if(array[i]>=largest) largest=array[i]; }
cout<<"\n\nThe smallest element is : "<<smallest<<"\n\nThe largest element is : "<<largest;
getch(); }
/* Wap using pointersusing pointersusing pointersusing pointers to swapswapswapswap two integers. */
#include<iostream.h> // Header files declared
#include<conio.h>
void swap_using_pointers(int *, int *); void main()
{
clrscr(); int a,b;
cout<<"\n\nEnter first integer : "; cin>>a; cout<<"\n\nEnter second integer : "; cin>>b; swap_using_pointers(&a,&b); cout<<"\n\nNow value of first integer = "<<a; cout<<"\n\nNow value of second integer = "<<b; getch();
}
void swap_using_pointers(int *a,int *b) {
int temp; temp=*a; *a=*b;
*b=temp; }
OUTPUT
/* Wap using pointerspointerspointers to find the lengthpointers find the lengthfind the lengthfind the length of a string and print the reversed stringreversed stringreversed string . */ reversed string #include<iostream.h>
#include<string.h> // Header files declared
#include<conio.h> void main()
{
clrscr();
char *str= new char[256]; cout<<"\n\nEnter the string : "; cin.getline(str,256);
cout<<"\n\nThe string length => "<<strlen(str); cout<<"\n\nThe reverse string is : ";
char intermediate;
for(int j=strlen(str)-1,i=0; i<=strlen(str)/2; j--,i++) {
intermediate = *(str+j);
//Loop to reverse the string
*(str+j)=*(str+i); *(str+i)=intermediate; }
cout<<str; getch();
}
/*Program to sort an array in ascending order using Selection sort Selection sort Selection sort Selection sort */ #include<iostream.h>
#include<conio.h> // Header files declared
void main() {clrscr(); int A[80],n;
lb: cout<<"Enter the size of array : "; cin>>n;
if(n<=0||n>80)
{cout<<"\n\nEnter size of array less than or equal to 80."; goto lb;
}
cout<<"\n\nEnter the elements of array : \n\n"; for(int i=0; i<n; i++)
cin>>A[i];
void selection_sort(int A[],int n); selection_sort(A,n);
cout<<"\n\n\n\n\nSorted array is : \n\n"; for(i=0; i<n; i++)
cout<<A[i]<<" "; getch();
}
void selection_sort(int A[], int n) // Function to sort using selection sort
{int small; int k,count=0; for(int i=0; i<n; i++) { small=A[i]; count++; for(int j=i+1; j<n; j++) { if(A[j]<small) {small=A[j]; A[j]=A[i]; A[i]=small; } }
cout<<"\n\nArray after iteration "<<count<<" is :\n\n"; for(k=0; k<n; k++)
cout<<A[k]<<" "; }
OUTPUT
/* WapWapWap to create a text file to input roll no. and marks of ten students and display them Wap on screen after reading from the text file using data file handlingusing data file handlingusing data file handlingusing data file handling. */
#include<fstream.h> // Header files declared
#include<conio.h> void main()
{
clrscr();
int a[10]; float b[10]; for(int i=0; i<10; i++) {
cout<<"\n\nEnter the roll no. of student "<<i+1<<" : "; cin>>a[i]; cout<<"\n\tEnter the marks of student "<<i+1<<" => "; cin>>b[i]; }
ofstream student;
student.open("stud.txt"); for(i=0; i<10; i++)
{
student<<"\n\nMarks of roll no. "<<a[i]<<" => "<<b[i]; }
student.close(); i=0;
cout<<”\n\nStudent details are :\n\n”; char str[80][80];
ifstream stude;
stude.open("stud.txt",ios::in);
stude.seekg(0); // Pointing to beginning of the text
while(!stude.eof()) {stude.getline(str[i],80); cout<<str[i]<<"\n\n"; i++; } getch(); }
/* WAP USING FUNCTION OVERLOADING TO CALCULATE A^B */ #include<iostream.h> // Header files declared
#include<math.h> #include<conio.h> int calc(int a, int b) {
return pow(a,b); }
float calc(float a, float b) {
return pow(a,b); }
float calc(int a, float b) {
return pow(a,b); }
float calc(float a, int b) { return pow(a,b); } void main() {clrscr(); int P,Q; float R,S;
cout<<"Enter int P : "; cin>>P; cout<<"Enter int Q : "; cin>>Q; cout<<"Enter real R : "; cin>>R; cout<<"Enter real S : "; cin>>S;
cout<<P<<"^"<<Q<<" = "<<pow(P,Q)<<"\n\n"; // Using ‘pow’ from math.h
cout<<P<<"^"<<R<<" = "<<pow(P,R)<<"\n\n"; cout<<R<<"^"<<Q<<" = "<<pow(R,Q)<<"\n\n"; cout<<R<<"^"<<S<<" = "<<pow(R,S);
getch(); }
/* WAP USING FUNCTION OVERLOADING TO CALCULATE AREA OF CIRCLE, SQUARE, RECTANGLE, RIGHT TRIANGLE AND TRIANGLE USING HERON’S FORMULAE */ #include<iostream.h>
#include<math.h> // Header files declared
#include<conio.h> float area(float p) { return 3.14*p*p; } float area(int p) { return p*p;
} // Function overloading of ‘area’
float area(float p,float q) {
return p*q; }
float area(int p, int q) {
return (0.5*p*q); }
float area(float p,float q, float r) { float s; s=(p+q+r)/2; return sqrt(s*(s-p)*(s-q)*(s-r)); } void main() {clrscr(); int p,q,ch; float a,b,r; char choice; do{
cout<<"\n\nChoose from the following : "; cout<<"\n\n1. Area of square ";
cout<<"\n\n2. Area of circle "; // Main Menu
cout<<"\n\n3. Area of rectangle "; cout<<"\n\n4. Area of right triangle "; cout<<"\n\n5. Area of triangle ";
cout<<"\n\nEnter your choice : "; cin>>ch; switch(ch)
{
case 1: cout<<"\n\nEnter side of square : "; cin>>p;
cout<<"\n\narea of square is : "<<area(p); break;
case 2: cout<<"\n\nEnter radius of circle : "; cin>>a;
cout<<"\n\narea of circle is : "<<area(a); break;
case 3: cout<<"\n\nEnter length of rectangle : "; cin>>a;
cout<<"\n\nEnter breadth of rectangle : "; cin>>b;
cout<<"\n\narea of rectangle is : "<<area(a,b); break;
case 4: cout<<"\n\nEnter base and altitude of right triangle : "; cin>>p;
cin>>q;
cout<<"\n\narea of right triangle is : "<<area(q,p); break;
case 5: cout<<"\n\nEnter sides of triangle : "; cin>>a;
cin>>b; cin>>r;
cout<<"\n\narea of triangle is : "<<area(a,b,r); break;
}
cout<<"\n\nWant to choose again : "; cin>>choice;
}while(choice=='y'||choice=='Y'); getch();
/* WAP USING MULTIPLE INHERITANCE FOR THE CLASSES STUDENT, GAME AND PERSON */
#include<iostream.h> // Header files declared
#include<stdio.h> #include<conio.h>
class person{ char name[21]; int age;
public: void indata()
{cout<<"\n\nEnter the name of Student: " ; gets(name);
cout<<"\n\nEnter the age : "; cin>>age; } void outdata(); }; void person::outdata() { cout<<"\n\n"; for(int i=0; i<79; i++) cout<<"-";
cout<<"\n\nName of the student is: "<<name; cout<<"\n\nAge of the student is : "<<age; } class game { char game_name[20]; public: void input() {
cout<<"\n\nEnter the game name : "; cin.get();cin.getline(game_name,20); }
void output() {
cout<<"\n\nGame opted by the student is : "<<game_name; }
};
class student: public person, public game
{ float Tmarks; // Multiple inheritance
int rollno; public: char calgrade() {if(Tmarks>90) return 'A'; else if(Tmarks>80&&Tmarks<=90)
return 'B'; else if(Tmarks>70&&Tmarks<=80) return 'C'; else if(Tmarks>60&&Tmarks<=70) return 'D'; else return 'E'; } void enter() { indata();
cout<<"\n\nEnter the roll number: "; cin>>rollno; input();
cout<<"\n\nEnter total marks (out of 100) : "; cin>>Tmarks;
}
void display() {
outdata();
cout<<"\n\nRoll number : "<<rollno; OUTPUT();
cout<<"\n\nTotal marks are : "<<Tmarks; cout<<"\n\nGrade = "<<calgrade(); } }; void main() { clrscr(); student A; A.enter();
cout<<”\n\nstudent details are : \n\n”; A.display();
getch(); }
SQL
sql .txt mysql> use shlok;
Database changed
mysql> CREATE TABLE CUSTOMERS(
-> ID INT NOT NULL, -> NAME VARCHAR (20) NOT NULL, -> AGE INT NOT NULL, -> ADDRESS CHAR (25) ,
-> SALARY DECIMAL (18, 2), -> PRIMARY KEY (ID)
-> );
Query OK, 0 rows affected (0.31 sec)
mysql> INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) -> VALUES (1, 'Ramesh', 32, 'Ahmedabad', 2000.00 ); Query OK, 1 row affected (0.06 sec)
mysql> INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) -> VALUES (2, 'Khilan', 25, 'Delhi', 1500.00 );
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) -> VALUES (3, 'kaushik', 23, 'Kota', 2000.00 );
Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) -> VALUES (4, 'Chaitali', 25, 'Mumbai', 6500.00 ); Query OK, 1 row affected (0.09 sec)
mysql> INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) -> VALUES (5, 'Hardik', 27, 'Bhopal', 8500.00 ); Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) -> VALUES (6, 'Komal', 22, 'MP', 4500.00 );
Query OK, 1 row affected (0.06 sec)
mysql> SELECT ID, NAME, SALARY FROM CUSTOMERS; +----+---+---+ | ID | NAME | SALARY | +----+---+---+ | 1 | Ramesh | 2000.00 | | 2 | Khilan | 1500.00 | | 3 | kaushik | 2000.00 | | 4 | Chaitali | 6500.00 | Page 1
sql .txt | 5 | Hardik | 8500.00 |
| 6 | Komal | 4500.00 | +----+---+---+ 6 rows in set (0.00 sec)
mysql> SELECT * FROM CUSTOMERS;
+----+---+---+---+---+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---+---+---+---+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 6 | Komal | 22 | MP | 4500.00 | +----+---+---+---+---+ 6 rows in set (0.00 sec)
mysql> SELECT ID, NAME, SALARY -> FROM CUSTOMERS -> WHERE SALARY > 2000; +----+---+---+ | ID | NAME | SALARY | +----+---+---+ | 4 | Chaitali | 6500.00 | | 5 | Hardik | 8500.00 | | 6 | Komal | 4500.00 | +----+---+---+ 3 rows in set (0.00 sec)
mysql> SELECT ID, NAME, SALARY -> FROM CUSTOMERS
-> WHERE SALARY > 2000 AND age < 25; +----+---+---+
| ID | NAME | SALARY | +----+---+---+ | 6 | Komal | 4500.00 | +----+---+---+ 1 row in set (0.00 sec)
mysql> UPDATE CUSTOMERS -> SET ADDRESS = 'Pune' -> WHERE ID = 6;
Query OK, 1 row affected (0.05 sec) Rows matched: 1 Changed: 1 Warnings: 0
sql .txt
mysql> DELETE FROM CUSTOMERS -> WHERE ID = 6;
Query OK, 1 row affected (0.04 sec)
mysql> SELECT * FROM CUSTOMERS -> ORDER BY NAME, SALARY;
+----+---+---+---+---+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---+---+---+---+ | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | | 3 | kaushik | 23 | Kota | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 | | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | +----+---+---+---+---+ 5 rows in set (0.00 sec)
mysql> ALTER TABLE CUSTOMERS ADD SEX char(1); Query OK, 0 rows affected (0.53 sec)
Records: 0 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM CUSTOMERS;
+----+---+---+---+---+---+ | ID | NAME | AGE | ADDRESS | SALARY | SEX | +----+---+---+---+---+---+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | NULL | | 2 | Khilan | 25 | Delhi | 1500.00 | NULL | | 3 | kaushik | 23 | Kota | 2000.00 | NULL | | 4 | Chaitali | 25 | Mumbai | 6500.00 | NULL | | 5 | Hardik | 27 | Bhopal | 8500.00 | NULL | +----+---+---+---+---+---+ 5 rows in set (0.00 sec)
mysql> ALTER TABLE CUSTOMERS DROP SEX; Query OK, 0 rows affected (0.69 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> SELECT * FROM CUSTOMERS;
+----+---+---+---+---+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---+---+---+---+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 2 | Khilan | 25 | Delhi | 1500.00 |
sql .txt | 3 | kaushik | 23 | Kota | 2000.00 | | 4 | Chaitali | 25 | Mumbai | 6500.00 | | 5 | Hardik | 27 | Bhopal | 8500.00 | +----+---+---+---+---+ 5 rows in set (0.00 sec)
mysql> SELECT ID, NAME, AGE, ADDRESS, SALARY -> FROM CUSTOMERS
-> GROUP BY age
-> HAVING COUNT(age) >= 2;
+----+---+---+---+---+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---+---+---+---+ | 2 | Khilan | 25 | Delhi | 1500.00 | +----+---+---+---+---+ 1 row in set (0.00 sec)
mysql> SELECT * FROM CUSTOMERS -> WHERE SALARY LIKE '200%';
+----+---+---+---+---+ | ID | NAME | AGE | ADDRESS | SALARY | +----+---+---+---+---+ | 1 | Ramesh | 32 | Ahmedabad | 2000.00 | | 3 | kaushik | 23 | Kota | 2000.00 | +----+---+---+---+---+ 2 rows in set (0.00 sec)
mysql> SELECT SALARY FROM CUSTOMERS -> ORDER BY SALARY; +---+ | SALARY | +---+ | 1500.00 | | 2000.00 | | 2000.00 | | 6500.00 | | 8500.00 | +---+
5 rows in set (0.00 sec)
mysql> DROP TABLE CUSTOMERS;
Query OK, 0 rows affected (0.16 sec)