Professional Documents
Culture Documents
#include <iostream.h> #include<conio.h> void insertion_sort(int x[],int length) { int key,i; for(int j=1;j<length;j++) { key=x[j]; i=j-1; while(x[i]>key && i>=0) { x[i+1]=x[i]; i--; } x[i+1]=key; } } int main() { clrscr(); int A[25]; int size,i; int x; cout<<"Enter size of array: "; cin>>size; cout<<"Enter numbers: "; for(x=0;x<size;x++) { cin>>A[x]; } cout<<"NON SORTED LIST: "<<endl; for(x=0;x<size;x++) { cout<<A[x]<<endl; }
OUTPUT:Enter size of array: 5 Enter numbers: 9 2 6 4 1 NON SORTED LIST: 9 2 6 4 1 SORTED LIST: 1 2 4 6 9
WAP to create an array of strings and sort them in ascending order using Bubble Sort.
#include<iostream.h> #include<conio.h> #include<string.h> main() { clrscr(); char *c,*st[]={"Pink","Orange","Black"}; cout<<"\n Original string: "; for(int i=0;i<3;i++) { cout<<"\n"<<st[i]<<"\n"; } for(int j=0;j<3;j++) { for(int i=0;i<2;i++) { if(strcmp(st[i],st[i+1])>0) { cout<<"\n\n\n"; cout<<st[i]<<" is greater than "<<st[i+1]<<"\n"; c=st[i]; cout<<"\n c= "<<c<<"\n"; st[i]=st[i+1]; cout<<"\n After copying: "; st[i+1]=c; cout<<st[i]<<" "<<st[i+1]<<"\n"; } } cout<<"\n Sorted array: \n"; for(i=0;i<3;i++) {cout<<st[i]<<endl;} } getch(); }
OUTPUT:Original string: Pink Orange Black Pink is greater than Orange c= Pink After copying: Orange Pink Pink is greater than Black c= Pink After copying: Black Pink Sorted array: Orange Black Pink Orange is greater than Black c=Orange After copying: Black Orange Sorted array: Black Orange Pink
WAP to create an array of numbers and search for a number using Linear Search.
#include<iostream.h> #include<conio.h> int Lsearch(int[],int,int); void main() { clrscr(); int ar[10],item,n,index; cout<<"\n Enter desired array size(max.10): "; cin>>n; cout<<"\n Enter array elements: \n"; for(int i=0;i<n;i++) { cin>>ar[i]; } cout<<"\n Enter element to be searched for: "; cin>>item; index=Lsearch(ar,n,item); if(index==-1) cout<<"\n SORRY given element could not be found; else cout<<"\n Element found at"<<"\n Index:"<<index <<"\n Position:"<<index+1; getch(); } int Lsearch(int AR[],int size,int item) { for(int i=0;i<size;i++) { if(AR[i]==item) return i; } return -1; }
OUTPUT:-
Enter desired array size(max.10): 5 Enter array elements: 1 2 3 4 5 Enter element to be searched for: 3 Element found at Index:2 Position:3
WAP to create an array of numbers and search for a number using Binary Search.
#include<iostream.h> #include<conio.h> main() { clrscr(); int x[10],item,top=0,bot=9,mid,f=0; cout<<"\n Enter the array: "; for(int i=0;i<10;i++) { cin>>x[i]; } cout<<"\n Enter the number to be searched: "; cin>>item; while((top<=bot)&&(f==0)) { mid=(top+bot)/2; if(x[mid]==item) f=1; else if(x[mid]<item) bot=mid-1; else if(x[mid]>item) top=mid+1; } if(f==1) cout<<"\n <<item<< is found in the array "; else if(f==0) cout<<"\n <<item<< is not found in the array "; }
OUTPUT 2:Enter the array: 1 2 3 4 5 6 7 8 9 10 Enter the number to be searched: 11 11 is not found in the array
if(a1<0) { while(b1>=0) { c[c1]=b[b1]; b1--; c1++; } } cout<<"\n\n\n\t THE SORTED ARRAY: "; for(int i=0;i<10;i++) { cout<<endl<<"\t"<<c[i]; } getch(); }
WAP to create a text file life.txt and count the number of vowels in it.
#include<fstream.h> #include<conio.h> #include<stdio.h> #include<string.h> main() { clrscr(); char to[10]; ofstream ofile("life.txt"); cout<<"\n Enter you name: "; cin.getline(to,10,'.'); ofile<<to; ofile.close(); ifstream ifile("life.txt"); int ct=0; char c; while(!ifile.eof()) { c=ifile.get(); if((c=='a') || (c=='e') || (c=='i') || (c=='o') || (c=='u') || (c=='A') || (c=='E') || (c=='I') || (c=='O') || (c=='U')) { ct++; } } cout<<"\n No. of vowels: "<<ct; ifile.close(); getch(); }
";
OUTPUT 1 :-
Enter the 'k'th position where a no. has to be inserted: 3 Enter the no. to be inserted: 2 Enter the array in which no. has to be inserted: 1 2 3 4 5 The new array after insertion of no. is: 122345
OUTPUT 2 :Enter the 'k'th position where a no. has to be inserted: 5 Enter the no. to be inserted: 1 Enter the array in which no. has to be inserted: 1 2 3 4 5 The new array after insertion of no. is: 123415
clrscr(); int x[5],k,j; cout<<"\n\n Enter 'k'th position from where a no. is to be deleted: "; cin>>k; cout<<"\n\n Enter the array from which no. has to be deleted: "; for(int i=0;i<5;i++) { cin>>x[i]; } for(i=0;i<5;i++) { if(i>=(k-1)) { j=x[i+1]; x[i]=j; } } cout<<"\n The new array after deletion of no. is: "; for(i=0;i<4;i++) { cout<<x[i]; getch(); } }
OUTPUT 1:-
Enter 'k'th position from where a no. is to be deleted: 3 Enter the array from which no. has to be deleted: 1 2 3 4 5 The new array after deletion of no. is: 1245
OUTPUT 2:-
Enter 'k'th position from where a no. is to be deleted: 2 Enter the array from which no. has to be deleted: 1 2 3 4 5 The new array after deletion of no. is: 1345
OUTPUT:-
Default constructor invoked Parameterised constructor invoked Copy constructor invoked Value of x: 0 Value of x: 15 Value of x: 15
} void main() { clrscr(); int stack[size],item,top=-1,res; char ch='y'; while(ch=='y') { cout<<"\n Enter item for insertion: "; cin>>item; res=push(stack,top,item); if(res==-1) { cout<<"\n Overflow!!!"; exit(1); } cout<<"\n The stack is: "; display(stack,top); cout<<"\n Want to insert more elements? "; cin>>ch; } cout<<"\n Now deletion..\n"; ch='y'; while(ch=='y') { res=pop(stack,top); if(res==-1) { cout<<"\n Underflow"; exit(1); } else { cout<<"\n Element deleted is: "<<res; cout<<"\n The stack is now: \n"; display(stack,top); } cout<<"\n Want to delete more: "; cin>>ch; } }
Want to insert more elements? y Enter item for insertion: 2 The stack is: 2<--1 Want to insert more elements? n Now deletion... Element deleted is: 2 The stack is now: 1<-Want to delete more: y Element deleted is: 1 The stack is now: Want to delete more: n
void pop() { if(top==NULL) { cout<<"\n Underflow"; } else { cust *p=top; top=top->next; cout<<"\n Item to be deleted: "; cout<<(p->cno); delete p; } } void disp() { cust *p=top; while(p!=NULL) { cout<<"\n Roll No.: "<<p->cno; cout<<"\n Name: "<<p->cname; p=p->next; } } main() { clrscr(); char h; do{ cout<<"\n 1. Push item in stacks"; cout<<"\n 2. Pop item from stacks"; cout<<"\n 3. Display item from stacks \n; int ch; cin>>ch;
switch(ch) { case 1: cout<<"\n Push function is called"; push(); break; case 2: cout<<"\n Pop function is called"; pop(); break; case 3: cout<<"\n Display function is called"; disp(); break; } cout<<"\n Do you want to continue?"; cin>>h; }while(toupper(h)=='Y'); getch(); }
OUTPUT:1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 1 Push function is called Enter roll no.: 1 Enter name: anushka
Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 1 Push function is called Enter roll no.: 2 Enter name: sugandha Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 2 Pop function is called Item to be deleted:2 Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 3 Display function is called Roll No.: 1 Name: anushka Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 2 Pop function is called Item to be deleted: 1 Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks
3. Display item from stacks 3 Display function is called Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 2 Pop function is called Underflow Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 1 Push function is called Enter roll no.: 1 Enter name: anushka Do you want to continue?y 1. Push item in stacks 2. Pop item from stacks 3. Display item from stacks 3 Display function is called Roll No.: 1 Name: anushka Do you want to continue?t
if(front==rear) front=rear=-1; else front++; } return ret; } void display(int queue[],int front,int rear) { if(front==-1)return; for(int i=front;i<rear;i++) cout<<queue[i]<<"<-\t"; cout<<queue[rear]; } void main() { int item,res; char ch='y'; clrscr(); while(ch=='y'||ch=='Y') { cout<<"\n Enter item for insertion: "; cin>>item; res=insert(queue,item); if(res==-1) { cout<<"Overflow"; exit(1); } cout<<"\n Now the queue( front to rear) is: \n"; display(queue,front,rear); cout<<"\n Want to insert more(y/n): "; cin>>ch; } cout<<"\n Deletion of item..."; ch='y'; while(ch=='y') {
res=remove(queue); if(res==-1) { cout<<"\n Underflow"; } else { cout<<"\n Element deleted is: "<<res; cout<<"\n Now the queue(front...to...rear)is: \n"; display(queue,front,rear); } cout<<"\n Want to delete more?"; cin>>ch; } }
OUTPUT:Enter item for insertion: 1 Now the queue( front to rear) is: 1 Want to insert more(y/n): Y Enter item for insertion: 2 Now the queue( front to rear) is: 1<- 2 Want to insert more(y/n): N Deletion of item... Element deleted is: 1 Now the queue(front...to...rear)is: 2 Want to delete more?N
{ cout<<"\n Underflow"; } else if(rear==front) { delete front; front=rear=NULL; } else { p=front; front=front->next; delete p; } } void disp() { stu *p=front; while(p!=NULL) { cout<<"\n Roll No.: "<<p->rno; p=p->next; } } main() { clrscr(); front=NULL; char c,h; do{ cout<<"\n 1. Enter item in queues"; cout<<"\n 2. Delete item from queues"; cout<<"\n 3. Display item from queues \n"; int ch;
cin>>ch; switch(ch) { case 1: cout<<"\n Insert function is called"; push(); break; case 2: cout<<"\n Del function is called"; pop(); break; case 3: cout<<"\n Display function is called"; disp(); break; } cout<<"\n Do you want to continue?"; cin>>h; }while(toupper(h)=='Y'); getch(); }
OUTPUT:1. Enter item in queues 2. Delete item from queues 3. Display item from queues 1 Insert function is called Enter roll no. 1 Do you want to continue?y 1. Enter item in queues
2. Delete item from queues 3. Display item from queues 1 Insert function is called Enter roll no. 2 Do you want to continue?y 1. Enter item in queues 2. Delete item from queues 3. Display item from queues 2 Del function is called Do you want to continue?y 1. Enter item in queues 2. Delete item from queues 3. Display item from queues 3 Display function is called Roll No.: 2 Do you want to continue?y 1. Enter item in queues 2. Delete item from queues 3. Display item from queues 2 Del function is called Do you want to continue?y 1. Enter item in queues 2. Delete item from queues 3. Display item from queues 2 Del function is called Underflow Do you want to continue?y 1. Enter item in queues 2. Delete item from queues
3. Display item from queues 1 Insert function is called Enter roll no. 1 Do you want to continue?y 1. Enter item in queues 2. Delete item from queues 3. Display item from queues 1 Insert function is called Enter roll no. 2 Do you want to continue?y 1. Enter item in queues 2. Delete item from queues 3. Display item from queues 3 Display function is called Roll No.: 1 Roll No.: 2 Do you want to continue?t
return; } else if( front== rare ) { front=rare=-1; return; } front= (front+1)%5; } void display() { int i; if( front <= rare) { for(i=front; i<=rare;i++) cout << q[i]<<" "; } else { for(i=front;i<=4;i++) { cout <<q[i] << " "; } for(i=0;i<=rare;i++) { cout << q[i]<< " "; } } } };
cout<<"\n 1.INSERT; cout<<\n 2.DELETE; cout<<\n 3.DISPLAY; cout<<\n 4.EXIT; cout<<\n Enter ur choice: "; cin >> ch; switch(ch) { case 1: cout<<"\n Enter element: "; cin>>ch; q1.push(ch); break; case 2: q1.pop(); break; case 3: q1.display(); break; case 4: exit(0); } } }
2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 1 Enter element: 2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 1 Enter element: 3 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 1 Enter element: 4 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 1 Enter element: 5 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 1 Enter element: 6 Circular Queue over flow 1.INSERT 2.DELETE 3.DISPLAY
4.EXIT Enter ur choice: 3 12345 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 3 2345 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 3 345 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 1 Enter element: 6 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 1 Enter element: 7
1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 3 34567 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 2 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 3 567 1.INSERT 2.DELETE 3.DISPLAY 4.EXIT Enter ur choice: 4
OUTPUT:-
WAP to create pattern of stars as shown:* *** ***** *** * #include<iostream.h> #include<conio.h> main() { clrscr(); for(int i=0;i<5;i++) { for(int j=0;j<i;j++) { cout<<"*"; } cout<<"*"<<endl; } }
void putdata() { clrscr(); cout<<"\n Name: "<<name<<"\n Rollno: "<<rollno<<"\n Class: "<<cls <<"\n Marks in comp. sci.: "<<marks;
} int getrno() { return rollno; } }s1; void main() { clrscr(); ofstream fo("stu.dat",ios::app); char ans='y'; while(ans=='y') { s1.getdata(); fo.write((char*)&s1,sizeof(s1)); cout<<"\n Record added to file."; cout<<"\n Want to enter more records?(y/n).."; cin>>ans; s1.putdata(); } fo.close(); getch(); }
OUTPUT:Enter your rollno:1 Enter your name: SUGANDHA Enter your class: 12
Enter your marks: 70 Record added to file. Want to enter more records?(y/n).. n Name: SUGANDHA Rollno: 1 Class: 12 Marks in comp. sci.: 70
entsal(); calcsal(); cout<<"\n Basic salary: "<<basic; cout<<"\n HRA value: "<<hra; cout<<"\n DA value: "<<da; cout<<"\n CCA value: "<<cca; cout<<"\n Tax: "<<tax; cout<<"\n Net salary: "<<netsalary; } }; class employee:protected salary { int eno; char ename[20]; char designation[20]; public: void entemp() { cout<<"\n Enter employee no.: "; cin>>eno; cout<<"\n Enter employee name: "; gets(ename); cout<<"\n Enter the designation of the employee: "; gets(designation); } void showemp() { cout<<"\n Employee no.: "<<eno; cout<<"\n Employee name: "; puts(ename);
cout<<"\n Employee designation: "; puts(designation); showsal(); } }; main() { clrscr(); employee e1; e1.entemp(); e1.showemp(); getch(); }
OUTPUT:Enter employee name: sugandha Enter the designation of the employee: manager Employee no.: 1 Employee name: sugandha Employee designation: manager Enter the basic salary: 30000 Enter the hra: 1500 Enter The da: 250 Enter the cca: 350 Enter the tax: 2300
Basic salary: 30000 HRA value: 1500 DA value: 250 CCA value: 350 Tax: 2300 Net salary: 29800
15 SQL commands.
CREATING TABLE:create table student (adno integer primary key, sname varchar(20) not null, class number, rollno number unique, fees integer check fees<11000 and fees>8000, city varchar(20));
INSERTING VALUES:insert into student values(1,Anil,10,1,10500,Mumbai); insert into student values(2,Ashish,10,2,9800,Delhi); insert into student values(3,Ashwini,10,3,9900,Kolkata); insert into student values(8,Jia,11,5,10800,Mumbai); insert into student values(9,Khushboo,12,6,9200,Delhi); insert into student values(23,Sugandha,12,17,8100,Delhi);
ADDING NEW FIELD:alter table student add school varchar(20) not null;
UPDATING FIELD VALUE:update class set class=class+1 where name like An%;
COUNT() FUNCTION & GROUP BY CLAUSE:select city,count(city) from student group by city;