object - kendriya vidyalaya...object make a c++ program to read a one-dimensional integer array,...
TRANSCRIPT
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[1]
Page-1
DATE :
OBJECT � To make a C++ menu driven program to perform following
operation on string without using built in functions. 1. Find the length of the string, 2. Compare two string, 3. Concatenate two strings.
SOURCE CODE �
#include<iostream.h> #include<conio.h>
#include<stdio.h> #include<process.h>
void findlength()
{ char str[30]; int i=0;
cout<<"\n Enter the string (size<=30) "; gets(str);
while(str[i]!='\0') { i++; }
cout<<"\n Length Of the given String is: "<<i<<endl; }
void compare() { char str1[30], str2[30];
int i1=0,i2=0,i=0,flag=0; cout<<"\n Enter the string1 (size<=30) "; gets(str1);
while(str1[i1]!='\0') { i1++; }
cout<<"\n Enter the string2 (size<=30) "; gets(str2);
while(str2[i2]!='\0') { i2++; }
if(i2!=i1) { cout<<"\n Strings are not Equal "; }
else { for(i=0;i<i1;i++)
{ if(str1[i]!=str2[i]) { flag=1;
break; }
} if(flag==1)
{ cout<<"\n Strings are not Equal "; } else
{ cout<<"\n Strings are Equal "; }
} }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[2]
Page-2
void concat()
{ char str1[30], str2[30]; int i1=0,i2=0,i=0,flag=0;
cout<<"\n Enter the string1 (size<=30) "; gets(str1); while(str1[i1]!='\0')
{ i1++; } cout<<"\n Enter the string2 (size<=30) "; gets(str2);
while(str2[i2]!='\0') { i2++; }
for(i=0;i<i2;i++) { str1[i1+i]=str2[i]; }
str1[i1+i2]='\0'; cout<<"\n The concatenated String is: "; puts(str1);
} void main()
{ G:
clrscr(); cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n"; cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~\n";
cout<<"Enter your choice \n \t1.Find length of string\n\t" "2.Compare two Strings \n\t3.Concatenate two strings\n\n";
char ch, opt; cout<<"\t\t My Choice is..."; cin>>ch;
cout<<"\n__________________________________________________\n"; if(ch=='1') { findlength(); }
else if(ch=='2') { compare(); } else if(ch=='3') { concat(); }
cout<<"\n__________________________________________________\n"; cout<<"\n\tDo you want to do again (y/n)..."; cin>>opt;
if((opt=='y')||(opt=='Y'))
goto G; else exit ; }
OUTPUT �
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[3]
Page-3
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[4]
Page-4
DATE :
OBJECT � Create an array containing country and capital. Now write a menu driven program to:
� Display capital of given country � Print the tabular report of the country and corresponding
capital
SOURCE CODE �
#include<iostream.h> #include<stdio.h>
#include<conio.h> #include<string.h>
#include<process.h> struct world
{ char country[30]; char capital[30];
}; void main()
{ clrscr();
world w[10]; int i=0, n;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t A Creation of: A. NASRA\n";
cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~\n"; cout<<"\tEnter no. of records (<=10): ";
cin>>n; for(i=0;i<n;i++)
{ cout<<"\tEnter the country: "; gets(w[i].country);
cout<<"\tEnter the capital: "; gets(w[i].capital);
}
G:
clrscr(); char ch,*cont;
int flag;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t A Creation of: A. NASRA\n";
cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~\n"; cout<<"\n\t1.Search for capital\n\t2.List all Records\n";
cout<<"\n\t My Choice is ... " ; cin>>ch;
{ if(ch=='1') {
cout<<"\n_______________________________________\n"; cout<<"\n\tEnter the country : ";
gets(cont);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[5]
Page-5
flag=0;
for(i=0;i<n;i++) { if(strcmp(cont,w[i].country)==0)
{ cout<<"\n\t capital is: "<<w[i].capital; flag=1;
break; }
} if(flag==0)
cout<<"\n\tcountry not found"; cout<<"\n________________________________________________\n";
cout<<"\n\tDo you want to do again (y/n)...";cin>>ch ; if((ch=='y')||(ch=='Y'))
goto G; else exit(0) ; }
else if(ch=='2')
{ cout<<"\n_______________________________________________\n"; cout<<"\n\tCOUNTRY\t\tCAPITAL";
cout<<"\n\t-------\t\t-------"; for(i=0;i<n;i++)
cout<<"\n\t"<<(w[i].country)<<"\t\t"<<(w[i].capital); cout<<"\n\t-------\t\t-------";
cout<<"\n_______________________________________________\n"; cout<<"\n\tDo you want to do again (y/n)...";cin>>ch ;
if((ch=='y')||(ch=='Y')) goto G; else exit(0) ;
} }
}
OUTPUT �
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[6]
Page-6
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The name of arry must be logical.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming. 5. The string functions must be defined globally.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[7]
Page-7
DATE :
OBJECT � Make a C++ program to declare arrays for : name, roll, m1, m2, m3, total for N students and print the tabular report and report
card of individual student according to the users choice.
SOURCE CODE �
#include<iostream.h> #include<conio.h>
#include<stdio.h> #include<string.h>
#include<process.h> void main()
{ clrscr(); char *name[10],*nm;
int rno[50],m1[50],m2[50],m3[50],tot[50],i,n,trn; cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n"; cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~\n";
cout<<"Enter no. of records: "; cin>>n; cout<<"\n";
for(i=0;i<n;i++) { cout<<"Enter Name of Student-"<<(i+1)<<"\t: "; gets(name[i]);
cout<<"Enter Roll No. of "<<name[i]<<"\t: "; cin>>rno[i]; cout<<"Enter Mark1 of "<<name[i]<<"\t: "; cin>>(m1[i]);
cout<<"Enter Mark2 of "<<name[i]<<"\t: "; cin>>(m2[i]); cout<<"Enter Mark3 of "<<name[i]<<"\t: "; cin>>(m3[i]);
tot[i]=m1[i]+m2[i]+m3[i]; cout<<"\n"; }
G: clrscr();
char ch,ch1,opt; int flag=0; cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n"; cout<<"\~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~~\n";
cout<<"\t1.Report card for particular student"
"\n\t2.List all records\n"; cout<<"\n\tMy Choice is - ";cin>>ch;
cout<<"\n__________________________________________________\n"; { if(ch=='1')
{ cout<<"\nEnter the Roll no."; cin>>trn; flag=0;
for(i=0;i<n;i++) if(trn==rno[i])
{ cout<<"\n\t\tName : "<<name[i]; cout<<"\n\t\tRoll No.: "<<rno[i];
cout<<"\n\t\tMark1 : "<<m1[i]; cout<<"\n\t\tMark2 : "<<m2[i];
cout<<"\n\t\tMark3 : "<<m3[i];
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[8]
Page-8
cout<<"\n\t____________________________\n";
cout<<"\n\t\tTotal : "<<tot[i]; cout<<"\n\t____________________________";
flag=1; }
if(flag==0) cout<<"\n\tRecord not found !";
} else if(ch=='2')
{ cout<<"\n\tRollno\tName \tMark1\tMark2\tMark3\tTotal"; cout<<"\n\t------\t-----\t-----\t-----\t-----\t-------";
for(i=0;i<n;i++) { cout<<"\n\t"<<rno[i]<<"\t"<<name[i];
cout<<"\t"<<m1[i]<<"\t"<<m2[i]<<"\t"<<m3[i]<<"\t"<<tot[i]; }
}
} cout<<"\n__________________________________________________\n";
cout<<"\n\tDo you want to do again (y/n)..."; cin>>opt;
if((opt=='y')||(opt=='Y')) {goto G ;} else exit(0); }
OUTPUT �
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[9]
Page-9
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. Pretty printing must be kept in mind while writing the source code.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully and terminators must not be forgetten.
4. Semantic errors must be avoided by doing thoughtful logical programming. 5. The string functions must be defined globally.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[10]
Page-10
DATE :
OBJECT � Write a function in C++ which accepts a 2D array of integers and its size as arguments and displays elements which are exactly two
digit number.
SOURCE CODE �
#include<iostream.h> #include<conio.h>
#include<process.h> void twodigit(int A[10][10],int m,int n)
{ int i,j,flag=0; cout<<"\n\tThe Two digit Numbers are: ";
for(i=0;i<m;i++) for(j=0;j<n;j++)
{ if(A[i][j]>=10&&A[i][j]<=99) { cout<<A[i][j]<<ends;
flag=1; }
}
if(flag==0) cout<<"\tNone";
} void main()
{ G: clrscr(); char opt;
int A[10][10],i,j,m,n; textcolor(GREEN);
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t";cprintf("A Creation of: A. NASRA");cout<<"\n";
cout<<"\~~~~~~~~~~~-------------------------~~~~~~~~~~~~~~\n"; cout<<"\n\tEnterNo. of rows : "; cin>>m;
cout<<"\n\tEnter the no. of columns: "; cin>>n; cout<<"\n\tEnter the Elements of the array...\n";
for(i=0;i<m;i++)
{ for(j=0;j<n;j++) { cout<<"\n\t\tA["<<i<<"]["<<j<<"] = ";
cin>>A[i][j]; }
} cout<<"\n___________________________________________________\n";
twodigit(A,m,n); cout<<"\n___________________________________________________\n";
cout<<"\n\tDo you want to do again (y/n)..."; cin>>opt;
if((opt=='y')||(opt=='Y')) { goto G; } else exit ; getch();
}
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[11]
Page-11
OUTPUT �
POSSIBLE ERRORS AND PRECAUTIONS ����
1. The string functions must be defined globally. 2. Comments must be given at appropriate places, so that program may be
refined and developed afterwards in future. 3. In a big program, there may be typing errors. So typing must be done
carefully. 4. Semantic errors must be avoided by doing thoughtful logical programming.
5. The complete program must be tested many times with various variable so
the correct function and errors may be detected and corrected.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[12]
Page-12
DATE :
OBJECT � Make a C++ program to read a one-dimensional integer array, then create a menu to :
A. Sort the array, using bubble sort, B. Search an element of the array using the binary search.
SOURCE CODE ���� #include<iostream.h>
#include<conio.h> #include<process.h>
void main() { clrscr();
int A[20],n,i,j,k,temp,el; char ch,opt;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t A Creation of: A. NASRA\n";
cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~\n"; cout<<"\n\tEnter no. of Elements (<=20) in 1-D Array : "; cin>>n;
cout<<"\n\tNow enter "<<n<<" elements in the array...\n";
for(i=0;i<n;i++) { cout<<"\n\t\t\tA["<<i<<"] = "; cin>>A[i]; }
G: clrscr();
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t A Creation of: A. NASRA\n";
cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~\n"; cout<<"\n\tEnter your choice...\n";
cout<<"\n\t1.Do you want to Sort Array? OR\n\t2.Do you want to Search an element?\n";
cout<<"\n\tMy choice is : "; cin>>ch; switch(ch)
{ case '1': for(i=0;i<n;i++)
{ for(j=0;j<n-1;j++)
if(A[j]>A[j+1]) {
temp=A[j]; A[j]=A[j+1];
A[j+1]=temp; }
} cout<<"\n\tThe Sorted Array is...\n";
for(k=0;k<n;k++) { cout<<"\n\t\tA["<<k<<"] = "<<A[k];}
break; case '2':
cout<<"\n\tEnter the element to be searched : "; cin>>el;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[13]
Page-13
int first=0,last=n-1,mid=0,flag=0;
while(first<=last&&flag==0) { mid=(first+last)/2;
if(A[mid]==el) { flag=mid; }
else if(A[mid]<el) { first=mid+1; }
else { last=mid-1; }
} if(flag>0)
cout<<"\n\tThe Element is Found at: "<<++flag <<" in the sorted array";
else cout<<"\n\tThe Element is not in the Array !";
break;
} cout<<"\n\tDo you want to do again (y/n)..."; cin>>opt;
if((opt=='y')||(opt=='Y')) { goto G; } else exit ;
}
OUTPUT �
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[14]
Page-14
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. In bubble sort two adjoing values are compared so the code for this must be
written in a logical way so the no error may occur. 2. First algorithm for the bubble sort must be chalked out in a separate place
then it must be converted into C++ language. 3. Comments must be given at appropriate places, so that program may be
refined and developed afterwards in future. 4. In a big program, there may be typing errors. So typing must be done
carefully. 5. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[15]
Page-15
DATE :
OBJECT � A class student has three data members and few member functions:
� Name � Rollno
� Marks
� Function members as per need. Now create a file for a menu driven program
1. To add records 2. To show records along with the stream according to the
total marks of the student: � 96 or more Computer Sc.
� 91 – 95 Electronics � 86 – 90 Mechanics
� 81 – 85 Electrical � 76 – 80 Chemical
� 71 – 75 Civil
SOURCE CODE ����
#include<fstream.h> #include<stdio.h>
#include<conio.h> #include<process.h>
class student { char name[30];
int rollno; int marks;
public: void input()
{ cout<<"\n\tEnter Name : "; gets(name); cout<<"\tEnter Rollno. : "; cin>>rollno;
cout<<"\tEnter marks : "; cin>>marks; }
void display()
{ cout<<"\n\t"<<name<<"\t"<<rollno<<"\t"<<marks<<"\t"; if(marks>=96) cout<<"Computer sc.";
else if(marks>=91&&marks<=95) cout<<"Electronics"; else if(marks>=86&&marks<=90) cout<<"Mechanical";
else if(marks>=81&&marks<=85) cout<<"Electrical"; else if(marks>=76&&marks<=80) cout<<"Chemical";
else if(marks>=71&&marks<=75) cout<<"Civil"; else cout<<"none";
} };
void main() { G:
clrscr();
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[16]
Page-16
student s;
int n,i,j; fstream ofile,afile;
char ch,opt; textcolor(YELLOW);
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; cout<<"\t\t"; cprintf("A Creation of: A. NASRA"); cout<<"\n";
cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~\n"; cout<<"\n\t1.Add records\n\t2.Show Records\n";
cout<<"\n\t\tMy choice is : "; cin>>ch; clrscr() ;
switch(ch) {
case '1' : cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n";
cout<<"\~~~~~~~~~~~-------------------------~~~~~~~~~~~~\n"; ofile.open("st.dat",ios::app|ios::binary);
cout<<"\n\tEnter no. of records to be Entered : "; cin>>n; for(i=0;i<n;i++)
{ s.input();
ofile.write((char*)&s,sizeof(student)); }
ofile.close(); break;
case '2' : cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n"; cout<<"\~~~~~~~~~~~-------------------------~~~~~~~~~~~~\n";
cout<<"\n\tName\tRollno\tMarks\tStream";
cout<<"\n\t-----------------------------------"; afile.open("st.dat",ios::in);
while(afile) {
afile.read((char*)&s,sizeof(student)); if (!afile)
break; s.display();
} afile.close();
break; }
cout<<"\n_______________________________________________\n"; cout<<"\n\t DO U want to continue(y/n)... ";
cin>>opt;
if((opt=='y')||(opt=='Y')) goto G;
else exit ; }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[17]
Page-17
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ����
1. While declaring the class the inline function must not be complex. 2. After the completion of class code terminator ( ; ) must not be forgotten.
3. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
4. In a big program, there may be typing errors. So typing must be done carefully.
5. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[18]
Page-18
DATE :
OBJECT � Declare a class containing • Eno
• Name] • Salary
• And required functions
Write a menu driven program • To create an array of 10 employees
• To display the record in ascending order of salary
SOURCE CODE ���� #include<iostream.h>
#include<conio.h> #include<stdio.h>
#include<process.h> #include<iomanip.h>
class employee {
int eno;
char name[30]; float salary;
public : void input()
{ cout << "\n\tEnter Emp. Number \t : ";
cin >>eno; cout << "\tEnter Name of Employee\t : " ;
gets(name); cout << "\tEnter salary (in Rs.) \t : ";
cin >>salary; }
void show() {
cout <<"\n\t"<<eno<<"\t\t"<<name<<"\t\t"<<salary<<endl;
} float rt_sal()
{ return salary;
} }emp[10];
void main() {
int n,ch,i,j; char choice;
do { clrscr();
textcolor(CYAN);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[19]
Page-19
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n"; cout<<"\~~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~\n";
cout<<"\n\t1. Enter Data of Employees."<<endl; cout<<"\n\t2. Show Employees' Tabular Report."<<endl;
cout<<"\n\t3. EXIT "<<endl; cout << "\n\tMy Choice is... ";
cin >> ch; cout<<"\n__________________________________________________\n";
switch(ch) {
case 1: cout << "\n\tEnter no. of employees (<=10) : "; cin >>n;
for(i=0;i<n;i++) {
emp[i].input();
}
break; case 2:
employee temp; for (i=0;i<n;i++)
{ for(j=i;j<n-1;j++)
{ if (emp[j].rt_sal()>emp[j+1].rt_sal())
{ temp = emp[j];
emp[j]=emp[j+1]; emp[j+1]=temp;
}
} }
cout <<"\n\tEmp. Number"; cout <<"\tName";
cout <<"\t\tSalary"<<endl; int r = 8;
for(i=0;i<n;i++) {
emp[i].show(); r++;
} break;
case 3: exit(0);
}
cout<<"\n__________________________________________________\n"; cout << "\n\tDo U want to continue (y/n)... "; cin>>choice;
}while((choice == 'Y')||(choice =='y')); }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[20]
Page-20
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ����
1. 2. Comments must be given at appropriate places, so that program may be
refined and developed afterwards in future. 3. In a big program, there may be typing errors. So typing must be done
carefully. 4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[21]
Page-21
DATE :
OBJECT � Write a menu driven program to: • Create a text file
• Create another file using the first one which will store all the words starting with vowel.
SOURCE CODE ���� /*menu driven program to:1.Create a text file2.Create another file using the first
one which will store all the words starting with vowel*/ #include<fstream.h>
#include<stdio.h> #include<conio.h>
#include<process.h>
void main() {
textcolor(YELLOW); int n,j;
fstream ofile,afile;
char str[100]; char ch,ch1;
do {
clrscr(); cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n"; cout<<"\~~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~\n";
cout<<"\n\t1.Enter Text in Input File"; cout<<"\n\t2.Show Words Starting With Vowel\n ";
cout<<"\n\tMy choice is : "; cin>>ch; switch(ch)
{ case '1' :
textcolor(CYAN);
ofile.open("smp.txt",ios::out); cout<<"\n__________________________________________________\n";
cout<<"\n\tEnter The Text in Input File\n\n\t"; gets(str);
ofile<<str; ofile.close();
char tmp[20]; afile.open("smp.txt",ios::in);
ofile.open("vwl.txt",ios::out); while(!afile.eof())
{ afile.getline(tmp,20,' ');
if(tmp[0]=='a'||tmp[0]=='e'||tmp[0]=='i'||tmp[0]=='o'||tmp[0]=='u')
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[22]
Page-22
{
ofile<<tmp; ofile<<' ';
} }
afile.close(); ofile.close();
break;
case '2' :
textcolor(5); //Magenta color cout<<"\n__________________________________________________\n";
cout<<"\n\tWord(s) starting with Vowel are:\n\n\t "; afile.open("vwl.txt",ios::in);
while(afile)
{ afile.get(ch);
cout<<ch; }
afile.close();
break;
} cout<<"\n__________________________________________________\n";
cout<<"\n\tDO U want to continue(y/n)... "; cin>>ch1;
}while(ch1=='Y'||ch1=='y'); getch();
}
OUTPUT �
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[23]
Page-23
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[24]
Page-24
DATE :
OBJECT � Write a menu driven program: • To create a text file
• Count number of vowels, digits and words • Create another file using the original which will contain the text
after replacing all the blank spaces with _
SOURCE CODE ����
#include<fstream.h> #include<stdio.h>
#include<conio.h> #include<ctype.h>
#include<process.h> void main()
{ int n,j;
fstream ofile,afile; char str[100];
char ch,ch1;
do { clrscr();
textcolor(CYAN + BLINK); cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t"; cprintf(" A Creation of: A. NASRA"); cout<<"\n\n~~~~~~~~~~~~-------------------------~~~~~~~~~~~~\n";
textcolor(12); cout<<"\n\t";cprintf("1.Write Text In Input File");
cout<<"\n\t";cprintf("2.Count vowels, words and digits"); cout<<"\n\t";cprintf("3.Show Text, Blank Spaces Replaced With _");
cout<<"\n\n\t My Choice is ... "; cin>>ch;
cout<<"\n_________________________________________________\n"; switch(ch)
{
case '1' : textcolor(5);
ofile.open("smp.txt",ios::out); cout<<"\n\tEnter The Text\n\n\t ";
gets(str); ofile<<str;
ofile.close(); break;
case '2' : textcolor(7);
char tmp1; int v=0,d=0,w=0;
afile.open("smp.txt",ios::in);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[25]
Page-25
while(!afile.eof())
{ afile.get(tmp1);
if(tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u') v++;
if(isdigit(tmp1)) d++;
if(tmp1==' '||tmp1=='.') w++;
} afile.close();
cout<<"\n\tNo of Vowels: "<<v; cout<<"\n\tNo of Digits: "<<d;
cout<<"\n\tNo of Words : "<<w; break;
case '3' :
textcolor(YELLOW); char tmp2;
afile.open("smp.txt",ios::in); ofile.open("spl.txt",ios::out);
while(!afile.eof()) {
afile.get(tmp2); if(tmp2==' ')
{ ofile<<'_';
} else
{ ofile<<tmp2;
}
} afile.close();
ofile.close(); cout<<"\n\tFormatted text is:\n\n\t";
afile.open("spl.txt",ios::in); while(afile)
{ afile.get(ch);
cout<<ch; }
afile.close(); break;
} cout<<"\n__________________________________________________\n";
cout<<"\n\t DO U want to continue(y/n)... ";
cin>>ch1; }while(ch1=='Y'||ch1=='y');
getch(); }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[26]
Page-26
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[27]
Page-27
DATE :
OBJECT � A blood bank maintains the record of donor: name, address blood group. Write a menu driven program:
• To create file of donor • Print the name of all the donors having given blood group
• Print the tabular list of donors.
SOURCE CODE ����
#include<fstream.h> #include<stdio.h>
#include<conio.h> #include<process.h>
#include<string.h> class donor
{ char name[30]; char address[30];
char bgroup[5]; public:
void input()
{ cout<<"\n\tEnter Donor Name\t: "; gets(name);
cout<<"\tEnter Address\t\t: "; gets(address); cout<<"\tEnter Blood Group\t: "; gets(bgroup);
} void display()
{ textcolor(5); cout<<"\n\tDonor Name: "<<name;
cout<<"\n\tAddress: "<<address; cout<<"\n\tBlood Group: "<<bgroup;
cout<<"\n\t---------------------------------------"; }
char *getbgroup() {
return bgroup;
} };
void main() { donor d;
int n,i,j; fstream ofile,afile;
char ch,ch1; textcolor(14);
do { clrscr();
textcolor(5); cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t";cprintf(" A Creation of: A. NASRA");cout<<"\n";
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[28]
Page-28
cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~\n";
cout<<"\n\t1.Add doners records in a file"; cout<<"\n\t2.Search Records from the file";
cout<<"\n\t3.List Records in tabular form"; cout<<"\n\n\t My Choice is ... "; cin>>ch;
cout<<"\n_________________________________________________\n"; switch(ch)
{ case '1' : ofile.open("dnr.dat",ios::out|ios::binary);
cout<<"\n\tEnter no. of records to be Entered: "; cin>>n;
for(i=0;i<n;i++) { d.input();
ofile.write((char*)&d,sizeof(donor)); }
ofile.close();
break; case '2' :
cout<<"\n\tEnter Blood Group to be searched: "; char bg[5],flag=0;
gets(bg); afile.open("dnr.dat",ios::in);
while(afile) { afile.read((char *)&d,sizeof(donor));
if(!afile) break;
if (strcmp(bg,d.getbgroup())==0) { d.display();
flag=1; }
}
if(flag==0) { textcolor(4);
cout<<"\n\t "; cprintf("!! RECORD NOT FOUND !!");
} afile.close();
break; case '3' :
afile.open("dnr.dat",ios::in); while(afile)
{ afile.read((char *)&d,sizeof(donor)); if(!afile)
break; d.display();
}
afile.close(); break;
} textcolor(14);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[29]
Page-29
cout<<"\n__________________________________________________\n"; cout<<"\n\t DO U want to continue(y/n)... "; cin>>ch1;
}while(ch1=='Y'||ch1=='y'); getch();
}
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[30]
Page-30
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[31]
Page-31
DATE :
OBJECT � Declare a class containing: • Bno //Book Number
• Bname //Book Name • Price
• And required functions
Write a menu driven program: • To add record of book in a file
• To search a book • To modify the price of the given Bno.
SOURCE CODE ����
#include<fstream.h>
#include<stdio.h> #include<conio.h>
#include<process.h> class book
{
char bname[30]; int bno;
float price; public:
void input() {
cout<<"\n\tEnter Book Name : "; gets(bname);
cout<<"\tEnter BOOK No. : "; cin>>bno;
cout<<"\tEnter Price (in Rs.) : "; cin>>price;
} void setprice()
{
cout<<"\n\tEnter Price (in Rs.): "; cin>>price;
} void display()
{ cout<<"\n\tBook Name: "<<bname<<"\tBook No.: "<<bno<<"\tPrice:
"<<price<<"\t";
} int getbno()
{ return bno;
}
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[32]
Page-32
};
void main() { book b;
int n,i,j; fstream ofile,afile;
char ch,ch1; textcolor(3);
do { clrscr();
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~\n"; cout<<"\t\t A Creation of: A. NASRA\n";
cout<<"\~~~~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~~~~\n";
cout<<"\n\t1.Add Record of Books";
cout<<"\n\t2.Search Book"; cout<<"\n\t3.Modify Price of a Book";
cout<<"\n\n\t My Choice is ... "; cin>>ch;
cout<<"\n________________________________________________________\n";
switch(ch) {
case '1' : ofile.open("bk.dat",ios::out|ios::binary);
cout<<"\n\tEnter no. of Records to be Entered: "; cin>>n;
for(i=0;i<n;i++) {
b.input();
ofile.write((char*)&b,sizeof(book)); }
ofile.close(); break;
case '2' : cout<<"\n\tEnter Book No. to be Searched: ";
int bn,flag=0; cin>>bn;
afile.open("bk.dat",ios::in); while(afile)
{ afile.read((char *)&b,sizeof(book));
if(!afile) break;
if (bn==b.getbno())
{ b.display();
flag=1; break;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[33]
Page-33
}
} if(flag==0)
{ textattr(4); textcolor(4+128);
cout<<"\n\t\t\a"; cprintf("!! No Record Found !!");
} afile.close();
break; case '3' :
cout<<"\n\tEnter Book No. to Modify the Price "; int bn1,flag1=0,r=0;
cin>>bn1; afile.open("bk.dat",ios::in|ios::out|ios::binary);
while(afile)
{ afile.read((char *)&b,sizeof(book));
if(!afile) break;
if (bn1==b.getbno()) {
b.setprice(); afile.seekp(r*sizeof(b),ios::beg);
afile.write((char *)&b,sizeof(book)); flag1=1;
break; }
r++; }
if(flag1==0)
{ textattr(4);
textcolor(4+128); cout<<"\n\t\t\a";
cprintf("!! No Record Found !!"); }
afile.close(); break;
}textcolor(10);
cout<<"\n_____________________________________________________\n";
cout<<"\n\tDO U Want to Continue(y/n)... "; cin>>ch1;
}
while(ch1=='Y'||ch1=='y'); getch();
}
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[34]
Page-34
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[35]
Page-35
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[36]
Page-36
DATE :
OBJECT � Declare a class Sports having: • Sno
• Sname • Fees
• Required functions
Write a menu driven program to: • Append record in a file
• Search record of a student • Delete the record of given sno
SOURCE CODE ����
#include<fstream.h> #include<stdio.h>
#include<conio.h> #include<process.h>
class students {
char sname[30];
int sno; float fees;
public: void input()
{ cout<<"\n\tEnter Student Name\t: ";
gets(sname); cout<<"\tEnter Roll No.\t\t: ";
cin>>sno; cout<<"\tEnter Fees\t\t: ";
cin>>fees; }
void display() {
cout<<"\n\tStudent Name\t: "<<sname<<"\n\tRoll No.\t:
"<<sno<<"\n\tFees\t\t: "<<fees<<"\t"; }
int getsno() {
return sno; }
}; void main()
{
students s; int n,i,j;
fstream ofile,afile;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[37]
Page-37
char ch,ch1; textcolor(10);
do { clrscr();
cout<<"\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\t\t A Creation of: A. NASRA\n"; cout<<"\n~~~~~~~~~~~~~~~~-------------------------
~~~~~~~~~~~~~~~~~~~~\n"; textcolor(2);
cout<<"\n\t";cprintf("1. Add Record of Student in a File"); textcolor(4);
cout<<"\n\t";cprintf("2. Search Record of a Studnet"); textcolor(6);
cout<<"\n\t";cprintf("3. Delete Record of a Student");
textcolor(5); cout<<"\n\n\t";cprintf("My choice is ... "); cin>>ch;
cout<<"\n________________________________________________________
____\n"; switch(ch)
{ case '1' :
ofile.open("std.dat",ios::out|ios::binary); cout<<"\n\tEnter no. of records to be Entered: ";
cin>>n; for(i=0;i<n;i++)
{ s.input();
ofile.write((char*)&s,sizeof(students));
} ofile.close();
break; case '2' :
cout<<"\n\tEnter Roll No. to be Searched: "; int sn,flag=0;
cin>>sn; afile.open("std.dat",ios::in);
while(afile) {
afile.read((char *)&s,sizeof(students)); if(!afile)
break; if(sn==s.getsno())
{
s.display(); flag=1;
break; }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[38]
Page-38
}
if(flag==0) { textcolor(4+128);
cout<<"\n\t\t\a"; cprintf("!! NO RECORD FOUND !!");
} afile.close();
break; case '3' :
cout<<"\n\tEnter Roll No. to be Deleted: "; int sn1,flag1=0;
cin>>sn1; afile.open("std.dat",ios::in|ios::binary);
ofile.open("tmpstd.dat",ios::out|ios::binary); while(afile)
{
afile.read((char *)&s,sizeof(students)); if(!afile)
break; if (sn1==s.getsno())
{ textcolor(14); cout<<"\n\t";cprintf(" !! RECORD DELETED !!");
flag1=1; }
else {
ofile.write((char *)&s,sizeof(students)); }
}
if(flag1==0)
{ textcolor(4+128); cout<<"\n\t\t\a";
cprintf("!! NO RECORD FOUND !!"); }
afile.close(); ofile.close();
afile.open("tmpstd.dat",ios::in|ios::binary); ofile.open("std.dat",ios::out|ios::binary);
while(afile) {
afile.read((char *)&s,sizeof(students)); ofile.write((char *)&s,sizeof(students));
} afile.close();
ofile.close();
break;
} textcolor(11);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[39]
Page-39
cout<<"\n____________________________________________________________\n";
cout<<"\n\t DO U want to continue(y/n)... "; cin>>ch1; }while(ch1=='Y'||ch1=='y');
getch(); }
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[40]
Page-40
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[41]
Page-41
DATE :
OBJECT � Declare a class containing: • Name
• Address • Telephone/mobile no
Write a menu driven program to:
• Append record in a file • Display the name and address for a given
telephone/mobile no.
SOURCE CODE ����
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[42]
Page-42
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[43]
Page-43
DATE :
OBJECT � Make a C++ program for the linear search of an element in a one dimensional array.
SOURCE CODE ����
/*Linear search in 1D arry*/ #include<iostream.h>
#include<conio.h> #include<process.h>
int Lsearch(int[], int, int); void main()
{ G: textcolor(6);
clrscr(); char opt;
int AR[50], ITEM, N, index; cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
textcolor(10);
cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~-------------------------~~~~~~~~~~~~~~~~~~\n";
cout<<"\n\tEnter desired array size (max. 50)... "; cin>>N; cout<<"\n\tEnter "<<N<<" elements of array AR\n\n";
for(int i=0;i<N;i++) {
cout<<"\t\tAR["<<i<<"] = "; cin>>AR[i]; }
cout<<"\n\tEnter Element to be searched for... " ; cin>> ITEM; cout<<"\n___________________________________________________\n";
index=Lsearch(AR, N, ITEM); if(index== -1)
{ textcolor(12); cout<<"\n\t";cprintf("Sorry!! Given element is not in the array.");
}
else cout<<"\n\tElement found at AR["<<index<<"]"<<endl;
cout<<"\n____________________________________________________\n"; cout<<"\n\tDo you want to do again (y/n)... ";
cin>>opt; textcolor(6);
if((opt=='y')||(opt=='Y')) goto G; else exit ;
} //end of main int Lsearch(int AR[], int size, int item)
{ for(int i=0;i<size;i++) { if(AR[i]==item) return i; }
return -1; }
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[44]
Page-44
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
DATE :
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[45]
Page-45
OBJECT � Make a C++ program for the binary search of an element in a one dimensional array.
SOURCE CODE ����
/*Binary search in 1D arry*/ #include<iostream.h>
#include<conio.h>
#include<process.h> int Bsearch(int[], int, int);
void main() { G:
textcolor(11); clrscr();
char opt; int AR[50], ITEM, N, index;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
textcolor(10); cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n";
cout<<"\~~~~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"\n\tEnter desired array size (max. 50)... "; cin>>N;
cout<<"\n\tEnter "<<N<<" elements of array AR (in ascending order):\n\n"; for(int i=0;i<N;i++)
{ cout<<"\t\tAR["<<i<<"] = "; cin>>AR[i]; } cout<<"\n\tEnter Element to be searched for... " ; cin>> ITEM;
cout<<"__________________________________________________________
________\n"; index=Bsearch(AR, N, ITEM);
if(index== -1) { textcolor(12);
cout<<"\t";cprintf("Sorry!! Given element is not in the array.");cout<<"\n"; }
else cout<<"\tElement found at AR["<<index<<"]\n";
cout<<"__________________________________________________________________\n";
cout<<"\tDo you want to do again (y/n)... "; cin>>opt;
textcolor(11); if((opt=='y')||(opt=='Y'))
goto G; else exit ; } //end of main
int Bsearch(int AR[], int size, int item) { int beg, last, mid;
beg=0; last =size-1;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[46]
Page-46
while(beg<=last)
{ mid = (beg+last)/2; if(item == AR[mid])
return mid; else if(item>AR[mid]) beg = mid + 1;
else last = mid - 1; }
return -1; }
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ����
1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
DATE :
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[47]
Page-47
OBJECT � Make a C++ program to illustrate the sorting of an one dimensional array by Selection Sort.
SOURCE CODE ����
/*Selection sort in 1D array*/ #include<iostream.h>
#include<conio.h>
#include<process.h> void SelSort(int[], int);
void main() { G:
textcolor(11); clrscr();
char opt; int AR[50], ITEM, N, index;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~\n"; textcolor(14);
cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~~~~~~~~-------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"\n\tEnter desired array size (max. 50)... "; cin>>N; cout<<"\n\tEnter "<<N<<" elements in array AR:\n\n";
for(int i=0;i<N;i++) { cout<<"\t\tAR["<<i<<"] = "; cin>>AR[i]; }
cout<<"__________________________________________________________
________\n"; SelSort(AR, N);
cout<<"\n\tThe Sorted array is as shown below:\n"; for(i=0;i<N;i++)
{ cout<<"\t\tAR["<<i<<"] = "<<AR[i]<<endl; }
cout<<"__________________________________________________________________\n";
cout<<"\tDo you want to do again (y/n)... ";
cin>>opt; textcolor(11);
if((opt=='y')||(opt=='Y')) goto G; else exit ;
} //end of main void SelSort(int AR[], int size)
{ int pos, tmp, sm; for(int i=0;i<size;i++)
{ sm = AR[i]; for(int j=i+1;j<size;j++)
{ if(AR[j] < sm)
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[48]
Page-48
{ sm = AR[j]; pos = j; }
} tmp = AR[i]; AR[i] = AR[pos]; AR[pos] = tmp;
textcolor(13); cout<<"\n\t";cprintf("Array after Pass -");cout<<i+1;cprintf(" is: ");
for(j=0;j<size;j++) { cout<<AR[j]<<" "; } }
}
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ����
1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
DATE :
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[49]
Page-49
OBJECT � Make a C++ program to illustrate the sorting of an one dimensional array by Bubble Sort.
SOURCE CODE ����
/*Bubble sort in 1D array*/ #include<iostream.h>
#include<conio.h>
#include<process.h> void BubbleSort(int[], int);
void main() { G:
textcolor(11); clrscr();
char opt; int AR[50], ITEM, N, index;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~\n"; textcolor(14);
cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~~~~~~~~-------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"\n\tEnter desired array size (max. 50)... "; cin>>N; cout<<"\n\tEnter "<<N<<" elements in array AR:\n\n";
for(int i=0;i<N;i++) { cout<<"\t\tAR["<<i<<"] = "; cin>>AR[i]; }
cout<<"__________________________________________________________
________\n"; BubbleSort(AR, N);
cout<<"\n\tThe Sorted array is as shown below:\n"; for(i=0;i<N;i++)
{ cout<<"\t\tAR["<<i<<"] = "<<AR[i]<<endl; }
cout<<"__________________________________________________________________\n";
cout<<"\tDo you want to do again (y/n)... ";
cin>>opt; textcolor(11);
if((opt=='y')||(opt=='Y')) goto G; else exit ;
} //end of main void BubbleSort(int AR[], int size)
{ int tmp, ctr=0; for(int i=0;i<size;i++)
{ for(int j=0;j<(size-1)-i;j++)
{ if(AR[j] > AR[j+1])
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[50]
Page-50
{ tmp = AR[j];
AR[j] = AR[j+1]; AR[j+1] = tmp;
} }
textcolor(13); cout<<"\n\t";cprintf("Array after Iteration -");cout<<++ctr;cprintf(" is: ");
for(int k = 0;k<size;k++) cout<<AR[k]<<" ";
}
}
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ����
1. The string functions must be defined globally. 2. Comments must be given at appropriate places, so that program may be
refined and developed afterwards in future. 3. In a big program, there may be typing errors. So typing must be done
carefully. 4. Semantic errors must be avoided by doing thoughtful logical programming.
DATE :
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[51]
Page-51
OBJECT � Make a C++ program to illustrate the sorting of an one dimensional array by Insertion Sort.
SOURCE CODE ����
/*Insertion sort in 1D array*/ #include<iostream.h>
#include<conio.h>
#include<process.h> void InsSort(int[], int);
void main() { G:
textcolor(11); clrscr();
char opt; int AR[50], ITEM, N, index;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~~~~~~~~~~~~~~~~~~~~~~~\n"; textcolor(13);
cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~~~~~~~~-------------------------
~~~~~~~~~~~~~~~~~~~~~~~~~~";
cout<<"\n\tEnter desired array size (max. 50)... "; cin>>N; cout<<"\n\tEnter "<<N<<" elements in array AR:\n\n";
for(int i=0;i<N;i++) { cout<<"\t\tAR["<<i<<"] = "; cin>>AR[i]; }
cout<<"__________________________________________________________
________\n"; InsSort(AR, N);
cout<<"\n\tThe Sorted array is as shown below:\n"; for(i=0;i<N;i++)
{ cout<<"\t\tAR["<<i<<"] = "<<AR[i]<<endl; }
cout<<"__________________________________________________________________\n";
cout<<"\tDo you want to do again (y/n)... ";
cin>>opt; textcolor(11);
if((opt=='y')||(opt=='Y')) goto G; else exit ;
} //end of main void InsSort(int AR[], int size)
{ int tmp, j; for(int i=0;i<size;i++)
{ tmp = AR[i];
j = i-1;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[52]
Page-52
while((tmp < AR[j])&&(j >= 0))
{ AR[j+1] = AR[j]; j-- ;
} AR[j+1] = tmp;
textcolor(14);
cout<<"\n\t";cprintf("Array after Pass -");cout<<i;cprintf(" is: "); for(int k = 0;k<size;k++)
cout<<AR[k]<<" ";
} }
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
DATE :
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[53]
Page-53
OBJECT � Write the show the execution of a C++ program for the merging sort of two unsorted arrays. First the two unsorted arrays are to be
sorted with the help of Bubble Sort function and then the sorted arrays are to be merged and sorted in the final array.
SOURCE CODE ����
/*Merging of two unsorted 1D arrays in one 1D array*/
#include<iostream.h> #include<conio.h>
#include<process.h> void SelSort(int[], int);
void main() { G:
textcolor(11); clrscr();
char opt; int AR1[30], AR2[30],final[60], N1, N2, ptr1,ptr2,ptr3 ;
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; textcolor(14);
cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~~~";
cout<<"\n\tEnter desired array size in 1st array(max. 30)... "; cin>>N1;
cout<<"\n\tEnter "<<N1<<" elements in array AR1:\n\n"; for(int i=0;i<N1;i++)
{ cout<<"\t\tAR1["<<i<<"] = "; cin>>AR1[i]; } cout<<"\n\tEnter desired array size in 2nd array(max. 30)... "; cin>>N2;
cout<<"\n\tEnter "<<N2<<" elements in array AR2:\n\n"; for(i=0;i<N2;i++)
{ cout<<"\t\tAR2["<<i<<"] = "; cin>>AR2[i]; } cout<<"\______________________________________________________\n";
SelSort(AR1,N1); SelSort(AR2,N2);//calling of sorted arrays. ptr1=ptr2=ptr3=0;
while((ptr1<N1)&&(ptr2<N2)) { if(AR1[ptr1]<AR2[ptr2])
final[ptr3++]=AR1[ptr1++]; else
final[ptr3++]=AR2[ptr2++];
} while(ptr2<N2)
final[ptr3++]=AR2[ptr2++]; while(ptr1<N1)
final[ptr3++]=AR1[ptr1++]; cout<<"\n\tThe merged array is: ";
for(i=0;i<N1+N2;i++) cout<<final[i]<<" ";
cout<<"\n_____________________________________________________\n"; cout<<"\tDo you want to do again (y/n)... ";
cin>>opt;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[54]
Page-54
textcolor(11);
if((opt=='y')||(opt=='Y')) goto G; else exit ;
} //end of main void SelSort(int AR[], int size)
{ int pos, tmp, sm; for(int i=0;i<size-1;i++)
{ sm = AR[i];pos=i; for(int j=i+1;j<size;j++)
{ if(sm > AR[j]) { sm = AR[j]; pos = j; }
} tmp = AR[i]; AR[i] = AR[pos]; AR[pos] = tmp;
} }
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
DATE :
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[55]
Page-55
OBJECT � Write and execute a program that displays the multiplication of two
matrices of desired size of the user together with the info that the matrices are compatible for product or not.
SOURCE CODE ����
#include<iostream.h> #include<conio.h>
#include<process.h> void main()
{ G:
clrscr(); int A[10][10], B[10][10], C[10][10], i, j, m, n, p, q, ip; char opt;
textcolor(11); cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
textcolor(13); cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n";
cout<<"\~~~~~~~~~-------------------------~~~~~~~~~~~~~~~~~~~\n"; cout<<"\n\tEnter Row (max.10) & Column (max.10) of Matrix A:\n";
cout<<"\t Row = "; cin>>m; cout<<"\t Col = "; cin>>n; cout<<"\n\tEnter Row (max.10) & Column (max.10) of Matrix B:\n";
cout<<"\t Row = "; cin>>p; cout<<"\t Col = "; cin>>q; if(n == p)
{ textcolor(14); cout<<"\n\t";cprintf("Matrices can be multiplied because,");
cout<<"\n\t\t";cprintf("Coloumn of A = Row of B.");
cout<<"\n\tInput the elements of matrix A:\n"; for(i=0; i<m; ++i)
{ cout<<"\n"; for(j=0; j<n; ++j)
{ cout<<"\tA["<<i<<"]["<<j<<"] = "; cin>>A[i][j] ; } }
cout<<"\n\tInput the elements of matrix B:\n"; for(i=0; i<p; ++i)
{ cout<<"\n"; for(j=0; j<q; ++j)
{ cout<<"\tB["<<i<<"]["<<j<<"] = "; cin>>B[i][j]; } }
for(i=0;i<m;++i) for(j=0;j<q;++j)
{ C[i][j] = 0;
for(ip=0;ip<n;++ip) C[i][j] += (A[i][ip]*B[ip][j]);
} cout<<"\n_____________________________________________________\n";
cout<<"\n\tProduct of Matrices A and B is:\n" ; for(i=0;i<m;++i)
{ cout<<"\n\t"; for(j=0;j<q;++j)
cout<<C[i][j]<<"\t"; }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[56]
Page-56
}
else { textcolor(12);
cout<<"\n\t";cprintf("Matrices can't be multiplied because,"); cout<<"\n\t\t";cprintf("Coloumn of A != Row of B.");
} textcolor(11);
cout<<"\n_____________________________________________________\n"; cout<<"\n\tDo you want to do again (y/n)..."; cin>>opt;
if((opt=='y')||(opt=='Y')) goto G; else exit ; }
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[57]
Page-57
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[58]
Page-58
DATE :
OBJECT � Write a C++ program to convert Infix expression to Postfix expression, using a Stack.
SOURCE CODE ����
/*Infex 2 Postfix using stack*/
#include<iostream.h> #include<stdlib.h>
#include<conio.h> #include<stdio.h>
#include<string.h> #include<process.h>
const int size = 50; char infix[size], postfix[size], stack[size]; int top = -1; int precedence(char ch); //function to get precedence of operator
char pop(); //function to pop an element from stack char topelement(); //function to return top element from stack without pop
void push(char ch); //function to push an element in stack int braces(char*); //function to match number of braces
void main()
{ G: textcolor(11);
clrscr(); char opt, ele,elem, st[2];
int prep, pre, popped, j=0, chk=0; strcpy(postfix, " ");
cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"; textcolor(13);
cout<<"\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~~~~~-------------------------~~~~~~~~~~~~~~~~";
cout<<"\n\tASSUMPTION: Infix expression contains single letter variables\n\t" <<"\t\tand single digit constants only.\n";
cout<<"\n\tEnter Infix Expression:"; gets(infix); chk = braces(infix);
if(chk != 0)
{ textcolor(12); cout<<"\n\t"; cprintf("Unbalanced no. of braces! Extra ");
cout<<(chk==1?"Right braces":" Left braces"); getch(); cout<<"\n_____________________________________________________\n";
cout<<"\n\tDo you want to do again (y/n)..."; cin>>opt; if((opt=='y')||(opt=='Y'))
goto G; else exit ; }
for(int i =0; infix[i]!='\0';i++) { if(infix[i]!='('&& infix[i]!=')' && infix[i] != '^'&&infix[i] !='*'&&infix[i] !='/'
&&infix[i] !='+' && infix[i] !='-') postfix[j++]=infix[i];
else if(infix[i]=='(')
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[59]
Page-59
{ elem = infix[i]; push(elem); }
else if(infix[i] == ')') { while((popped = pop()) != '(')
{ postfix[j++] = popped; } }
else { elem = infix[i];
pre = precedence(elem); ele = topelement();
prep = precedence(ele); if(pre >prep) push(elem);
else { while(prep >= pre)
{ if(ele =='#') break; popped = pop();
ele = topelement();
postfix[j++] = popped; prep = precedence(ele);
} push(elem);
} }
} while((popped = pop()) != '#')
postfix[j++] = popped; postfix[j] ='\0';
cout<<"\n_____________________________________________________\n"; textcolor(12);
cout<<"\n\t"; cprintf("Postfix Expression: "); cout<< postfix ; getch(); cout<<"\n_____________________________________________________\n";
cout<<"\n\tDo you want to do again (y/n)...";
cin>>opt; textcolor(11); if((opt=='y')||(opt=='Y')) goto G; else exit ;
} int precedence( char ch)
{ switch(ch) { case '^' : return 5;
case '/' : return 4; case '*' : return 4;
case '+' : return 4; case '-' : return 4;
default : return 0; }
} char pop()
{ char ret;
if(top!=-1) { ret = stack[top];
top-- ; return ret ; }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[60]
Page-60
else return '#';
} char topelement()
{ char ch; if(top!=-1) ch = stack[top];
else ch='#'; return ch; }
void push(char ch) { if(top!=size-1)
{ top++ ; stack[top] = ch ; } }
int braces(char * s) { int leftbr, rightbr;
leftbr=rightbr=0; for(int i =0; s[i];i++)
{ if(s[i] =='(') leftbr++;
else if(s[i] == ')') rightbr++; }
if(leftbr == rightbr) return 0; else if(leftbr<rightbr) return 1; else return -1;
}
OUTPUT ����
POSSIBLE ERRORS AND PRECAUTIONS ����
1. The string functions must be defined globally. 2. Comments must be given at appropriate places, so that program may be
refined and developed afterwards in future. 3. In a big program, there may be typing errors. So typing must be done
carefully. 4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[61]
Page-61
DATE :
OBJECT � Write a program to illustrate the PUSH and POP operation of an array stack which stores the information as integer value, along
with the current status of the stack after every push or pop operation
SOURCE CODE ���� #include<iostream.h>
#include<conio.h> #include<process.h>
int push(int [],int &,int ); int pop(int [],int &);
void display(int [],int ); const int size=50;
void main() { G: textcolor(11); clrscr();
int stack[size], item, top=-1, res ; char ch , opt; cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
textcolor(13);
cout<<"\t\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\n\tPUSH OPERATION starts...\n"; do { cout<<"\n\tEnter the value to be Pushed "; cin>> item;
res=push(stack,top,item); if(res == -1) {cout<<"\n\tOVERFLOW !";}
display(stack,top); cout<<"\n\tPush more elements? (y/n)... "; cin>>ch;
}while((ch=='y')||(ch =='Y')) ; cout<<"\n____________________________________________________\n";
cout<<"\n\tNow POP OPERATION starts...\n"; do { res = pop(stack, top);
if(res == -1) { cout<<"\n\tUNDERFLOW !";} else { cout<<"\n\tPopped element is: " <<res; }
cout<<"\n";
display(stack,top); cout<<"\n\tPop more elements? (y/n)... "; cin >> ch;
}while((ch=='y')||(ch =='Y')); cout<<"\n____________________________________________________\n";
cout<<"\tDo you want to do again (y/n)... "; cin>>opt; textcolor(11); if((opt=='y')||(opt=='Y')) goto G; else exit ;
} int push(int stack[], int & top, int ele)
{ if(top == (size - 1)) return -1; else { top++; stack[top] = ele; }
return 0; }
int pop(int stack[], int &top)
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[62]
Page-62
{ int ret;
if(top == -1) return -1; else { ret = stack[top]; top--;} return ret;
} void display(int stack[], int top)
{ cout<<"\n\tCURRENT STATUS OF STACK"; cout<<"\n\t--------------------------------";
if(top == -1) return; cout<<"\n\t\t";
for(int i=top ; i >= 0; i--) cout<<stack[i]<<"\n\t\t"; }
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[63]
Page-63
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[64]
Page-64
DATE :
OBJECT � Write a following menu driven program to illustrate operation on a Linked Stack which stores the information as integer value:
1. PUSH into Linked Stack 2. POP from Linked Stack
3. Display the Linked Stack
4. Exit from Menu
SOURCE CODE ���� #include<iostream.h>
#include<stdio.h> #include<conio.h>
#include<stdlib.h> #include<ctype.h>
//Declares a stack structure struct node
{ int data; node *link;
};
node *push(node *top, int val); // Add stack node *pop(node *top, int &val); // Delete stack
void show_Stack(node *top); // Show stack //Main programming logic
void main() { node *top;
int val; int choice;
char opt = 'Y';// To continue the do loop in case top = NULL; // Initialization of Stack
do { textcolor(11); cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
textcolor(4); cout<<"\t\t\t";cprintf(" A Creation of: A. NASRA"); cout<<"\n"; cout<<"\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
cout<<"\n\t";textcolor(12);cprintf(" MENU FOR STACK OPERATION");
cout<<"\n\t";textcolor(8);cprintf("--------------------------"); cout<<"\n\t";textcolor(9);cprintf("1.PUSH into Linked Stack");
cout<<"\n\t";textcolor(10);cprintf("2.POP from Linked Stack"); cout<<"\n\t";textcolor(14);cprintf("3.Display Linked Stack");
cout<<"\n\t";textcolor(5);cprintf("4.Exit from Menu"); cout<<"\n\n\tEnter Your choice from above "; cin >> choice;
cout<<"\n----------------------------------------------------------------------------"; switch (choice)
{ case 1: do
{ textcolor(11); cout<<"\n\tEnter the value to be pushed in stack "; cin >> val;
top = push(top, val);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[65]
Page-65
cout<<"\n\tDo you want to push more element <Y/N>? "; cin >> opt;
}while (toupper(opt) == 'Y'); clrscr();
break; case 2:
opt = 'Y'; // Initialize for the second loop do
{ textcolor(11); top = pop(top,val);
if (val != -1) cout<<"\n\tValue popped from Stack is " << val;
cout<<"\n\n\tDo you want to pop more element <Y/N>? "; cin >> opt; }while (toupper(opt) == 'Y');
clrscr(); break;
case 3:
do { textcolor(11);;
show_Stack(top); cout<<"\n\tDo you want to see for more time? <Y/N> "; cin >> opt;
}while(toupper(opt) == 'Y'); clrscr();
break; case 4:
exit(0); }
}while(choice != 4); }
// Function body for push stack elements node *push(node *top, int val)
{ node *temp;
temp = new node; temp->data = val;
temp->link = NULL; if(top ==NULL)
top = temp; else
{ temp->link = top; top = temp;
} return(top);
} // Function body for pop stack elements
node *pop(node *top,int &val) { node *temp;
if (top == NULL )
{ textcolor(4); cout<<"\n\t";cprintf("UNDERFLOW !! (i.e., Stack Empty)");
val = -1; textcolor(11); }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[66]
Page-66
else
{ temp = top; top = top->link;
val = temp->data; temp->link = NULL;
delete temp; }
return (top); }
// Function body for show stack elements void show_Stack(node *top)
{ node *temp; temp = top;
cout<<"\n\tCURRENT STATUS OF LINKED STACK"; cout<<"\n\t------------------------------\n";
while (temp != NULL)
{ cout <<"\n\t\t"<< temp->data; temp = temp->link;
} cout<<"\n\t\t";
}
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[67]
Page-67
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[68]
Page-68
DATE :
OBJECT � Write a following menu driven program to illustrate operation on a Array Queue which sotres the information as integer value :
1. Addition To Array Queue 2. Deletion From Array Queue
3. Display Of Array Queue
4. Exit from Menu
SOURCE CODE ���� /*operation of add, delete,and show array queue*/
#include <iostream.h> #include <stdio.h>
#include <conio.h> #include <stdlib.h>
#include <ctype.h> #define MAX 100 // Shows maximum array length
int queue[MAX]; // Declares array global variable int front, rear; // Declares integer front and rear
//FP of add, delete, and show queue in arrayimplementation
void add_Q(int queue[], int val, int &rear); // Add queue int del_Q(int queue[], int &front, int rear); // Delete queue
void show_Q(int queue[], int front, int rear); // Show queue void main()
{ int choice, val; char opt = 'Y'; // To continue the do loop in case
rear = -1; // Initialization of Queue front = -1;
do {
cout<<"\t\t\t.______________________.\n"; cout<<"\t\t\t";textcolor(12);cprintf("| CREATION OF A. NASRA |");
cout<<"\n"; cout<<"\t\t\t*~~~~~~~~~~~~~~~~~~~~~~*\n";
cout<<"\n\t";textcolor(12);cprintf(" MENU FOR QUEUE OPERATION");
cout<<"\n\t";textcolor(8); cprintf("--------------------------"); cout<<"\n\t";textcolor(9);cprintf("1.Addition To Array Queue");
cout<<"\n\t";textcolor(10);cprintf("2.Deletion From Array Queue"); cout<<"\n\t";textcolor(14);cprintf("3.Display Of Array Queue");
cout<<"\n\t";textcolor(5);cprintf("4.Exit from Menu"); textcolor(11);
cout<<"\n\n\tEnter Your choice from above "; cin >> choice; cout<<"\n----------------------------------------------------------------------------";
switch (choice) { case 1:
do { cout<<"\n\tEnter the value to be added "; cin >> val;
add_Q(queue, val, rear);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[69]
Page-69
cout<<"\n\tDo you want to add more elements <Y/N> ? ";cin >> opt;
} while (toupper(opt) == 'Y'); clrscr();
break; case 2:
opt = 'Y'; // Initialize for the second loop do
{ val = del_Q(queue, front, rear); if (val != -1)
cout<<"\n\tValue deleted from Queue is " << val; cout<<"\n\tDo you want to delete more elements <Y/N> ? " ; cin >> opt;
} while (toupper(opt) == 'Y'); clrscr();
break; case 3:
do
{ show_Q(queue, front, rear); cout<<"\n\tDo you want to see for more time? <Y/N> "; cin >> opt;
}while (toupper(opt) == 'Y'); clrscr();
break; case 4:
exit(0); }
}while(choice != 4); }
//Function body for add queue with array void add_Q(int queue[], int val, int &rear)
{ if (rear == MAX) { cout <<"\n\tQUEUE FULL !!"; }
else
{ rear = rear + 1; queue[rear] = val;
} }
//Function body for delete queue with array int del_Q(int queue[], int &front, int rear)
{ int value; if (front == rear)
{ cout<<"\n\tQUEUE EMPTY !!"; value = -1;
} else
{ front = front + 1; value = queue[front];
}
return (value); }
//Function body 2 show queue with array void show_Q(int queue[], int front, int rear)
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[70]
Page-70
{ if(front == rear)
{ cout<<"\n\tQUEUE EMPTY !!"; return; } cout <<"\n\tThe Current Status of Queue is: ";
do { front = front + 1;
cout<<"\n\t\t" << queue[front]; }while (front != rear);
}
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[71]
Page-71
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[72]
Page-72
DATE :
OBJECT � Write a following menu driven program to illustrate operation on a Linked Queue which stores the information as a single character:
1. Addition To Linked Queue 2. Deletion From Linked Queue
3. Display Of Linked Queue
4. Exit from Menu
SOURCE CODE ���� /*operation to add, delete and show linked queue(8:41 AM 14/9/2011)*/
#include <iostream.h> #include <stdio.h>
#include <conio.h> #include <stdlib.h>
#include <ctype.h> // Declares a queue structure
struct node { char data;
node *link;
}; // Functions prototype to add queue, delete queue, and show queue
node *add_Q(node *rear, char val); // Add queue node *del_Q(node *front, char &val); // Delete queue
void show_Q(node *front); // Show queue // Main programming logic
void main() {
node *front, *rear; char val;
int choice; char opt = 'Y'; // To continue the do loop in case
front = rear = NULL;// Initialization of Queue do
{
cout<<"\t\t\t.______________________.\n"; cout<<"\t\t\t";textcolor(12);
cprintf("| CREATION OF A. NASRA |");cout<<"\n"; cout<<"\t\t\t*~~~~~~~~~~~~~~~~~~~~~~*\n";
cout<<"\n\t";textcolor(13);cprintf(" MENU FOR QUEUE OPERATION"); cout<<"\n\t";textcolor(8); cprintf("--------------------------");
cout<<"\n\t";textcolor(9); cprintf("1.Addition To Linked Queue"); cout<<"\n\t";textcolor(10);cprintf("2.Deletion From Linked Queue");
cout<<"\n\t";textcolor(14);cprintf("3.Display Of Linked Queue"); cout<<"\n\t";textcolor(5); cprintf("4.Exit from Menu");
cout<<"\n\n\tEnter Your choice from above "; cin >> choice; cout<<"\n-----------------------------------------------------------------------";
textcolor(3);
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[73]
Page-73
switch (choice)
{ case 1:
do {
cout<<"\n\tEnter the value to be added in the queue "; cin >> val;
rear = add_Q(rear, val); if (front == NULL)
front = rear; cout<<"\n\tDo you want to add more element <Y/N>? ";
cin >> opt; } while (toupper(opt) == 'Y');
clrscr(); break;
case 2:
opt = 'Y'; // Initialize for the second loop do
{ front = del_Q(front, val); if (front == NULL)
rear = front; if (val != -1)
cout<<"\n\tValue deleted from Queue is " << val; cout<<"\n\tDo you want to delete more element <Y/N>? ";
cin >> opt; } while (toupper(opt) == 'Y');
clrscr(); break;
case 3: do
{ show_Q(front);
cout<<"\n\tDo you want to see for more time? <Y/N> "; cin >> opt; }while (toupper(opt) == 'Y');
clrscr(); break;
case 4: exit(0);
} }while (choice != 4);
} // Function body to add queue elements
node *add_Q(node *rear, char val) { node *temp;
temp = new node; temp->data = val;
temp->link = NULL;
rear->link = temp; rear = temp;
return (rear); }
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[74]
Page-74
// Function body to delete queue elements
node *del_Q(node *front, char &val) { node *temp;
if (front == NULL) { cout<<"\n\t\tQUEUE EMPTY !! ";
val = -1; }
else { temp = front;
front = front->link; val = temp->data;
temp->link = NULL; delete temp;
} return (front);
}
// Function body to show queue elements void show_Q(node *front)
{ node *temp; temp = front;
cout <<"\n\tThe Current Status of Queue is:"; while (temp != NULL)
{ cout <<"\n\t\t"<< temp->data; temp = temp->link;
} }
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[75]
Page-75
POSSIBLE ERRORS AND PRECAUTIONS ����
1. The string functions must be defined globally. 2. Comments must be given at appropriate places, so that program may be
refined and developed afterwards in future. 3. In a big program, there may be typing errors. So typing must be done
carefully. 4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[76]
Page-76
DATE :
OBJECT � Write a following menu driven program to illustrate operation on a Circular Queue which stores the information as a single character:
1. Addition To Circular Queue 2. Deletion From Circular Queue
3. Display Of Circular Queue
4. Exit from Menu
SOURCE CODE ���� /*operation to add, delete, and show circular queue using array. The queue
contains data of type character*/ #include <iostream.h>
#include <stdio.h> #include <conio.h>
#include <stdlib.h> #include <ctype.h>
#define MAX 20 // Show maximum array length char queue[MAX]; // Declares array global variable
int front, rear; // Declares integer front and read
/*Function prototypes to add queue, delete queue and show queue in array implementation*/
void add_Q(char queue[], int front, char val, int &rear); // Add queue char del_Q(char queue[], int &front, int rear); // Delete queue
void show_Q(char queue[], int front, int rear); // Show queue void main()
{ int choice; char val;
char opt = 'Y'; // To continue the do loop in case rear = -1; // Initialization of Queue
front = -1; do
{ cout<<"\t\t\t.______________________.\n"; cout<<"\t\t\t";textcolor(12);
cprintf("| CREATION OF A. NASRA |");cout<<"\n";
cout<<"\t\t\t*~~~~~~~~~~~~~~~~~~~~~~*\n"; cout<<"\n\t";textcolor(6);cprintf(" MENU FOR QUEUE OPERATION");
cout<<"\n\t";textcolor(8); cprintf("--------------------------"); cout<<"\n\t";textcolor(9);cprintf("1.Addition To Array Queue");
cout<<"\n\t";textcolor(10);cprintf("2.Deletion From Array Queue"); cout<<"\n\t";textcolor(14);cprintf("3.Display Of Array Queue");
cout<<"\n\t";textcolor(5);cprintf("4.Exit from Menu"); textcolor(15);
cout<<"\n\n\tEnter Your choice from above "; cin >> choice; cout<<"\n-------------------------------------------------------------------------";
switch (choice) { case 1:
do
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[77]
Page-77
{ cout << "\n\tEnter the value to be added in the queue ";
cin >> val; add_Q(queue, front, val, rear);
cout << "\n\tDo you want to add more element <Y/N>? "; cin >> opt;
} while (toupper(opt) == 'Y'); clrscr();
break; case 2:
opt = 'Y'; // Initialize for the second loop do
{ val = del_Q(queue, front, rear); if (val != -1)
cout << "\n\tValue deleted from Queue is " << val; cout << "\n\tDo you want to delete more element <Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y'); clrscr();
break; case 3:
do { show_Q(queue, front, rear);
cout<<"\n\tDo you want to see for more time? <Y/N> "; cin >> opt; } while(toupper(opt) == 'Y');
clrscr(); break;
case 4: exit(0);
} }while (choice != 4);
}
// Function body to add circular queue with array of character void add_Q(char queue[], int front, char val, int &rear)
{ if ((rear + 1) % MAX == front) { cout<<"\n\tQUEUE FULL !!"; }
else { rear = (rear + 1) % MAX;
queue[rear] = val; }
} // Function body to delete circular queue with array of character
char del_Q(char queue[], int &front, int rear) { char value;
if (front == rear) { cout<<"\n\tQUEUE EMPTY !!";
value = -1;
} else
{ front = (front + 1) % MAX; value = queue[front];
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[78]
Page-78
}
return (value); }
// Function body to show circular queue with array void show_Q(char queue[], int front, int rear)
{ cout<<"\n\tThe Current Status of Queue is: "; do
{ front = (front + 1) % MAX; cout << "\n\t\t" << queue[front];
}while(front != rear); }
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[79]
Page-79
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[80]
Page-80
DATE :
OBJECT � Write and show the execution of a C++ program to perform all basic operations on DEQUEUE, which sores information as string of
characters.
SOURCE CODE ����
/*basic operations on dequeue, which sotres information as string*/ #include<iostream.h>
#include<conio.h> #include<process.h>
class stud //CLASS DECLARATION { char name[20]; stud *rear, *front;
public: void add_end();
void add_front(); void del_front();
void del_end(); void display();
stud *link;
stud() { front = NULL;
rear=NULL; //CONSTRUCTOR }
}; void stud::add_end() //FUNCTION TO ADD IN THE END OF QUEUE
{ stud *temp; if(rear==NULL)
{ rear=new stud; cout<<"\n\tEnter the name: "; cin>>rear->name;
rear->link=NULL; front=rear;
} else
{ temp=new stud;
cout<<"\n\tEnter the name: "; cin>>temp->name; temp->link=NULL;
rear->link=temp; rear=temp;
} }
void stud::add_front() //FUNCTION TO ADD IN THE FRONT OF THE QUEUE { stud *temp;
if(front==NULL) { front=new stud;
cout<<"\n\tEnter the name: "; cin>>front->name; front->link=NULL;
rear = front;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[81]
Page-81
}
else { temp=new stud;
cout<<"\n\tEnter the name: "; cin>>temp->name; temp->link=NULL;
temp->link=front; front = temp;
} }
void stud::del_front()//FUNCTION TO DELETE FROM THE FRONT OF THE QUEUE { stud *temp;
if(front==NULL) cout<<"\n\tCIRCULAR QUEUE IS EMPTY !!";
else { temp=new stud;
temp=front;
front=front->link; temp->link=NULL;
delete(temp); }
} void stud::del_end()//FUNCTION TO DELETE FROM THE END OF THE QUEUE
{ stud *temp,*back; if(rear==NULL)
cout <<"\n\tCIRCULAR QUEUE IS EMPTY !!"; else
{ temp = front; while (temp->link != NULL)
{ back = temp; temp = temp->link;
}
back->link = NULL; rear = back;
delete(temp); }
} void stud::display()//FUNCTION TO DISPLAY THE QUEUE
{ stud *temp; if(front==NULL)
cout<<"\n\tCIRCULAR QUEUE IS EMPTY !!"; else
{ cout<<"\n\tStatus of Circular Queue"; cout<<"\n\t------------------------";
temp=new stud; temp=front;
while(temp->link!=NULL)
{ cout<<"\n\t\t"<<temp->name;//DISPLAYS THE NODE DATA temp=temp->link; //TO POINT TO NEXT NODE
} cout<<"\n\t\t"<<temp->name;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[82]
Page-82
}
} void main() //MAIN PROGRAM
{ textcolor(14); stud x; int ch; char choi='y'; while((choi=='y')||(choi=='Y'))
{ clrscr(); cout<<"\t\t.______________________.\n";
cout<<"\t\t";textcolor(12);cprintf("| CREATION OF A. NASRA |");cout<<"\n"; cout<<"\t\t*~~~~~~~~~~~~~~~~~~~~~~*\n";
cout<<"\n\t";textcolor(11);cprintf(" MENU FOR CIRCULAR QUEUE OPERATION");
cout<<"\n\t";textcolor(14);cprintf("---------------------------------------"); cout<<"\n\t";textcolor(10);cprintf("1.Addition To Front Of Circular Queue");
cout<<"\n\t";textcolor(10);cprintf("2.Addition To Rear Of Circular Queue"); cout<<"\n\t";textcolor(13);cprintf("3.Deletion From Front Of Circular Queue");
cout<<"\n\t";textcolor(13);cprintf("4.Deletion From Rear Of Circular Queue");
cout<<"\n\t";textcolor(9); cprintf("5.Exit from Menu"); cout<<"\n\n\tEnter Your choice from above "; cin >> ch;
textcolor(14); cout<<"\n----------------------------------------------------------------------------";
switch(ch) //STATEMENT TO REACH THE REQUIRED INSERTION { case 1: x.add_front(); break;
case 2 : x.add_end(); break; case 3 : x.del_front(); break;
case 4 : x.del_end(); break; case 5 : exit(0); break;
default: cout<<"\n\tInvalid choice "; }
x.display(); cout<<"\n\n\tDo U want to continue (y/n)... "; cin>>choi;
} clrscr();
OUTPUT ����
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[83]
Page-83
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[84]
Page-84
POSSIBLE ERRORS AND PRECAUTIONS ���� 1. The string functions must be defined globally.
2. Comments must be given at appropriate places, so that program may be refined and developed afterwards in future.
3. In a big program, there may be typing errors. So typing must be done carefully.
4. Semantic errors must be avoided by doing thoughtful logical programming.
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[85]
Page-85
NOTE: All SQL Commands are according to the
DATE :
OBJECT � Write SQL commands for (i) to (vii) along with the output based on
the relation SPORTS
SPORTS
Student_No Class Name Game1 Grade1 Game2 Grade
10system 7 Sameer Cricket B Swimming A
11 8 Sujita Tennis A Skating C
12 7 Kamal Swimming B Football B
13 7 Veena Tennis C Tennis A
14 9 Archana Basketball A Cricket A
15 10 Arpita Cricket A Atheletics C
(i) Display the names of the students who have grade ‘C’ in either Game1or Game2 or both.
(ii) Display the number of students getting grade ‘A’ in Cricket. (iii) Display the names of the students who have same game for both
Game1 and Game2. (iv) Display the games taken up by the students, whose name starts with ‘A’
Add a new column named ‘Marks’.
(v) Assign a value 200 for Marks for all those who are getting grade ‘B’ or Grade ‘B’ or Grade ‘A’ in both Game1 and Game2.
(vi) Arrange the whole table in the alphabetical order of Name.
SQL COMMANDS �
(i) SELECT Name FROM SPORTS WHERE Grade = ‘C’ OR Grade1 = ‘C’ ; (ii) SELECT COUNT (*) FROM SPORTS WHERE (Game1 = ‘Cricket’ AND Grade = ‘A’) ; OR (Game2 = ‘Cricket’ AND Grade1 = ‘A’) ; (iii) SELECT Name FROM Sports WHERE Game1 = Game2 ; (iv) SELECT Game1, Game2 FROM SPORTS WHERE Name LIKE ‘A%’ ; (v) ALTER TABLE SPORTS ADD(Marks NUMBER(3)) ; (vi) UPDATE SPORTS SET Marks =200 WHERE Grade = ‘A’ OR Grade = ‘B’ OR grade1 = ‘B’ ; (vii) SELECT *FROM SPORTS ORDER BY Name;
OUTPUT �
(i)
Name
Sujita
Veena
(ii)
COUNT(*)
1
(iii)
Name
Veena
(iv)
Game1 Game2
Basketball Cricket
Cricket Atheletics
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[86]
Page-86
(v)
Student_No Class Name Game1 Grade1 Game2 Grade Marks
10 7 Sameer Cricket B Swimming A -
11 8 Sujita Tennis A Skating C -
12 7 Kamal Swimming B Foolball B -
13 7 Veena Tennis C Tennis A -
14 9 Archana Basketball A Cricket A -
15 10 Arpita Cricket - Atheletics - -
(vi)
Studnet_No Class Name Game1 Grade1 Game2 Grade Marks
10 7 Sameer Cricket B Swimming A 200
11 8 Sujita Tennis A Skating C -
12 7 Kamal Swimming B Foolball B 200
13 7 Veena Tennis C Tennis A 200
14 9 Archana Basketball A Cricket A 200
15 10 Arpita Cricket - Atheletics - -
(vi)
Student_No Class Name Game1 Grade1 Game2 Grade Marks
14 9 Archana Basketball A Cricket A 200
15 10 Arpita Cricket - Atheletics - -
12 7 Kamal Swimming B Foolball B 200
10 7 Sameer Cricket B Swimming A 200
11 8 Sujita Tennis A Skating C -
13 7 Veena Tennis C Tennis A 200
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[87]
Page-87
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation LAB
LAB
No ItemName CostPerItem Quantity DateOfPurchase Warranty Operational
1 Computer 60000 9 21-MAY-1996 2 7
2 Printer 15000 3 21-MAY-1997 4 2
3 Scanner 18000 1 29-AUG-1998 3 1
4 Camera 21000 2 13-JUN-1996 1 2
5 Hub 8000 1 31-OCT-1999 2 1
6 UPS 5000 5 21-MAY-1996 1 4
7 Plotter 25000 2 11-JAN-2000 2 2
(a) To select the ItemName purchased after 31.10.1997 (b) To list the ItemName, which are within the Warranty period is more than or
equal to 2 years. (c) To list the ItemName in ascending order fo the date of purchase where
quantity is more than 3. (d) To count the number of items whose cost is more than 10000.
(e) To insert a new record in the LAB table with the following data: 8, ‘VCR’, 10000, 2, 02.02.2000, 1, 2
SQL COMMANDS �
(a) SELECT ItemName FROM LAB
WHERE DateOfPurchase> '31Oct1997'; (b) SELECT ItemName, CostPerItem, Quantity FROM LAB
WHERE warrranty >= 2; (c) SELECT Itemname FROM LAB
WHERE Quantity > 3 ORDER BY DateOfPurchase ;
(d) SELECT COUNT(*) FROM LAB WHERE CostPerItem > 10000 ; (e) INSERT INTO LAB VALUES(8, 'VCR', 10000, 2, '02Feb2000', 1, 2) ;
OUTPUT �
(a)
ItemName
Printer
Hub
Plotter
(b)
ItemName CostPerItem Quantity
Computer 60000 9
Printer 15000 3
Hub 8000 5
Plotter 25000 2
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[88]
Page-88
(d)
COUNT(*)
4
(c)
ItemName
Computer
UPS
Camera
Hub
(e)
NO ItemName CostPerItem Quantity DateOfPurchase Warrranty Operational
1 Computer 60000 9 21-MAY-96 2 7
2 Printer 15000 3 29-AUG-98 3 1
4 Camera 21000 4 13-JUN-96 1 2
5 Hub 8000 5 31-OCT-99 2 1
6 UPS 5000 5 21-MAY-96 1 4
7 Plotter 25000 2 11-JAN-00 2 2
8 VCR 10000 2 02-FEB-00 1 2
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[89]
Page-89
DATE :
OBJECT � Write SQL commands for (a) to (e) along with the output based on the relation INTERIORS
INTERIORS
No ItemName Type DateOfStock Price Discount
1 Red rose Double bed 23-FEB-02 32000 15
2 Soft touch Baby cot 20-JAN-02 9000 10
3 Jerry’s home Baby cot 19-FEB-02 8500 10
4 Rough wood Office table 01-JAN-02 20000 20
5 Comfort zone Double bed 12-JAN-02 15000 20
6 Jerry look Baby cot 24-FEB-02 7000 19
7 Lion king Office table 20-FEB-02 16000 20
8 Royal tiger Sofa 22-FEB-02 30000 25
9 Park sitting Sofa 13-DEC-01 9000 15
10 Dine paradise Dining table 23-MAR-03 11000 15
11 White wood Double bed 23-MAR-03 20000 20
12 James 007 Sofa 20-FEB-03 15000 15
13 Tom look Baby cot 21-FEB-03 7000 10
(a) To show all information about the Sofa.
(b) To list the ItemName which are priced at more than 10000.
(c) To list ItemName and Type of those items, in which DateOfStock is before 22.01.02 in descending order of ItemName.
(d) To display ItemName and DateOfStock of those items, in which the discount percentage is more than 15.
(e) To count the number of items, whose type is ‘Bouble bed’.
SQL COMMANDS � (a) SELECT * FROM INTERIORS
WHERE Type = ‘Sofa’ ; (b) SELECT ItemName FROM INTERIORS WHERE Price > 10000 ;
(c) SELECT ItemName, Type FROM INTERIORS WHERE Date_Of_Stock < ’22-JAN-02’ ;
ORDER BY ItemName DESC ;
(d) SELECT ItemName, DateOfStock FROM INTERIORS WHERE Discount > 15 ;
(e) SELECT COUNT(Type) FROM INTERIORS WHERE Type = ‘Double bed’ ;
OUTPUT �
(a)
No ItemName Type Date_Of_Stock Price Discount
8 Royal tiger Sofa 22-FEB-02 30000 25
9 Park sitting Sofa 13-DEC-01 9000 15
12 James 007 Sofa 20-FEB-03 15000 15
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[90]
Page-90
(b)
ItemName
Red rose
Rouhg wood
Lion king
Comfort zone
Royal tiger
Dine paradise
White wood
James 007
(c)
ItemName Type
Soft touch Baby cot
Rouhg wood Office table
Park sitting Sofa
Comfort zone Double bed
(d)
ItemName Date_Of_Stock
Rouhg wood 01-JAN-02
Lion king 20-FEB-02
Jerry look 24-FEB-02
Comfort zone 12-JAN-02
Royal tiger 22-FEB-02
White wood 23-MAR-03
(e)
COUNT(Type)
3
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[91]
Page-91
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation LIBRARY
LIBRARY
BookId BookName AuthorName Publishers Price Type Quantity
C001 Fast Cook Lata Kapoor EPB 355 Cookery 5 F001 The Tears William Hopkins First Publ. 650 Fiction 20
T001 My First C++ Brain & Brooke EPB 350 Text 10 T002 C++ Brainworks A. W. Rossaine TDH 350 Text 15
F002 Thunderbolts Anna Robets First Publ. 750 Fiction 50
ISSUED
BookId QuantityIssued
T001 4
C001 5
F001 2
(a) To show BookName, AuthorName and PriceOfBook of First Publi. Publishers. (b) To list he names from books of Text type.
(c) To display the names and price from books in ascending order of their price. (d) To increase the price of all books of EPB Publishers by 50 (e) To display the BookId, BookNmae and QunatityIssued for all books which
have been issued. (The query will require contents from both the tables.)
(f) To insert a new row in the table ISSUED having the following data: ‘F0003’, 1
SQL COMMANDS �
(a) SELECT BookName, AuthorName, Price FROM LIBRARY WHERE Publisher = ‘First Publ.’ ;
(b) SELECT BookName FROM LIBRARY WHERE Type = ‘Text’ ;
(c) SELECT BookName, Price FROM LIBRARY ORDER BY Price ;
(d) UPDATE LIBRARY SET Price = Price + 50
WHERE Publishers = ‘EPB’ ; (e) SELECT a.BookId, a.BookName, b.QuantityIssued
FROM LIBRARY a, ISSUED b WHERE a.BookId = b.BookId ;
OUTPUR �
(a)
BookName AuthorName Price
The Tears William Hopkins 650
(b)
BookName
My First C++
C++ Brainworks
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[92]
Page-92
(c)
BookName Price
My First C++ 350
C++ Brainworks 350
Fast Cook 355
The Tears 650
Thunderbolts 750
(d)
BookId BookName AuthorName Publisher Price Type Quantity
C001 Fast Cook Lata Kapoor EPB 405 Cookery 5
F001 The Tears William Hopkins First Publ. 650 Fiction 20
T001 My First C++ Brain & Brooke EPB 400 Text 10
T002 C++ Brainworks A. W. Rossaine TDH 350 Text 15
F002 Thunderbolts Anna Robets First Publ. 750 Fiction 50
(e)
BookId BookName QuantityIssued
C001 Fast Cook 5
F001 The Tears 2
T001 My First C++ 4
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[93]
Page-93
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation SCHOOLBUS
SCHOOLBUS
RtNo AreaCovered Capacity NoOfStudent Distance Transporter Charges
1 Vasant Kunj 120 120 10 Shivam Travels 100000
2 Hauz Khas 80 80 10 Anand Travels 85000
3 Pitampura 60 55 30 Anand Travels 60000
4 Rohini 100 90 35 Anand Travels 100000
5 Yamuna Vihar 50 60 20 Bhalla Co. 55000
6 Kirshna Nagar 70 80 30 Yadav Co. 80000
7 Vasundhara 100 110 20 Yadav Co. 100000
8 Paschim Vihar 40 40 20 Speed Travels 55000
9 Saket 120 120 10 Speed Travels 100000
10 Janak Puri 100 100 20 Kisan Tours 95000
(a) To show all records students more than the capacity in order of RtNo.
(b) To show AreaCovered for buses covering more than 20 km, but charges less than 80000.
(c) To show Transporter wise total no. of students traveling. (d) To show RtNo, AreaCovered and average cost per student for all routs. (e) Add a new column direvername in the SchoolBus relation.
(f) Add a new record with following data: (11, ‘MotiBagh’, 35, 32, 10, ‘Kishan Tours’, 35000)
SQL COMMANDS �
(a) SELECT * FROM SCHOOLBUS WHERE NoOfStudent > Capacity ORDER BY RtNo ;
(b) SELECT AreaCovered FROM SCHOOLBUS WHERE Distance > 20 AND Charges < 80000 ;
(c) SELECT SUM(NoOfStudent) FROM SCHOOLBUS GROUP BY Transporter ; (d) SELECT RtNo, AreaCovered, Charges, NoOfStudent FROM SCHOOLBUS ;
(e) ALTER TABLE SCHOOLBUS ADD(DriverName CHAR(20)) ; (f) INSERT INTO SCHOOLBUS
VALUES(11, ‘Motihagh’, 35, 32, 10, ‘Kisan Tours’, 35000, ‘David’) ;
OUTPUT �
(a)
RtNo AreaCovered Capacity NoOfStudent Distance Transporter Charges
5 Yamuna Vihar 50 60 20 Bhalla Co. 55000
6 Kirshna Nagar 70 80 30 Yadav Co. 80000
7 Vasundhara 100 110 20 Yadav Co. 100000
(b) AreaCovered
Pitampura
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[94]
Page-94
(c)
SUM(NoOfStudent)
120
225
190
100
60
160
(d)
RtNo AreaCovered Charges NoOfStudent
1 Vasant Kunj 100000 120
2 Hauz Khas 85000 80
3 Pitampura 60000 55
4 Rohini 100000 90
5 Yamuna Vihar 55000 60
6 Kirshna Nagar 80000 80
7 Vasundhara 100000 110
8 Paschim Vihar 55000 40
9 Saket 100000 120
10 Janak Puri 95000 100
(e)
RtNo AreaCovered Capacity NoOfStudent Distance Transporter Charges DriverName
1 Vasant Kunj 120 120 10 Shivam Travels 100000 -
2 Hauz Khas 80 80 10 Anand Travels 85000 -
3 Pitampura 60 55 30 Anand Travels 60000 -
4 Rohini 100 90 35 Anand Travels 100000 -
5 Yamuna Vihar 50 60 20 Bhalla Co. 55000 -
6 Kirshna Nagar 70 80 30 Yadav Co. 80000 -
7 Vasundhara 100 110 20 Yadav Co. 100000 -
8 Paschim Vihar 40 40 20 Speed Travels 55000 -
9 Saket 120 120 10 Speed Travels 100000 -
10 Janak Puri 100 100 20 Kisan Tours 95000 -
(f)
RtNo AreaCovered Capacity NoOfStudent Distance Transporter Charges DriverName
1 Vasant Kunj 120 120 10 Shivam Travels 100000 -
2 Hauz Khas 80 80 10 Anand Travels 85000 -
3 Pitampura 60 55 30 Anand Travels 60000 -
4 Rohini 100 90 35 Anand Travels 100000 -
5 Yamuna Vihar 50 60 20 Bhalla Co. 55000 -
6 Kirshna Nagar 70 80 30 Yadav Co. 80000 -
7 Vasundhara 100 110 20 Yadav Co. 100000 -
8 Paschim Vihar 40 40 20 Speed Travels 55000 -
9 Saket 120 120 10 Speed Travels 100000 -
10 Janak Puri 100 100 20 Kisan Tours 95000 -
11 Motihagh 35 32 10 Kisan Tours 35000 David
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[95]
Page-95
DATE :
OBJECT � Write SQL commands for (a) to (d) along with the output based on the relation EMPLOYEES and EMPSALARY
EMPLOYEES
EMPID FIRSTNAME LASTNAME ADDRESS CITY
010 George Smith 83 First Street Howard
105 Mary Jones 852 Vine Ave. Losantiville
152 Sam Tones 33 Elm St. Paris
215 Sarah Acherman 440 U.S. 110 Upton
244 Manila Sengupta 24 Friends Street New Delhi
300 Rabert Samuel 9 Fifth Cross Washington
335 Hanery Williams 12 Moore Street Boston
400 Rachel Lee 12 Harrson St. New York
441 Peter Thompson 11 Red Road Paris
EMPSALARY
EMPID SALARY BENEFITS DESIGNATION
010 75000 15000 Manager 105 65000 15000 Manager 152 80000 25000 Director
215 75000 12500 Manager 244 50000 12000 Clerk
300 45000 10000 Clerk 335 40000 10000 Clerk
400 32000 7500 Salesman
441 28000 28000 Salesman
(a) To display FIRSTNAM, LASTNAME, ADDRESS and CITY of all employees living in Paris from the table EMPLOYEES.
(b) To display thye contents of EMPLOYEES table in descending order of FIRSTNAME.
(c) To display the RIRSTNAME, LASTNAME, and Total Salary of all Managers from the tables EMPLOYEES and EMPSALARY, where Total Salary is
calculates as Salary + Benefits. (d) To display the maximum salary among MANAGERS and Clerks from the
table EMPSALARY.
SQL COMMANDS �
(a) SELECT FIRSTNAME, LASTNAME, ADDRESS, CITY FROM EMPLOYEES WHERE CITY = ‘Paris’ ;
(b) SELECT FIRSTNAME, LASTNAME, SALARY + BENEFITS FROM EMPLOYEES, EMPSALARY WHERE DESIGNATION = ‘Manager’
AND EMPLOYEES.EMPID = EMPSALARY.EMPID ; (c) SELECT * FROM EMPLOYEES
ORDER BY FIRSTNAME DESC ; (d) SELECT MAX(SALARY) FROM EMPSALARY
WHERE DESIGNATION = ‘Manager’ OR DESIGINATION = ‘Clerk’ ;
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[96]
Page-96
OUTPUT �
(a) FIRSTNAME LASTNAME ADDRESS CITY
Sam Tones 33 Elm St. Paris
Peter Thompson 11 Red Road Paris
(b) FIRSTNAME LASTNAME SALARY+BENEFITS
George Smith 90000
Mary Jones 80000
Sarah Acherman 87500
(c)
ID EMPID FIRSTNAME LASTNAME ADDRESS CITY
4 215 Sarah Acherman 440 U.S. 110 Upton
3 152 Sam Tones 33 Elm St. Paris
8 400 Rachel Lee 12 Harrson St. New York
6 300 Rabert Samuel 9 Fifth Cross Washington
9 441 Peter Thompson 11 Red Road Paris
2 105 Mary Jones 852 Vine Ave. Losantiville
5 244 Manila Sengupta 24 Friends Street New Delhi
7 335 Hanery Williams 12 Moore Street Boston
1 10 George Smith 83 First Street Howard
(d)
MAX(SALARY)
75000
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[97]
Page-97
DATE :
OBJECT � Write SQL commands for (a) to (d) along with the output based on the relation PRODUCT and CLIENT
PRODUCTS
P_ID PNAME COMPANY PRICE
PC01 Personal Computer ABC 35000
LC05 Laptop ABC 55000
PC03 Personal Computer XYZ 32000
PC06 Personal Computer COMP 37000
LC03 Laptop PQR 57000
CLIENTS
C_ID CNAME CITY I_ID
01 N Roy Delhi LC03 06 H Singh Mumbai PC03 12 R Pandey Delhi PC06 15 C Sharma Delhi LC03
16 K Agrawal Bangalore PC01
(a) To display the details of those Customers whose city is Delhi.
(b) To display the details of Items whose Price is in the range of 35000 to 55000 (Both values inclusive)
(c) To display the CustomerName, City from table CLIENTS and Itemname and Price from table PRODUCTS, with their corresponding matching I_ID.
(d) To increase the Price of all items by 1000 in the table Item.
SQL COMMANDS �
(a) SELECT * FROM CLIENTs WHERE CITY = ‘Delhi’ ; (b) SELECT * FROM PRODUCTS WHERE PRICE BETWEEN 35000 AND 55000 ;
(C) SELECT A.PNAME, A.PRICE, B.CNAME, B.CITY FROM PRODUCTS A, CLIENTS B WHERE A.P_ID = B.I_ID ;
(d) UPDATE PRODUCTS SET PRICE = PRICE + 1000 ;
OUTPUT �
(a)-
C_ID CNAME CITY I_ID
1 N Roy Delhi LC03
3 R Pandey Delhi PC06
4 C Sharma Delhi LC03
(b)
P_ID PNAME COMPANY PRICE
1 Personal Computer ABC 35000
2 Laptop ABC 55000
4 Personal Computer COMP 37000
(C) No data found
(D)
P_ID PNAME COMPANY PRICE
1 Personal Computer ABC 36000
2 Laptop ABC 56000
3 Personal Computer XYZ 33000
4 Personal Computer COMP 38000
5 Laptop PQR 58000
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[98]
Page-98
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation BANKS
BANKS
ACNO CNAME BNAME AMOUNT OPENDATE TRANSFERID
1 Karar Bank Of Baroda 15000 12-JAN-98 10
2 Puneet State Bank 25000 01-FEB-97 09
3 Anirban Oriental Bank 17000 15-JUL-99 05
4 Yatin Standard Charted 38000 10-FEB-99 11
5 Sunny State Bank 47000 06-FEB-98 15
6 Jayant Uco Bank 34000 10-AUG-98 07
7 Nikhil Bank Of Baroda 56000 02-JAN-99 12
8 Tarun Oriental Bank 22000 04-APR-99 08
9 Jisha Uco Bank 34000 05-JAN-98 11
(a) Display data for all Customers whose TRANSFERID between 8 and 11.
(b) Display data for all Customers sorted by their OPENDATE. (c) To count the number of Customers with Amount < 30000.
(d) List the minimum and maximum amount from the BANK. (e) To list CNAME, BNAME, Amount for all the clients whose amount is less
than 20000.
(f) To display ACNO, Cname , Bname, Total TRANSFERID in reverse order of Amount.
SQL COMMANDS �
(a) SELECT * FROM BANKS WHERE TRANSFERID BETWEEN 8 AND 11 ; (b) SELECT * FROM BANKS ORDER BY OPENDATE ;
(c) SELECT COUNT(*) FROM BANKS WHERE AMOUNT < 30000 ; (d) SELECT MIN(AMOUNT), MAX(AMOUNT) FROM BANKS ;
(e) SELECT CNAME, BNAME, AMOUNT FROM BANKS WHERE AMOUNT<20000; (f) SELECT ACNO, CNAME, BNAME, TRANSFERID FROM BANKS
ORDER BY AMOUNT DESC ;
OUTPUT �
(a)
ACNO CNAME BNAME AMOUNT OPENDATE TRANSFERID
1 Karar Bank Of Baroda 15000 12-JAN-98 10
2 Puneet State Bank 25000 01-FEB-97 9
4 Yatin Standard Charted 38000 10-FEB-99 11
8 Tarun Oriental Bank 22000 04-APR-99 8
9 Jisha Uco Bank 34000 05-JAN-98 11
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[99]
Page-99
(b)
ACNO CNAME BNAME AMOUNT OPENDATE TRANSFERID
2 Puneet State Bank 25000 01-FEB-97 9
9 Jisha Uco Bank 34000 05-JAN-98 11
1 Karar Bank Of Baroda 15000 12-JAN-98 10
5 Sunny State Bank 47000 06-FEB-98 15
6 Jayant Uco Bank 34000 10-AUG-98 7
7 Nikhil Bank Of Baroda 56000 02-JAN-99 12
4 Yatin Standard Charted 38000 10-FEB-99 11
8 Tarun Oriental Bank 22000 04-APR-99 8
3 Anirban Oriental Bank 17000 15-JUL-99 5 (c)
COUNT(*)
4
(d)
MIN(AMOUNT) MAX(AMOUNT)
15000 56000 (e)
CNAME BNAME AMOUNT
Karar Bank Of Baroda 15000
Anirban Oriental Bank 17000 (f)
ACNO CNAME BNAME TRANSFERID
7 Nikhil Bank Of Baroda 12
5 Sunny State Bank 15
4 Yatin Standard Charted 11
9 Jisha Uco Bank 11
6 Jayant Uco Bank 7
2 Puneet State Bank 9
8 Tarun Oriental Bank 8
3 Anirban Oriental Bank 5
1 Karar Bank Of Baroda 10
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[100]
Page-100
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation MOVIE
MOVIES
NO TITLE TYPE RATIN STARS QTY PRICE
1 Gone with the wind Drama G Gable 4 39.95
2 Friday the 13th Horror R Jason 2 69.95
3 Top gun Drama PG Cruise 7 49.95
4 Splash Comedy PG13 Hanks 3 29.95
5 Independence Day Drama R Turner 3 19.95
6 Risky Business Comedy R Cruise 2 44.95
7 Cocoon Sci-Fi PG Ameche 2 31.95
8 Crocodile Dundee Comedy PG13 Harris 2 69.95
9 101 Dalmatians Comedy G 3 59.95
10 Tootsie Comedy PG Hoffman 1 29.95
(a) Find the total value of the movie cassettes available in the library.
(b) Display a list of all movies with price over 20 and sorted by price. (c) Display all the movies sorted by Qty in descending order.
(d) Display a report, listing a movie number, current value and replacement value for each movie in the above table. Calculate the replacement value
for all movies as Qty*Price*1.15 (e) Count the number of movies where rating is not ‘G’.
(f) Insert a new movie in the MOVIE table. Fill the entire column with values.
SQL COMMANDS �
(a) SELECT SUM(QTY*PRICE) FROM MOVIES ;
(b) SELECT * FRPOM MOVIES WHERE PRICE > 20 ORDER BY PRICE ;
(c) SELECT * FROM MOVIES ORDER BY QTY DESC ;
(d) SELECT NO, QTY*PRICE, QTY*PRICE*1.15 FROM MOVIES ;
(e) SELECT COUNT(*) FROM MOVIES WHERE RATIN <> ‘G’ ;
(f) INSERT INTO MOVIES VALUES (11, ‘100 Days’, ‘horror’, ‘R’, ‘John’, 6, 43.22) ;
OUTPUT �
(a)
SUM(QTY*PRICE)
1302.55
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[101]
Page-101
(b)
NO TITLE TYPE RATIN STARS QTY PRICE
10 Tootsie Comedy PG Hoffman 1 29.95
4 Splash Comedy PG13 Hanks 3 29.95
7 Cocoon Sci-Fi PG Ameche 2 31.95
1 Gone with the wind Drama G Gable 4 39.95
6 Risky Business Comedy R Cruise 2 44.95
3 Top gun Drama PG Cruise 7 49.95
9 101 Dalmatians Comedy G - 3 59.95
2 Friday the 13th Horror R Jason 2 69.95
8 Crocodile Dundee Comedy PG13 Harris 2 69.95
(c)
NO TITLE TYPE RATIN STARS QTY PRICE
3 Top gun Drama PG Cruise 7 49.95
1 Gone with the wind Drama G Gable 4 39.95
5 Independence Day Drama R Turner 3 19.95
9 101 Dalmatians Comedy G - 3 59.95
4 Splash Comedy PG13 Hanks 3 29.95
6 Risky Business Comedy R Cruise 2 44.95
7 Cocoon Sci-Fi PG Ameche 2 31.95
8 Crocodile Dundee Comedy PG13 Harris 2 69.95
2 Friday the 13th Horror R Jason 2 69.95
10 Tootsie Comedy PG Hoffman 1 29.95
(e)
COUNT(*)
8 (d)
NO QTY*PRICE QTY*PRICE*1.15
1 159.8 183.77
2 139.9 160.885
3 349.65 402.0975
4 89.85 103.3275
5 59.85 68.8275
6 89.9 103.385
7 63.9 73.485
8 139.9 160.885
9 179.85 206.8275
10 29.95 34.4425
(f) NO TITLE TYPE RATIN STARS QTY PRICE
1 Gone with the wind
Drama G Gable 4 39.95
2 Friday the 13th Horror R Jason 2 69.95
3 Top gun Drama PG Cruise 7 49.95
4 Splash Comedy PG13 Hanks 3 29.95
5 Independence Day
Drama R Turner 3 19.95
6 Risky Business Comedy R Cruise 2 44.95
7 Cocoon Sci-Fi PG Ameche 2 31.95
8 Crocodile Dundee
Comedy PG13 Harris 2 69.95
9 101 Dalmatians Comedy G - 3 59.95
10 Tootsie Comedy PG Hoffman 1 29.95
11 100 Days horror R John 6 43.22
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[102]
Page-102
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation PRODUCT.
COMP_PRODUCT
NO NAME PRICE SUPPLIER STOCK
1 Motherboard 7000 Intel 20
2 Keyboard 1000 Tvse 70
3 Mouse 500 Logitech 60
4 Soundcard 600 Samsung 50
5 Speaker 600 Samsung 25
6 Monitor 3000 Philips 22
7 CD-ROM 2800 Ntech 32
8 Printer 7900 HP 10
(a) Display data for the entire item sorted by their name (b) Display the Name and Price from the table item in reverse order of their
stock. (c) List all Name and Price with Price between 3000 and 7000.
(d) Write the command to set the price field of all products to 1200 corresponding to NAME = ‘Keyboard’.
(e) To delete rows with stock between 20 to 40. (f) To count the number of products with stock less than 5.
SQL COMMANDS �
(a) SELECT * FROM COMP_PRODUCT ORDER BY Name ;
(b) SELECT NAME, PRICE ORDER BY STOCK DESC ; (c) SELECT NAME, PRICE FROM COMP_PRODUCT
WHERE PRICE BETWEEN 3000 AND 7000; (d) UPDATE COMP_PRODUCT SET PRICE = 1200 WHERE NAME = ‘Keyboard’ ;
(e) SELECT COUNT(STOCK) FROM COMP_PRODUCT WHERE STOCK < 5 ; (f) DELETE FROM COMP_PRODUCT WHERE STOCK BETWEEN 20 AND 40 ;
OUTPUT �
(a)
NO NAME PRICE SUPPLIER STOCK
7 CD-ROM 2800 Ntech 32
2 Keyboard 1000 Tvse 70
6 Monitor 3000 Philips 22
1 Motherboard 7000 Intel 20
3 Mouse 500 Logitech 60
8 Printer 7900 HP 10
4 Soundcard 600 Samsung 50
5 Speaker 600 Samsung 25
(b)
NAME PRICE
Keyboard 1000
Mouse 500
Soundcard 600
CD-ROM 2800
Speaker 600
Monitor 3000
Motherboard 7000
Printer 7900
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[103]
Page-103
(c)
NAME PRICE
Motherboard 7000
Monitor 3000
(d)
NO NAME PRICE SUPPLIER STOCK
1 Motherboard 7000 Intel 20
2 Keyboard 1200 Tvse 70
3 Mouse 500 Logitech 60
4 Soundcard 600 Samsung 50
5 Speaker 600 Samsung 25
6 Monitor 3000 Philips 22
7 CD-ROM 2800 Ntech 32
8 Printer 7900 HP 10
(e)
COUNT(STOCK)
0
(f) 4 Rows deleted, hence current table is:
NO NAME PRICE SUPPLIER STOCK
2 Keyboard 1200 Tvse 70
3 Mouse 500 Logitech 60
4 Soundcard 600 Samsung 50
8 Printer 7900 HP 10
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[104]
Page-104
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation SHOP.
SHOP
NO SHOPNAME SALE AREA CUST_PERCENT GRADE CITY
1 S.M. Sons 250000 West 68.6 C Delhi
2 Dharohar 500000 South 81.8 A Mumbai
3 Driti Art 300000 North 79.8 B Kolkota
4 Ripple 380000 North 88.0 B Mimbai
5 Biswas Sotre 456000 East 92.0 A Delhi
6 crystal 290000 South 66.7 A Kolkota
(a) To display the names of all shops which are in the area South. (b) To display name and Customer Percentage of all the shops having
cust_percent > 80. (c) To display list of all the shops with sale > 300000 in ascending order of
shop_name. (d) To display a report with Shop_name, Area and GRADE for each shop in table,
for only those shops whose sale is between 350000 and 400000 (including both values)
(e) To display the City and the number of shops in each city in the table SHOP. (f) To insert details of a new shop in the table SHOP with the following data: 7, The Shop, 550000, South, 90.8, A, Ahemdabad.
SQL COMMANDS � (a) SELECT SHOPNAME FROM SHOP WHERE AREA = ‘South’ ;
(b) SELECT SHOPNAME, CUST_PERCENT FROM SHOP
WHERE CUST_PERCENT > 80 ; (c) SELECT SHOPNAME FROM SHOP WHERE SALE > 300000
ORDER BY SHOPNAME ASC ; (d) SELECT SHOPNAME, AREA, GRADE FROM SHOP
WHERE SALE BETWEEN 350000 AND 400000 ; (e) SELECT CITY, COUNT(DISTINCT SHOPNAME)
FROM SHOP GROUP BY CITY ; (f) INSERT INTO SHOP
VALUES(7, ‘The Shop’, 550000, ‘South’, 90.8, ‘A’, ‘Ahemdabad’ ;
OUTPUT �
(a)
SHOPNAME
Dharohar
crystal
(b)
SHOPNAME CUST_PERCENT
Dharohar 81.8
Ripple 88
Biswas Sotre 92
(c)
SHOPNAME
Biswas Sotre
Dharohar
Ripple
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[105]
Page-105
(d)
SHOPNAME AREA GRADE
Ripple North B
(e)
CITY COUNT(DISTINCT SHOPNAME)
Delhi 2
Kolkota 2
Mimbai 1
Mumbai 1
(f) One row inserted, hence now the table is:
NO SHOPNAME SALE AREA CUST_PERCENT GRADE CITY
1 S.M. Sons 250000 West 68.6 C Delhi
2 Dharohar 500000 South 81.8 A Mumbai
3 Driti Art 300000 North 79.8 B Kolkota
4 Ripple 380000 North 88 B Mimbai
5 Biswas Sotre 456000 East 92 A Delhi
6 crystal 290000 South 66.7 A Kolkota
7 The Shop 550000 South 90.8 A Ahemdabad
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[106]
Page-106
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation UNI_STUDNET.
UNI_STUDENT
NO NAME STIPEND STREAM AVGMARK GRADE CLASS
1 Neha 450.00 Medical 89.2 A 11C
2 Damini 400.00 Commerce 78.5 B 12B
3 Gaurav 250.00 Humanities 64.4 C 11A
4 Anu 300.00 Commerce 67.5 C 12B
5 Vikas 500.00 Non-medical 92.0 A 12A
6 Rubina 450.00 Non-medical 88.5 A 12A
(a) To display the names of all students who are in Medical stream. (b) To display name and average of all the students having AvgMark < 70.0
(c) To display list of all the students with Stipend > 350.00 in ascending order of Name.
(d) To display a report with Name, Marks for each student in the table. Marks are calculated as AvgMark*5.
(e) To display the Stream and the number of students in each Stream in the table STUDENT.
(f) To insert a new student in the table STUDENT with the following data: 7, Radhika, 500.00, Commerce, 90.8, A, 12A
SQL COMMANDS �
(a) SELECT NAME FROM UNI_STUDENT
WHERE STREAM = ‘Medical’ ; (b) SELECT NAME, AVGMARK FROM UNI_STUDENT
WHERE AVGMARK < 70.0 ; (c) SELECT * FROM UNI_STUDENT
WHERE STIPEND > 35.0 ORDER BY NAME ASC ; (d) SELECT NAME, AVGMARK*5 FROM UNI_STUDENT ;
(e) SELECT STREAM, COUNT(STREAM) FROM UNI_STUDENT GROUP BY STREAM ;
(f) INSERT INTO UNI_STUDENT VALUES(7, ‘Radhika’, 500.00, ‘Commerce’, 90.8, ‘A’, ‘12A’) ;
OUTPUT �
(a)
NAME
Neha
(b)
NAME AVGMARK
Gaurav 64.4
Anu 67.5
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[107]
Page-107
(C)
NO NAME STIPEND STREAM AVGMARK GRADE CLASS
4 Anu 300 Commerce 67.5 C 12B
2 Damini 400 Commerce 78.5 B 12B
3 Gaurav 250 Humanities 64.4 C 11A
1 Neha 450 Medical 89.2 A 11C
6 Rubina 450 Non-medical 88.5 A 12A
5 Vikas 500 Non-medical 92 A 12A
(d)
NAME AVGMARK*5
Neha 446
Damini 392.5
Gaurav 322
Anu 337.5
Vikas 460
Rubina 442.5
(e)
STREAM COUNT(STREAM)
Commerce 2
Humanities 1
Medical 1
Non-medical 2
(f) 1 Row inserted. Hence the updated table is:
NO NAME STIPEND STREAM AVGMARK GRADE CLASS
1 Neha 450 Medical 89.2 A 11C
2 Damini 400 Commerce 78.5 B 12B
3 Gaurav 250 Humanities 64.4 C 11A
4 Anu 300 Commerce 67.5 C 12B
5 Vikas 500 Non-medical 92 A 12A
6 Rubina 450 Non-medical 88.5 A 12A
7 Radhika 500 Commerce 90.8 A 12A
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[108]
Page-108
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation EMPLOYEE.
EMPLOYEES
NO NAME SALARY AREA AGE GRADE DEPT
1 Neha 40000 West 45 C Civil
2 Damini 35000 South 38 A Elec
3 Gaurav 60000 North 52 B Civil
4 Anu 38000 North 29 B Civil
5 Vikas 42000 East 35 A Comp
6 Rubina 29000 South 34 A Mech
(a) To display the names of all employees who are in the area South. (b) To display name and age of all employees having age > 40.
(c) To display list of all employees whose salary >= 30000 and <= 40000. (d) To display the list department wise.
(e) To display the employee names in descending order of age. (f) To insert a new row with the following data: 7, Tarik, 45000, South, 45, C, Elec
SQL COMMANDS � (a) SELECT NAME FROM EMPLOYEE WHERE AREA = ‘South’ ;
(b) SELECT NAME, AGE FROM EMPLOYEE WHERE AGE >40 ;
(c) SELECT * FROM EMPLOYEE WHERE SALARY IS BETWEEN 30000 AND 40000 ;
(d) SELECT * FROM EMPLOYEE ORDER BY DEPT ; (e) SELECT NAME FROM EMPLOYEE ORDER BY AGE DESC ;
(f) INSERT INTO EMPLOYEE VALUES(7, ‘Tarik’, 45000, ‘South’ , 45, ‘C’, ‘Elec’) ;
OUTPUT �
(a) NAME
Damini
Rubina
(b) NAME AGE
Neha 45
Gaurav 52 (C)
NO NAME SALARY AREA AGE GRADE DEPT
1 Neha 40000 West 45 C Civil
2 Damini 35000 South 38 A Elec
4 Anu 38000 North 29 B Civil
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[109]
Page-109
(d)
NO NAME SALARY AREA AGE GRADE DEPT
1 Neha 40000 West 45 C Civil
3 Gaurav 60000 North 52 B Civil
4 Anu 38000 North 29 B Civil
5 Vikas 42000 East 35 A Comp
2 Damini 35000 South 38 A Elec
6 Rubina 29000 South 34 A Mech
(e)
NAME
Gaurav
Neha
Damini
Vikas
Rubina
Anu
(f) 1 Row inserted, so updated table is:
NO NAME SALARY AREA AGE GRADE DEPT
1 Neha 40000 West 45 C Civil
2 Damini 35000 South 38 A Elec
3 Gaurav 60000 North 52 B Civil
4 Anu 38000 North 29 B Civil
5 Vikas 42000 East 35 A Comp
6 Rubina 29000 South 34 A Mech
7 Tarik 45000 South 45 C Elec
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[110]
Page-110
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation B_STUDNET.
B_STUDENT
NO NAME AGE DEPT AD_DATE FEE SEX
1 Pankaj 24 Computer 10-JAN-97 120 M
2 Shalini 21 History 24-MAR-98 200 F
3 Sanjay 22 Hindi 12-DEC-96 300 M
4 Sudha 25 History 01-JUL-99 400 F
5 Rakesh 22 Hindi 05-AUG-97 250 M
6 Shakeel 30 History 27-JUN-98 300 M
7 Surya 34 Computer 25-FEB-97 210 M
8 Shikha 23 Hindi 31-JUL-97 200 F
(a) To show all information about the students of History dept.
(b) To list the names of female students who are in Hindi dept.
(c) To list names of all students with their date of admission in ascending order.
(d) To display student’s name, fee, age for the male students only. (e) To count the number of students with Age < 23.
(f) To insert a new row in the STUDENT table with the following data: 9, Zaheer, 36, Computer, 12/03/95, 230, M.
SQL COMMANDS �
(a) SELECT * FROM B_STUDENT WHERE DEPT = ‘History’ ; (b) SELECT NAME FROM B_STUDENT WHERE SEX = ‘F’ ;
(c) SELECT NAME, AD_DATE FROM B_STUDENT ORDER BY AD_DATE DESC ; (d) SELECT NAME, FEE, AGE FROM B_STUDENT WHERE SEX = ‘M’ ;
(e) SELECT COUNT(*) FROM B_STUDENT WHERE AGE < 23 ; (f) INSERT INTO B_STUDENT
VALUES(9, ‘Zaheer’, 36, ‘Computer’, ’12-MAR-95’, 230, ‘M’ ) ;
OUTPUT �
(a)
NO NAME AGE DEPT AD_DATE FEE SEX
2 Shalini 21 History 24-MAR-98 200 F
4 Sudha 25 History 01-JUL-99 400 F
6 Shakeel 30 History 27-JUN-98 300 M
(b)
NAME
Shalini
Sudha
Shikha
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[111]
Page-111
(c)
NAME AD_DATE
Sudha 01-JUL-99
Shakeel 27-JUN-98
Shalini 24-MAR-98
Rakesh 05-AUG-97
Shikha 31-JUL-97
Surya 25-FEB-97
Pankaj 10-JAN-97
Sanjay 12-DEC-96
(d)
NAME FEE AGE
Pankaj 120 24
Sanjay 300 22
Rakesh 250 22
Shakeel 300 30
Surya 210 3
(e)
COUNT(*)
3
(f) 1 Row insert, hence updated table is:
NO NAME AGE DEPT AD_DATE FEE SEX
1 Pankaj 24 Computer 10-JAN-97 120 M
2 Shalini 21 History 24-MAR-98 200 F
3 Sanjay 22 Hindi 12-DEC-96 300 M
4 Sudha 25 History 01-JUL-99 400 F
5 Rakesh 22 Hindi 05-AUG-97 250 M
6 Shakeel 30 History 27-JUN-98 300 M
7 Surya 34 Computer 25-FEB-97 210 M
8 Shikha 23 Hindi 31-JUL-97 200 F
9 Zaheer 36 Computer 12-MAR-95 230 M
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[112]
Page-112
DATE :
OBJECT � Write SQL commands for (a) to (f) along with the output based on the relation HOSPITAL.
HOSPITAL
NO NAME AGE DEPORTMENT AD_DATE CHARGE SEX
1 Arpit 62 Surgery 21-JAN-98 200 M
2 Zarina 22 ENT 12-DEC-97 250 F
3 Lareema 32 Orthopedic 19-FEB-98 200 M
4 Arun 12 Surgery 11-JAN-98 300 M
5 Zubin 30 ENT 12-JAN-98 250 M
6 Ketaki 16 ENT 24-FEB-98 250 F
7 Ankita 29 Cardiology 20-FEB-98 800 F
8 Zareen 45 Gynaecology 22-FEB-98 300 F
9 Sush 19 Cardiology 13-JAN-98 800 M
10 Shilpa 23 Nuclear Medicine 21-FEB-98 400 F
(a) To show all information about the patients of cardiology department.
(b) To list the names of female patients who are in ENT department. (c) To list names of all patients with their date of admission in ascending
order. (d) To display Patients’ Name, Charges, Age for female patients only.
(e) To count the number of patients with AGE <30. (f) To insert a new row in the hospital table with the following data:
11. Aftan. 24. Sirgeru. 25/02/98, 300, M.
SQL COMMANDS � (a) SELECT * FROM HOSPITAL WHERE DEPORTMENT = ‘Cardiology’ ;
(b) SELECT NAME FROM HOSPITAL WHERE DEPORTMENT = ‘ENT’ AND SEX = ‘F’ ; (c) SELECT NAME, AD_DATE FROM HOSPITAL ORDER BY AD_DATE ASC ;
(d) SELECT NAME, CHARGE, AGE WHERE SEX = ‘F’ ; (e) SELECT COUNT(*) WHERE AGE < 30 ;
(f) INSERT INTO VALUES(11, ‘Aftan’, 24, ‘Sirgeru’, ’25-FEB-98’, 300, ‘M’) ;
OUTPUT �
(a)
NO NAME AGE DEPORTMENT AD_DATE CHARGE SEX
7 Ankita 29 Cardiology 20-FEB-98 800 F
9 Sush 19 Cardiology 13-JAN-98 800 M
(b)
NAME
Zarina
Ketaki
PRACTICAL FILE PRICTICAL FILE ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
This tutorial is written by: Er. A. Nasra ---------------------------------------------------------------------------------------------------------------------------------
[113]
Page-113
(c)
NAME AD_DATE
Zarina 12-DEC-97
Arun 11-JAN-98
Zubin 12-JAN-98
Sush 13-JAN-98
Arpit 21-JAN-98
Lareema 19-FEB-98
Ankita 20-FEB-98
Shilpa 21-FEB-98
Zareen 22-FEB-98
Ketaki 24-FEB-98
(d)
NAME CHARGE
Zarina 250
Ketaki 250
Ankita 800
Zareen 300
Shilpa 400
(e)
COUNT(*)
6
(f) 1 Row inserted so updated table is:
NO NAME AGE DEPORTMENT AD_DATE CHARGE SEX
1 Arpit 62 Surgery 21-JAN-98 200 M
2 Zarina 22 ENT 12-DEC-97 250 F
3 Lareema 32 Orthopedic 19-FEB-98 200 M
4 Arun 12 Surgery 11-JAN-98 300 M
5 Zubin 30 ENT 12-JAN-98 250 M
6 Ketaki 16 ENT 24-FEB-98 250 F
7 Ankita 29 Cardiology 20-FEB-98 800 F
8 Zareen 45 Gynaecology 22-FEB-98 300 F
9 Sush 19 Cardiology 13-JAN-98 800 M
10 Shilpa 23 Nuclear Medicine 21-FEB-98 400 F
11 Aftan 24 Sirgeru 25-FEB-98 300 M