Professional Documents
Culture Documents
Programs
Programs
2009-2010
ON
C++ PROGRAMS
&
SQL COMMANDS
Sorting
Program to sort the elements of an
array in ascending order by using
Selection Sort method
Program to sort the elements of an
array in ascending order by using
Bubble Sort method
Program to sort the elements of an
array by using Insertion Sort
Program to sort the two arrays(arrA
and arrB) while merging(merge
sort) and the resultant array that we
will get after merging should also be
sorted in ascending order if arrA and
arrB are in ascending order
• Array of Structure
Program to store information of
ten employees by using array
of structure and then display
the information of those
employee(s) that the user want
Program to count ( no . of
Alphabet , no of uppercase
letter, no of lowercase letter,
no of digits, no of spaces, no
of lines.) in a text file
according to user choice.
Binary File
Program to first create the
binary file and then display
a particular record(when
records are implemented by
using objects)
• Default Arguments
Program to calculate the
Simple Interest by making a
function having default
arguments
• Stack and Queue
Stack
Program to perform PUSH
and POP operation in
dynamically allocated stack
Queue
Program to perform
INSERT and DELETE
operations in a dynamically
alloacted queue
SQL COMMANDS
• Create Table
• Alter Table
• Insert Into
• Update
• Order by (With date Type Data)
• IN
• Between
• Count
• Min
• Max
• Like
• Delete
• Drop Table
ONE
DIMENSIONAL
ARRAY
•SEARCHING
•SORTING
PROGRAM NO.1
//Function prototype
int Bsearch(int[ ],int,int);//To search the element in an array by binary search
//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arr[50],range,i,sitem,ipos;
//Display the message for taking the range of an array from the user
cout<<"\n Enter the range(max 50):";
cin>>range;
//Now read the elements of an array from the user
cout<<"\n Enter the elements of an array:";
for(i=0;i<range;i++)
cin>>arr[i];
//Display the message for taking the element to be searched from the user
cout<<"\n Enter the element to be searched:";
cin>>sitem;
//Now call the function Bsearch( ) with appropriate arguments
ipos=Bsearch(arr,range,sitem);
if(ipos==-1)
cout<<"\n The element:"<<sitem<<"does not exist in an array:";
else
cout<<"\n The element:"<<sitem<<"exists at position number:"<<ipos;
getch( );
} //End of main( )
//Function definition
int Bsearch(int arr[ ],int range,int sitem)
{
//Type declaration statement(s)
int pos,beg=0,last=range-1,mid;
while(beg<=last)
{
mid=(beg+last)/2;
if(arr[mid]==sitem)
{
pos=mid+1;
break;
}
else
{
if(sitem>arr[mid])
beg=mid+1;
else
last=mid-1;
} //end of else
} //End of while loop
//Now check whether the control has come out from the loop either due to
break statememt or due to false value of testing expression
if(beg>last)
pos=-1;
return pos;
}//End of function definition
Output:-
Enter the range(max 50):5
//Function prototype
void selsort(int[ ],int);//To sort the element of array in ascending order by
using Selection Sort method
//main( ) function
void main( )
{
clrscr( );//To clear the screen
//Type declaration statement(s)
int arr[50],range,i;
//Now accept the range of an array from the user
cout<<"\n Enter the range(max.50):";
cin>>range;
//Now accept the elements of array according to the range from the user
cout<<"\n Enter the elements of an array:";
for(i=0;i<range;i++)
{
cout<<"\n Enter the element no:"<<i+1;
cin>>arr[i];
}
/*Now pass the array and its range to selsort( ) function for sorting
(function calling)*/
selsort(arr,range);
//now display the sorted array
cout<<"\n The sorted array(ascending order) is given below:";
for(i=0;i<range;i++)
cout<<"\n"<<arr[i];
getch ( );
}//End of main( )
//Function definition
void selsort(int arr[ ],int size)
{
int temp,i,j;
for(i=0;i<size-1;i++)
{
for(j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function definition
Output:-
Enter the range(max.50):5
//Function prototype
void Bubble(int[ ],int); //to sort the elements of an array in ascending order
by bubble sort method
//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arr[50],i,range;
//Display the message for taking the range of an array from the user
cout<<"\n Enter the range:(max 50):";
cin>>range;
//Now accept the elements of an array according to the range from the user
cout<<"\n Enter the elements of an array:";
for(i=0;i<range;i++)
cin>>arr[i];
/*Now call the function Bubble() with appropriate values for sorting the
array*/
Bubble(arr,range);
//Now display the sorted array
cout<<"\n Sorted array(ascending order) is given below:";
for(i=0;i<range;i++)
cout<<"\n"<<arr[i];
getch( );
} //End of main( )
//Function definition
void Bubble(int arr[ ],int size)
{
int temp,i,j;
for(i=0;i<size-1;i++)
{
for(j=0;j<(size-i)-1;j++)
{
if(arr[j]>arr[j+1])
{
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
} //End of if
} //End of inner for loop
} //End of outer for loop
} //End of function definition
Output:-
Enter the range:(max 50):5
//Function prototype
void Inssort(int[ ],int);//To sort the elements by Insertion Sort method
//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arr[50],i,range;
//Now read the range of array from the user
cout<<"\n Enter the range(max 50):";
cin>>range;
//Now read the elements of array from the user
for(i=0;i<=range;i++)
cin>>arr[i];
//Now call the function Inssort( ) with appropriate arguments(values to
sort the element of an array in ascending order by using insertion sort
method)
Inssort(arr,range);
//Now display the sorted array
cout<<"\n The sorted array(ascending order) is given below:";
for(i=1;i<=range;i++)
cout<<"\n"<<arr[i];
getch( );
} //End of main()
//Function definition
void Inssort(int arr[ ],int range)
{
//Type declaration statements
int temp,i,j;
//Store the minimum value of integers into arr[0]
arr[0]=INT_MIN;
for(i=1;i<=range;i++)
{
temp=arr[i];
j=i-1;
while(temp<arr[j])
{
arr[j+1]=arr[j];
j--;
}
arr[j+1]=temp;
} //End of for loop
} //End of function definition
Output:-
Enter the range(max 50):5
5
2
7
8
4
/*Program to sort the two arrays(arrA and arrB) while merging(merge sort) and
the resultant array that we will get after merging should also be sorted in
ascending order if arrA and arrB are in ascending order*/
//Function prototype
void mergesort(int[ ],int,int[ ],int,int[ ]); //to apply merge sort technique
//main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
int arrA[50],arrB[50],arrC[100],rangeA,rangeB,rangeC,i;
//Now display the message for taking the range for first array
cout<<"\n Enter the range (max 50) for first array:";
cin>>rangeA;
//Now read the elements of first array
cout<<"\n Enter the elements of first array(elements should be in
ascending order):";
for(i=0;i<rangeA;i++)
cin>>arrA[i];
//Now read the range for second array
cout<<"\n Enter the range for second array:";
cin>>rangeB;
//Now read the elements for second array
cout<<"\n Enter the elements of second array(elements should be in
ascending order)";
for(i=0;i<rangeB;i++)
cin>>arrB[i];
//Now call the function merge sort()to get the resultant array with
appropriate arguments(values)
mergesort(arrA,rangeA,arrB,rangeB,arrC);
//Now display the resultant sorted array
rangeC=rangeA+rangeB;
cout<<"\n The resultant sorted array is given below:";
for(i=0;i<rangeC;i++)
cout<<"\n"<<arrC[i];
getch();
} //End of main( )
//Function definition
void mergesort(int arrA[ ],int rangeA,int arrB[ ],int rangeB,int arrC[ ])
{
//Type declaration statements
int CtrA=0,CtrB=0,CtrC=0;
//Now apply merge sort technique
while((CtrA<rangeA)&&(CtrB<rangeB))
{
if(arrA[CtrA]<=arrB[CtrB])
{
arrC[CtrC]=arrA[CtrA];
//Now increment the CtrC as well as CtrA by1
CtrC+=1;
CtrA+=1;
}//End of if
else
{
arrC[CtrC]=arrB[CtrB];
/*Now increment the CtrC as well as CtrB by 1*/
CtrC+=1;
CtrB+=1;
} //End of else
} //End of while loop
if(CtrA==rangeA)
{
/*Then copy the remaining element of arrB into arrC*/
while(CtrB<rangeB)
{
arrC[CtrC]=arrB[CtrB];
/*Now increment the CtrC and CtrB by 1*/
CtrC+=1;
CtrB+=1;
} //End of while loop
} //End of if
else
{
/*Then copy the remaining element of aaA into arrC*/
while(CtrA<rangeA)
{
arrC[CtrC]=arrA[CtrA];
//Now increment the CtrA and CtrC by 1
CtrC+=1;
CtrA+=1;
} //End of while loop
} //End of else
} //End of function definition
Output:-
Enter the range (max 50) for first array:5
//main( ) function
void main( )
{
clrscr( );//To clear the screen
//Type declaration instruction(s)
int a[10][10],b[10][10],c[10][10],rows,columns,i,j;
//Now accept the no. of rows and columns for matrix
cout<<"\n Enter the no. of rows:";
cin>>rows;
cout<<"\n Enter the no. of columns:";
cin>>columns;
//Now accept the elements of matrix from the user
cout<<"\n Enter the elements of matrix:";
for(i=0;i<rows;i++)
for(j=0;j<columns;j++)
cin>>a[i][j];
//Now find out transpose of the matrix
for(i=0;i<columns;i++)
for(j=0;j<rows;j++)
b[i][j]=a[j][i];
//Now display the transpose of the matrix
cout<<"\n Transpose of matrix is given below:";
for(i=0;i<columns;i++)
{
cout<<"\n";
for(j=0;j<rows;j++)
cout<<b[i][j]<<'\t';
}//end of for loop
getch ( );
}//End of main()
Output:-
Enter the no. of rows:2
//Structure definition
struct addr
{
int houseno;
char city[30];
char state[30];
};
//Function prototype(s)
void enter( );//To take input for storing the information about employee
void display(int);//To display information of particular employee
int search(int);//To locate the particular employee
//Function definition
void enter( )
{
//enter the information of ten employees
for(int i=0;i<10;i++)
{
clrscr();
cout<<"\n Employee Number "<<i+1<<" :";
cout<<"\n Enter the employee code:";
cin>>employee[i].emp_code;
cout<<"\n Enter the name of employee:";
gets(employee[i].name);
cout<<"\n Enter employee designation:";
cin>>employee[i].desig;
cout<<"\ Enter the address:houseno:";
cin>>employee[i].address.houseno;
cout<<"\n Enter the address:city:";
gets(employee[i].address.city);
cout<<"\n Enter the address:state:";
gets(employee[i].address.state);
cout<<"\n Enter the basic salary:";
cin>>employee[i].basic;
}//End of for loop
}//End of function definition
void display(int a)
{
//display the information of particular employee
cout<<"\n Employee code is:"<<employee[a].emp_code;
cout<<"\n Employee name is:"<<employee[a].name;
cout<<"\n Employee designation is:"<<employee[a].desig;
cout<<"\n Employee address is : "
<<employee[a].address.houseno<<'\t,'<<employee[a].address.city<<'\t,'
<<employee[a].address.state;
cout<<"\n Employee basic salary is:"<<employee[a].basic;
}//end of function definition
//main( ) function
void main( )
{
clrscr ( );//To clear the screen
//Type declaration statement
int ecode,i,pos;
char ch;
//Now call enter( ) function to store information about ten employees
enter( );
//Now execute the loop till the choice is'y'
do
{
//Accept the value of employee code to be searched
cout<<"\n Enter the employee code of which you want to see the
details";
cin>>ecode;
//Now call the function search( ) to locate the employee
pos=search(ecode);
//Now check whether the particular employee code exists or not
if((pos>=0)&&(pos<10))
{
//Yes empcode exists
//Now display the information of that employee by calling
display( ) function
display(pos);
}//End of if
else
{
//No,empcode doesnot exist
cout<<"\n Sorry:Employee information doesnot exists:";
}//End of else
cout<<"\n Do you want more:";
cin>>ch;
}while(ch=='y');//End of do
getch ( );
}//End of main( ) function
Output:-
Employee Number 1 :
Employee Number 2 :
Employee Number 3 :
Employee Number 4 :
Employee Number 5 :
Employee Number 6 :
Enter the employee code:206
Enter the name of employee:ram
Enter employee designation:clerk
Enter the address:houseno:89
Enter the address:city:noida
Enter the address:state:up
Enter the basic salary:10000
Employee Number 7 :
Employee Number 8 :
Employee Number 9 :
Employee Number 10 :
Enter the employee code of which you want to see the details : 209
Enter the employee code of which you want to see the details : 201
//Class definition
class item
{
//Declaration of data members
int icode;
float price;
public:
//Member function(s) prototype
void getdata(int,float);
void dispdata( );
float retcost( );
};
void item::dispdata( )
{
//Display the data members on the screen
cout<<"\n The item code is:"<<icode;
cout<<"\n The price is:"<<price;
}
float item::retcost( )
{
return price;
}
//Function(s) prototype(s)
void enter( );//To store information about 10 items
float maxcost( );//To find maximum cost amongst 10 items
//Main( ) function
void main( )
{
clrscr( ); //To clear the screen
//Type declaration statement(s)
float max_cost;
int i;
//Now call the enter( ) function to store information about 10 items
enter( );
/*Now call maxcost( ) to find out the maximum cost among 10 items and the
value returned by this function is of float type so store the value
returned by the function into max_cost*/
max_cost=maxcost( );
/*Now display the information of only those items whose price is maximum
amongst all the items*/
cout<<"\n the details of items whose price is maximum is given below:";
for(i=0;i<size;i++)
{
if(order[i].retcost( )==max_cost)
{
//call the display( ) member function of class item
order[i].dispdata();
}//End of if
}//End of for loop
getch( );
}//End of main( )
//Function(s) definition
void enter( )
{
int ino;
float cost;
/*Now execute the loop for 10 items to store information about 10 items*/
for(int i=0;i<10;i++)
{
cout<<"\n Enter the item code:";
cin>>ino;
cout<<"\n Enter the price of item:";
cin>>cost;
/*Now call getdata( ) function of class item to assign the value
input by user to the data members of the class*/
order[i].getdata(ino,cost);
}//End of for loop
}//End of function definition
float maxcost( )
{
float tempmax=order[0].retcost( ),tempcost;
for(int i=0;i<size;i++)
{
if(order[i].retcost( )>tempmax)
tempmax=order[i].retcost( );
}//End of for loop
//Now return the maximum cost
return tempmax;
}//End of function definition
Output :-
Enter the item code:101
Enter the price of item:300
//class definition
class stu
{
//Data member(s) declaration
int rollno;
char name[20],grade;
int eng,maths,physics,chemistry,comp_sc;
//Memeber function(s) prototype
public:
void getdata( );//to read the values for data members
void set_grade( );//to set the grade
void disp_result( );//to display the result
};//End of class definition
void stu::set_grade( )
{
//Type declaration statements(s)
float total,per;
int counter=0;
//Apply check to count number of subjects in which student is fail
if(eng<33)
counter=counter+1;
if(maths<33)
counter=counter+1;
if(physics<33)
counter=counter+1;
if(chemistry<33)
counter=counter+1;
if(comp_sc<33)
counter=counter+1;
//Set the grade
if(counter==0)
{
//Pass
grade='p';
//Now calculate the total
total=eng+maths+physics+chemistry+comp_sc;
per=(total/500)*100;
if(per>=80)
grade='A';
if((per>=60)&&(per<80))
grade='B';
if(per<60)
grade='C';
}
else
{
if(counter>2)
//fail
grade='f';
else
//compartment
grade='c';
}
}
void stu::disp_result( )
{
cout<<"\n Student's details with their Grade is given below:";
cout<<"\n Rollno is:"<<rollno;
cout<<"\n Name is:"<<name;
cout<<"\n Marks in english is:"<<eng;
cout<<"\n Marks in maths is:"<<maths;
cout<<"\n Marks in physics is:"<<physics;
cout<<"\n Marks is chemistry is:"<<chemistry;
cout<<"\n Marks in comp sc is:"<<comp_sc;
cout<<"\n Grade is:"<<grade;
}
//main( ) function
void main( )
{
clrscr ( );//to clear the screen
//Declare the object of class stu
stu student;
//Now call the member function in appropriate order
student.getdata( );
student.set_grade( );
student.disp_result( );
getch ( );
}
Output:-
Enter the rollno:2
//Class definition
class sepif
{
clrscr ( );//to clear the screen
//data members
int int_part;
float number,frac_part;
//member function(s)
public:
void getnumber( )//to read the number
{
cout<<"\n Enter the number:";
cin>>number;
}
void sep_parts( )//to separate integers and fractional parts
{
int_part=number;
frac_part=number-int_part;
}
void show_part( )//to show the result
{
cout<<"\n The number entered by the user is:"<<number;
cout<<"\n The integer part is:"<<int_part;
cout<<"\n The fractional part is:"<<frac_part;
}
};//End of class definition
//Main( ) function
void main( )
{
clrscr ( );//To clear the screen
//Declare the object
sepif sif;
/*Now call the member function(s) in appropriate manner*/
sif.getnumber( );
sif.sep_parts( );
sif.show_part( );
getch ( );
}
Output:-
Enter the number:25.458
//main( ) function
void main ( )
{
clrscr ( );//To clear the screen
//Type declaration statement(s)
char filen[20],str[40];
int count=0;
//Now declare the input stream
ifstream fin;
//Now accept the file name from the user
cout<<"\n Enter the file name:";
gets(filen);
/*Now establish the connection of the file with input stream fin by using
open( ) function*/
fin.open(filen);
//*Now check whether there is internal problem while handling the file*/
if(!fin)
{
cout<<"\n Unable to open the file:";
exit(1);
}
/*Now execute the loop to read the characters from file till the end of
file is encountered*/
while(!fin.eof( ))
{
//Read the characters and store it in str
fin>>str;
/*Now compare str with word "to" */
if(strcmpi(str,"to")==0)
{
//Increment the count variable by 1
count+=1;
}
}//End of while loop
//Now remove the connection of file from input stream by using close( )
function
fin.close( );
//Now display the result
cout<<"\n The word -to- exists "<<count<<"times in the file "<<filen;
getch ( );
}//End of main( )
Output:-
Enter the file name:try.dat
/*Program to create another file from an existing file having only those words
of an existing text file which starts from "T/t"*/
//main( ) function
void main( )
{
clrscr ( );//To clear the screen
//Type declaration statement(s)
char sfilen[20],dfilen[20],str[40];
//Now declare the input stream
ifstream fin;
//Now declare the output stream
ofstream fout;
//Now accept the source file name from the user
cout<<"\n Enter the source file name:";
gets(sfilen);
/*Now establish the connection of file with input stream by using open( )
function*/
fin.open(sfilen);
/*Now check the internal problem while handling the file*/
if(!fin)
{
cout<<"\n Unable to open the file:";
exit(1);
}
//Now accept the destination file name
cout<<"\n Enter the destination file name:";
gets(dfilen);
//Now establish the connection of file with output stream by using open( )
function
fout.open(dfilen);
//Now check the internal problem while handling the file
if(!fout)
{
cout<<"\n Unable to create the file:";
exit(1);
}
/*Now execute the loop for reading the characters from source file and
writing only those words which starts from 'T' into the destination file*/
while(!fin.eof( ))
{
if(fin.eof())
break;
/*Now read the characters from the source file and store it in str*/
fin>>str;
//Now Apply check on str and write those words which starts from
'T/t' into the destination file .
if((str[0]=='t')||(str[0]=='T'))
fout<<str<<' ';
Note:We can verify this by executing the type command at dos shell.
PROGRAM NO.14
//main( ) function
void main( )
{
clrscr( );//To clear the screen
//Type declaration statement(s)
char filen[20],ch;
int size=0;
//Now declare the input stream
ifstream fin;
//Now accept the file from the user
cout<<"\n Enter the file name(existing):";
gets(filen);
//Now open the file by using open( ) function
fin.open(filen);
/*Now check whether there is internal problem while handling the file*/
if(!fin)
{
cout<<"\n Unable to open the file:";
exit(1);
}
//Now move the get pointer to the end of the file.
fin.seekg(0,ios::end);
//Now use tellg() function to determine the size of the file
size=fin.tellg();
//Now display the size of the file.
cout<<"\n The size of file: "<<filen<<" is: "<<size;
//Now close the file
fin.close( );
getch ( );
}//End of main( )
Output:-
Enter the file name(existing):try.dat
//Function prototype(s)
int count_Text(char[] , int);//To count in a text File.
//main() function
void main()
{
clrscr();
//Type declaration Statement
int choice,count;
char fname[20];
//Now display the message for taking the file name from user
cout<<"\n enter the file Name:";
gets(fname);
//Now display the options
while(choice!=0)
{
clrscr();
cout<<"\n What Do You Want To Count :\n";
cout<<"\n 1. No Of Alphabets.";
cout<<"\n 2. No Of Upper Case Letters.";
cout<<"\n 3. No of Lowercase Letters." ;
cout<<"\n 4. No of Digits.";
cout<<"\n 5. No of Spaces.";
cout<<"\n 6. No of Lines.";
cout<<"\n 0. Exit";
cout<<"\n\n Enter your choice:";
cin>>choice;
//Now call the function count by passing the file name and choice
count = count_Text(fname , choice);
cout<<"\n Existence of Required Ones is : "<<count;
}//End of While
}//End of main()
//Function Definition
int count_Text(char fname[] , int choice)
{
//Type declaration statement
int count = 0;
char ch;
//Declare the stream.
ifstream fin(fname);
//Now check whether there is internal problem while handling the file
if(!fin)
{
cout<<"\n Unable to open the file :";
getch();
exit(1);
}
//Now read from the file and perform desired operations
switch(choice)
{
case 1:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(isalpha(ch))
count++;
}
break;
case 2:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(isupper(ch))
count++;
}
break;
case 3:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(islower(ch))
count++;
}
break;
case 4:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(isdigit(ch))
count++;
}
break;
case 5 :
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(ch==' ')
count++;
}
break;
case 6:
//Set the Get Pointer at Byte No 0
fin.seekg(0);
while(fin)
{
fin.get(ch);
if(ch=='\n')
count++;
}
break;
case 0:
//Now Remove the connection
fin.close();
exit(1);
1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit
1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit
1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit
1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit
Enter your choice:4
Existence of Required Ones is : 2
1. No Of Alphabets.
2. No Of Upper Case Letters.
3. No of Lowercase Letters.
4. No of Digits.
5. No of Spaces.
6. No of Lines.
0. Exit
//Class definition
class stu
{
//Declaration part
//Data members
int rollno,marks;
char name[20];
//Member functions
public:
void getdata( )
{
cout<<"\n Enter the rollno:";
cin>>rollno;
cout<<"\n Enter the name:";
gets(name);
cout<<"\n Enter the marks:";
cin>>marks;
}
int getrno( )
{
return(rollno);
}
void disp_data( )
{
cout<<"\n Name of the student is:"<<name;
cout<<"\n Rollno of the student is:"<<rollno;
cout<<"\n Marks of the student is:"<<marks;
}
};
//main( ) function
int main( )
{
clrscr( ); //To clear the screen
//Type declaration statements
stu student;
char ch='y',found='n';
int rno;
/*Now declare the fstream object as well as establish the connection of
file with output stream*/
fstream finout("student.dat",ios::in|ios::out|ios::binary);
if(!finout)
{
cout<<"\n Unable to create the file:";
getch( );
exit(1);
}
while((ch=='y')||(ch=='Y'))
{
student.getdata();
finout.write((char*)&student,sizeof(student));
cout<<"\n Do you want more:";
cout<<"\n Enter y for yes........n for no:";
cin>>ch;
}
//Now remove the connection of file with output stream.
finout.close( );
cout<<"\n Enter the rollno to be searched:";
cin>>rno;
//Now again establish the connection of file with the fstream object.
finout.open("student.dat",ios::in|ios::binary);
finout.seekg(0);
finout.read((char*)&student,sizeof(student));
while(!finout.eof( ))
{
if(finout.eof( ))
break;
if(student.getrno( )==rno)
{
student.disp_data( );
found='y';
}finout.read((char*)&student,sizeof(student));
}
if(found=='n')
cout<<"\n Record does not exist:";
finout.close( );
getch( );
return 0;
}
Output:-
Enter the rollno:2
/*Program to create another binary file from the existing binary file by writing
only those records into new one from the existing one which will satisfy some
condition*/
//Class definition
class stu
{
//Data Members
int rollno ,marks;
char name[20];
public :
//Member Function(s)
//Constructor
stu()
{
rollno = 0;
marks = 0;
strcpy(name , " ");
}
void getdata() //To take input from the user
{
cout<<"\nEnter The rollno:";
cin>>rollno;
cout<<"\n Enter The Name:";
gets(name);
cout<<"\n Enter the marks :";
cin>>marks;
}
void dispdata() //To display the data
{
cout<<"\n The rollno is :"<<rollno;
cout<<"\n The name is :"<<name;
cout<<"\n The Marks is :"<<marks;
}
int ret_marks()//To return marks
{
return marks;
}
};
//main() Function
void main()
{
//Type declaration statement(s)
char sfile[20] , dfile[20];
fbin.read((char*)&student , sizeof(student));
while(fbin)
{
if(fbin.eof())
break;
if(student.ret_marks()>=75)
fbout.write((char*)&student , sizeof(student));
fbin.read((char*)&student , sizeof(student));
}
//Now remove the connection of files with the stream
fbin.close();
fbout.close();
}
Output :
Enter The Source File Name : student.dat
Enter the Destination File name : s1.dat
DEFAULT
ARGUMENTS
PROGRAM NO.18
/*Program to calculate the Simple Interest by making a function having default
arguments*/
//Function(s) prototype
void sinterest(float,int=2,float=0.08);
//main( ) function
void main( )
{
clrscr ( );//To clear the screen
cout<<"\n Case 1:(Principal Amount):";
//Now call the function sinterest( ) with one value(argument)
sinterest(2000);
cout<<"\n Case 2:(Principal Amount,Time):";
//Now call the function sinterest with two values(argument)
sinterest(3000,3);
cout<<"\n Case 3:(Principal Amount,Time ,Rate):";
//Now call the function sinterest with three values(argument)
sinterest(2000,3,0.09);
getch ( );
}//End of main( ) function
//Function definition
void sinterest(float princ,int time,float rate)
{
cout<<"\n Principal amount is:"<<princ;
cout<<"\n Time is:"<<time;
cout<<"\n Rate is:"<<rate;
cout<<"\n The simple interest is:"<<(princ*time*rate);
}//End of function definition
Output:-
Case 1:(Principal Amount):
Principal amount is:2000
Time is:2
Rate is:0.08
The simple interest is:320
//Structure definition
struct Node
{
int info;
Node *link;
};
//Class definition
class Stack
{
//Data Members
Node *Top;
//Member Function(s)
//Constructor
public:
Stack( )
{
Top = NULL;
}
void Stack_Push( ) ;
void Stack_Pop();
void Stack_Disp();
};//End of class Definition
}
else
{
//Display the messsage for taking the info part from the user
cout<<"\n Enter The Information Part :" ;
cin>>newptr->info;
/*Now maintain the link part , for this check whether the node being
created is first node or not.*/
if(Top == NULL)
{
//yes it is the first node
newptr->link = NULL;
Top = newptr;
}
else
{
newptr->link = Top;
Top = newptr;
}
}
else
{
cout<<"\n The Deleted element is :"<<delptr->info;
Top = Top->link;
//Now deallocate the memory.
delete delptr;
}
void Stack::Stack_Disp()
{
Node *Disp=Top;
//Now check whether the Stack is Empty or Not.
clrscr();
if(Top == NULL)
{
cout<<"\n The Stack Is Empty:";
getch();
}
else
{
cout<<"\n The Stack Status is :";
while(Disp!=NULL)
{
cout<<'\n'<< Disp->info;
Disp = Disp ->link;
}
}
//main() function
void main()
{
clrscr();
//Type Declaration Statement
int choice;
//Declare the object of class Stack
Stack stk;
while(choice!=0)
{
clrscr();
cout<<"\n Stack's Options Are given Below :\n";
cout<<"\n 1.PUSH:";
cout<<"\n 2.POP:";
cout<<"\n 3.Display :";
cout<<"\n 0.Exit:";
cout<<"\n Enter Your Choice :";
cin>>choice;
switch(choice)
{
case 1:
stk.Stack_Push();
getch();
break;
case 2:
stk.Stack_Pop();
getch();
break;
case 3:
stk.Stack_Disp();
getch();
break;
case 0:
exit(1);
}//end of While
}//End of main() function
Output :
Push :
1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :1
1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :1
1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :1
Display :
Stack's Options Are given Below :
1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :3
1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :2
Exit:
1.PUSH:
2.POP:
3.Display :
0.Exit:
Enter Your Choice :0
PROGRAM NO.20
//Structure definition
struct Node
{
int info;
Node *Front , Rear;
};
//class definition
class Queue
{
//Data Members
Node *Front , Rear;
//Member Function(s)
//Constructor
public:
Queue( )
{
Front = NULL;
Rear = NULL;
}
void Queue_Push( ) ;
void Queue_Pop();
void Queue_Disp();
};//End of class Definition
}
else
{
//Display the messsage for taking the info part from the user
cout<<"\n Enter The Information Part :" ;
cin>>newptr->info;
//Now Maintain the link part
newptr->link = NULL;
//Now check whether the node being created is first node or not.
if(Rear == NULL)
{
//yes it is the first node
Front=newptr;
Rear = newptr;
}
else
{
Rear->link = newptr;
Rear = newptr;
}
}
else
{
cout<<"\n The Deleted element is :"<<delptr->info;
Front = Front->link;
//Now deallocate the memory.
delete delptr;
//Now check whether the queue is Empty or not.
If(front==NULL)
Rear=NULL;
}
void Queue::Queue_Disp()
{
Node *Disp=Front;
//Now check whether the Queue is Empty or Not.
clrscr();
if(Front == NULL)
{
cout<<"\n The Queue Is Empty:";
}
else
{
cout<<"\n The Queue Status is :";
while(Disp!=NULL)
{
cout<<'\n'<< Disp->info;
Disp = Disp ->link;
}
}
}//end of Function Definition
//main() function
void main()
{
clrscr();
//Type Declaration Statement
int choice;
//Declare the object of class Queue
Queue qu;
while(choice!=0)
{
clrscr();
cout<<"\n Queue Options Are given Below :\n";
cout<<"\n 1.Insert:";
cout<<"\n 2.Delete:";
cout<<"\n 3.Display :";
cout<<"\n 0.Exit:";
cout<<"\n Enter Your Choice :";
cin>>choice;
switch(choice)
{
case 1:
qu.Queue_Push();
getch();
break;
case 2:
qu.Queue_Pop();
getch();
break;
case 3:
qu.Queue_Disp();
getch();
break;
case 0:
exit(1);
}//end of While
}//End of main() Function
Output :
Insert:
1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :1
1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :1
1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :1
Display :
Queue Options Are given Below :
1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :3
1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :2
Exit:
1.Insert:
2.Delete:
3.Display :
0.Exit:
Enter Your Choice :0
SQL
COMMANDS
• CREATE TABLE
• ALTER TABLE
• INSERT INTO
• SELECT
• UPDATE
• ORDER BY
• IN
• BETWEEN
• COUNT
• MIN
• MAX
• LIKE
• DELETE
• DROP TABLE
SQL COMMANDS
Create Table:
Ans : SQL> Create Table Student(Name char(20) , Rollno number Primary Key ,
Marks number);
Alter Table
2.Q Alter Student to add new column Dob which is of date type.
Insert Into
(ii) To View only Rollno and Name of all the students of Student Table.
Select(to display only those records which fulfill the particular condition)
(iii) To View Only Records of those students whose marks are greater than 200.
(iv) To view records of only those Students whose Marks is Greater than 100 and
date of birth is ’27-Nov-80’.
Ans: SQL> Select * from Student where (Marks > 100) and (Dob ='27-nov-80');
Output: NAME
--------------------
Geeta
Update
5.Q Update the marks of those students whose rollno is 654 from 200 to 250.
Ans: SQL> Update Student Set Marks = 250 where Rollno = 654;
Order By
6.Q To view all the records of students sorted by the name in descending order.
Ans: SQL> Select * from Student Order By Name desc;
7.Q To view only name of all the students sorted according to date of birth in ascending
order.
Output: NAME
--------------------
Ritu
Geeta
Mamta
IN
8.Q To view records of only those students whose name is either geeta or mamta
Between
9.Q To view name and date of birth of those students whose dob lies between ’27-Oct-80’
and
’27-aug-81’.
Ans: SQL> Select Name , Dob from Student where Dob between '27-oct-80' and '27-nov-
80'
Output:NAME DOB
-------------------- ---------
Geeta 27-OCT-80
Mamta 27-NOV-80
Count
10.Q To count number of students who have got marks greater than 200.
Ans: SQL> Select Count(*) from Student Where Marks > 200;
Output:COUNT(*)
------------------
2
Min
Output:MIN(MARKS)
-------------------
100
Max
Output:MAX(MARKS)
-------------------
300
Like
13.Q To view Name and Roll no of those students whose Name has ‘t’ character within it.
Ans: SQL> Select Name , Rollno from Student where Name like '%t%'
Delete
14.Q To delete the record of those students whose name starts with ‘g’ character.
Drop Table
Output:Table dropped