You are on page 1of 84

COMPUTER SCIENCE CLASS XII SCIENCE (2011-2012)

1:- WAP to read the context from a text file student.txt , count and display the number of alphabates presents in it. 2:- WAP take 2 string concatenated string. arguments and return the

3:- Write a menu driven program to add , delete & modify records from a file using class student with data members. Assuming the proper data types and functions. 4:- WAP to sort elements in an array with the technique depending on the user s choice (Insertion, Bubble & Selection sort). 5:- Write a menu driven program to do operations matrices (Addition, Subtraction, multiplication). 6:- Given an array of Pointers names[] shown below:
Char *names[]={ Ajay , Rahul , Joe , Ali };

of

Write a program to reverse the order of these names. 7:- WAP to insert or delete an element from the linked Queue dependent on the user s choice. 8:- Write a menu driven program to insert, delete, modify an element in a linear linked list. 9:- WAP depending upon user s choice either push or pop an element in a stack array. 10:- WAP depending upon user s choice stackpush() to isert nodes and stackpop() to delete nodes for a linked list implemented stack.

11:- WAP to search an element in an array with the techniques depending upon user s choice (Linear search and Binary search). 12:- Write a function that passess the address of time structure and set the total time of two time variable. 13:- Write a c++ program that uses the following path:
Person(name, age)------- student(rno, grade)-----graduate student(enrollno, subject)

with the data members as possible. 14:- WAP to show the working Calculate interest amount. of function overloading.

15:- WAP to illustrate the working of function returning an object. 16:- WAP to calculate the two distance passed as reference arguments to a function. 17:- Define a class named HOUSING with the given data members:
Private members: reg_no, name, type, cost. Public members: Read_data() to read an object of housing. Display() to show details of an object. Draw_no() to calculate the cost depending on the type of house(2 room flat, 3 room flat or 4 room flat).

18:- WAP to calculate the sum of the following series


1+x/2+x^2/4+ x^n/2n

19:- WAP to calculate the sum of the following series


1+x+x^2/2!+x^3/3!+ x^n/n!

20:- WAP to read and display information aboutemployees and managers. 21:- WAP with a help of functions to encode a string that is passed to it. The string should get converted into an unrecognizable form. 22:- WAP using function that takes a string reversed string. and return a

23:- WAP to illustrate the use of object pointer. 24:- WAP that invokes a function to increase the basic salary of an employee if the employee has experience of 10 or more years. 25:- WAP to search the name and address of person having age more than 30 years in the data list of person. 26:- WAP to implement searching in a file that has records maintained through structures. 27:- WAP in which function transfer() in c++, that would copy all those records which are having area code as MUB from phone.txt to phonebook.txt. 28:- WAP to search for a given character inside a string and print the string from the point of match showing the function with the concept of function returning pointers. 29:- WAP in c++ to print the product of each row of a 2-D integer array passed as argument of the function. 30:- WAP in c++ using a function swaparray() to swap the first row elements with the last row elements, for a 2-D integer array passed as the argument of the function.

1:- WAP to read the context from a student.txt , count and display the alphabates presents in it.

text file number of

#include<fstream.h> #include<iostream.h> #include<conio.h> #include<ctype.h> void main() { clrscr(); char ch; int count = 0; ifstream object("student.txt"); while(!eof()); { object.get(ch); if(isalpha(ch)) count = count+1; } object. close(); cout<<"\n Number of alphabets in this file is :-\t"; cout<<count; getch(); }

/*

Output
Example: File contains this is my first project of computer in C++. Number of alphabets present in this file is :*/

35

2:- WAP to take 2 string concatenated string.

arguments

and

return

the

#include<iostream.h> #include<string.h> #include<conio.h> void main() { clrscr(); char str1[50], str2[50]; cout<<"\n Enter first sentence:"<<endl; cin.getline(str1,101); cout<<"\n Enter second sentence:"<<endl; cin.getline(str2,101); cout<<"\n Now the sentence is:"<<"\n"; cout.write(str1,50).write(str2,50); getch(); }

/*

Output
Enter first sentence: my name is Enter second sentence: muhammad mohsin Now the sentence is: my name is muhammad mohsin

*/

3:- Write a menu driven program to add , delete & modify records from a file using class student with data members. Assuming the proper data types and functions.

#include<stdio.h> #include<iostream.h> #include<conio.h> class student { private: int stud_id; char stud_name[20], stud_fname[20], stud_add[50], stud_g[1]; public: void read_data() { cout<<"Enter student ID number:\t"; cin>>stud_id; cout<<"Enter student name: \t"; gets(stud_name); cout<<"Enter student's father name:\t"; gets(stud_fname); cout<<"Enter student's grade:\t "; gets(stud_g); cout<<"Enter student's address:\t"; gets(stud_add); } void modify_data() { cout<<"Enter students correct name:\t"; gets(stud_name); cout<<"Enter sudent's father name:\t"; gets(stud_fname); cout<<"Enter student's grade:\t"; cin>>stud_g; cout<<"Enter student's address:\t"; gets(stud_add); } void display_data() { cout<<"\n Sudent ID No. :-\t"<<stud_id; cout<<"\n Student name:-\t"<<stud_name; cout<<"\n Father name:-\t"<<stud_fname; cout<<"\n Grade:-\t"<<stud_g; cout<<"\n Students address:-\t"<<stud_add; }

int getstud_id() { return stud_id; } }; void main() { clrscr(); student s[50]; int choice, i, p, id; char ch[2]; cout<<"\n What do you want to do?"; cout<<"\n1. Add Data"; cout<<"\n2. Modify Data"; cout<<"\n3. Delete Data"; cout<<"\n Enter your choice(1-3): "; cin>>choice; if(choice==1) { for(i=0; i<=50; i++) {s[i].read_data(); s[i].display_data();} while(ch==y || ch==Y) cout<<"\n Do you want to enter more data. Enter 'y' for yes \t\t"; gets(ch); } if(choice==2) { cout<<"\n Enetr student's ID No. whose data to be modified:\t"; cin>>id; { for(i=1; i<=50; i++) if(s[i].getstud_id()==id) s[i].modify_data(); else cout<<"\n SORRY entered ID No. did not found."; } } if(choice==3) { cout<<"\n Enter student's ID No. whose data to be deleted:\t\t"; cin>>id; { for(i=0; i<=50; i++) if(s[i].getstud_id()==id) p=i; else cout<<"\n SORRY entered ID No. did not found.";

} for(i=0; i<=50; i++) { if(i==p) cout<<"\nDetails of entered ID No. deleted. "; else s[i].display_data(); } } cout<<"\n Wrong key pressed TRY again"; getch(); }

/*

Output
What do you want to do? 1. Add Data 2. Modify Data 3. Delete Data Enter your choice(1-3): 1 Enter student ID Number: Enter sudent's father name: Enter student's father name: Enter student's grade: Enter student's address:

20081511 muhammad mohsin muhammad sabir A vill. sanpla bakkal, p.o. sanpla khatri, p.st. deoband, distt. saharanpur, u.p.

*/

4:- WAP to sort elements in an array with the technique depending on the user s choice (Insertion, Bubble & Selection sort).

#include<iostream.h> #include<limits.h> #include<conio.h> #include<ctype.h> void selsort(int[], int); void bubblesort(int[], int); void inssort(int[], int); void main() { clrscr(); int ar[50], n, ch; cout<<"\n How many elements do you want to create an array{max 50}: "; cin>>n; cout<<"\n enter element in the array\t"; for(int i=0; i<=n; i++) cin>>ar[i]; cout<<"\n Which type of sorting do you want to do?"; cout<<"\n MENU"; cout<<"\n1. SELECTION SORT"; cout<<"\n2. BUBBLE SORT"; cout<<"\n3. INSERTION SORT"; cout<<"\n Enter your choice(1-3):\t"; cin>>ch; cout<<"\n The sorted array is shown below"; if(ch==1) { selsort(ar,n); for(i=0; i<=n; i++) cout<<ar[i]<<"\t"; cout<<endl; } if(ch==2) { bubblesort(ar, n); for(i=0; i<=n; i++) cout<<ar[i]<<"\t"; cout<<endl; } if(ch==3) { inssort(ar, n);

for(i=1; i<=n; i++) cout<<ar[i]<<cout<<"\t"; } else cout<<"\n Wrong choice\t"; }

void selsort(int ar[], int size) { int small, position, temporary; for(int i=1; i<=size; i++) { small= ar[i]; for(int j=i+1; j<=i+1; j++) { if(ar[j]<small) { small=ar[j]; position=j; } } temporary=ar[i]; ar[i]=ar[position]; ar[position]=temporary; for(j=1; j<=size; j++) { for(j=0; j<size; j++) cout<<ar[j]<<"\t"; } } }

void bubblesort(int ar[], int size) { int temporary, ctr=0; for(int i=0; i<size; i++) { for(int j=1; j<(size-1); j++) { if(ar[j]>ar[j+1]) { temporary=ar[j]; ar[j]=ar[j+1]; ar[j+1]=temporary;

} } cout<<"\n Array after isertion - "<<++ctr<<"is:\t"; for(int k=0; k<size; k++) cout<<ar[k]<<"\t"; cout<<endl; } }

void inssort(int ar[], int size) { int temporary, j; ar[0]=INT_MIN; for(int i=1; i<=size; i++) { temporary=ar[i]; j=i-1; while(temporary<ar[j]) { ar[j+1]=ar[j]; j--; } ar[j+1]=temporary; for(int k=1; k<=size; k++) cout<<ar[k]<<"\t"; cout<<endl; } }

/*

Output
How many elements do you want to create an array{max 50}: 5 enter element in the array 25 14 2 54 36 4

Which type of sorting do you want to do? MENU 1. SELECTION SORT 2. BUBBLE SORT 3. INSERTION SORT Enter your choice(1-3): 1 The sorted array is shown below4 54 4 14 2 25 54 4 54 4 14 2 25 36 4 36 4 14 2 25 36 54 */ 14 14 14 25 2 2 2 25 25

5:- Write a menu driven program to do operations matrices (Addition, Subtraction, multiplication).

of

#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int a[2][2], b[2][2], c[2][2]; int n, p, m, q; int ch; cout<<"\n Enter the order of first matrix."; cout<<"\nrow: "; cin>>m; cout<<"\ncolumn: "; cin>>n; cout<<"\n Enter the order of second matrix."; cout<<"\n row: "; cin>>p; cout<<"\ncolumn: "; cin>>q; cout<<"\n Enter the first matrix:\t"; for(int i=1; i<=m; i++) { for(int j=1; j<=n; j++) cin>>a[i][j]; } cout<<"\n Enter the second matrix:\t"; for(i=1; i<=p; i++) { for(int j=1; j<=q; j++) cin>>b[i][j]; } cout<<"\n What do you want to do with matrices?"; cout<<"\nMATRIX MENU"; cout<<"\n1. Add Matrices"; cout<<"\n2. Subtract Matrices"; cout<<"\n3. Mutiply Matrice"; cout<<"\n Enter your choice(1-3): "; cin>>ch; { if(ch==1 || ch==2) { if((m==p)&&(n==q)) { for(int i=1; 1<=m; i++)

{ for(int j=1; j<=n; j++) if(ch==1) c[i][j]=a[i][j]+b[i][j]; else c[i][j]=a[i][j]-b[i][j]; } cout<<"\n SORRY matrices can't be added"; } } if(ch==3) { for(int i=1; i<=m; i++) for(int j=1; j<=q; j++) { c[i][j]=0; for(int i=1; i<=p; i++) c[i][j]=a[i][j]*b[i][j]; } } } for(int j=1; j<=q; j++) cout<<c[i][j]; getch(); }

/*

Output
Enter the order of first matrix. row: 2 column: 2 Enter the order of second matrix. row: 2 column: 2 Enter the first matrix: 2 4 6 8 Enter the second matrix: 4 5 7 9 What do you want to do with matrices? MATRIX MENU 1. Add Matrices 2. Subtract Matrices 3. Mutiply Matrices Enter your choice(1-3): 1 6 13 */ 11 17

6:- Given an array of Pointers names[] shown below:


Char *names[]={ Ajay , Rahul , Joe , Ali };

Write a program to reverse the order of these names.

#include<iostream.h> #include<conio.h> #include<string.h> void main() { char *names[]={"Ajay", "rahul","joe", "ali"}; int len=0; cout<<"\n Original order of name is\t"; for(int i=1; i<=4; i++) { len= strlen(names[i]); cout.write(names[i], len).put('\n'); } for(i=4; i>=1; i--) { len=strlen(names[i]); cout.write(names[i], len).put('\n'); } getch(); }

/*

Output
Original orde of name is Ajay rahul joe ali the reverse order of name is ali joe rahul ajay */

7:- WAP to insert or delete an element from the linked Queue dependent on the user s choice.

#include<iostream.h> #include<conio.h> #include<process.h> struct node { int info; node *next; } *start, *newptr, *rear, *save, *ptr; node *create_new_node(int); void insert(node*); void delnode(); void display(node*); void main() { clrscr(); int choice; cout<<"what do you want to do?"; cout<<"\ Linked List Menu "; cout<<"\n1. Insert element."; cout<<"\n2. Delete element."; cout<<"\n Enter your choice(1-2)...."; cin>>choice; if(choice==1) { start = NULL; int inf; char ch = 'y'; while(ch=='y'||ch=='Y') { clrscr(); cout<<"\n Enter information for the new node...."; cin>>inf; cout<<"\n press enetr to create new node..."; getch(); newptr = create_new_node(inf); if(newptr!=NULL) { cout<<"\n New node created successfully. press enter to continue..."; exit(1); } else {

cout<<"\n cannot create new node!!! aborting!!\n"; getch(); exit(1); } cout<<"\n now iserting node..... "; cout<<"press enter to continue\n"; getch(); insert(newptr); cout<<"noe the list is:\n"; display(start); cout<<"\n press y to enter more nodes, N to exit..."; } } if(choice==2) { start = rear = NULL; int inf; char ch='y'; while(ch=='y'||ch=='y') { clrscr(); cout<<"Enter the information of new node"; cin>>inf; newptr = create_new_node(inf); if(newptr == NULL) { cout<<"cannot create new node!!! Abosrting!"; getch(); exit(1); } insert(newptr); cout<<"\npress Y to enter more nodes, N to exit...; cin>>ch; } clrscr(); do { cout<<"\nthe list now is"; display(start); getch(); cout<<"want to delete first node?(y/n)..."; cin>>ch; if(ch=='y'||ch=='Y') delnode(); } while(ch=='y'||ch=='Y'); } }

node *create_new_node( int n ) { ptr = new node; ptr -> info = n; ptr -> next = NULL; return ptr; } void insert(node* np) { if(start == NULL) start = np; else { save = start; start = np; np -> next = save; } } void delnode() { if(start == NULL) cout<<"underflow!!!\n"; else { ptr=start; start = start -> next; delete ptr; } } void display(node* np) { while(np!= NULL) { cout<< np -> info <<"-> "; np= np -> next; } cout<<"!!!\n"; }

/*

Output
what do you want to do? LL MENU 1. Insert element. 2. Delete element. Enter your choice(1-2).... 1 Enter information for the new node....1 press enetr to create new node... New node created successfully. press enter to continue... Linked List Menu 1. Insert element. 2. Delete element. Enter your choice(1-2).... 2 Enter the information of new node5 press Y to enter more nodes, N to exit... y Enter the information of new node8 press Y to enter more nodes, N to exit... n the list now is8-> 5-> !!! want to delete first node?(y/n)...y the list now is5-> !!! want to delete first node?(y/n)...n */

8:- Write a menu driven program to insert, delete, modify an element in a linear linked list.

#include<iostream.h> #include<conio.h> #include<process.h> struct node { int info; node *next; } *start, *newptr, *rear, *save, *ptr; *create_new_node(int); insert(node*); delnode(); modify(); display(node*); main()

node void void void void void {

clrscr(); int a; cout<<"what do you want to do?"; cout<<"\nLL MENU"; cout<<"\n1. Insert element."; cout<<"\n2. Delete element."; cout<<"\n3. Modify element."; cout<<"\n Enter your choice(1-3)...."; cin>>a; if(a==1) { start = NULL; int inf; char ch = 'y'; while(ch=='y'||ch=='Y') { clrscr(); cout<<"\n Enter information for the new node...."; cin>>inf; cout<<"\n press enetr to create new node..."; getch(); newptr = create_new_node(inf); if(newptr!=NULL) { cout<<"\n New node created successfully. press enter to continue..."; exit(1); }

else { cout<<"\n cannot create new node!!! aborting!!\n"; getch(); exit(1); } cout<<"\n now iserting node..... "; cout<<"press enter to continue\n"; getch(); insert(newptr); cout<<"noe the list is:\n"; display(start); cout<<"\n press y to enter more nodes, N to exit..."; } } if(a==2) { start = rear = NULL; int inf; char ch='y'; while(ch=='y'||ch=='y') { clrscr(); cout<<"Enter the information of new node: "; cin>>inf; newptr = create_new_node(inf); if(newptr == NULL) { cout<<"cannot create new node!!! Abosrting!"; getch(); exit(1); } insert(newptr); cout<<"\npress Y to enter more nodes, N to exit...\n"; cin>>ch; } clrscr(); do { cout<<"\nthe list now is"; display(start); getch(); cout<<"want to delete first node?(y/n)..."; cin>>ch; if(ch=='y'||ch=='Y') delnode(); } while(ch=='y'||ch=='Y'); } if(a==3)

{ start = rear = NULL; int inf; char ch='y'; while(ch=='y'||ch=='y') { clrscr(); cout<<"enter the information of new node "; cin>>inf; cout<<"creating new node press enter to continue"; getch(); newptr = create_new_node(inf); if (newptr!=NULL) cout<<"node created successfully "; exit(1); } cout<<"inserting the node"; cout<<"press enter to continue"; getch(); { insert(newptr); cout<<"now the list is"; display (start); cout<<"press y to enter more nodes"; cin>>ch; } cout<<"the new list is now"; modify(); getch(); { cout<<"wrong choice"; } } } node *create_new_node( int n ) { ptr = new node; ptr -> info = n; ptr -> next = NULL; return ptr; } void insert(node* np) { if(start == NULL) start = np; else { save = start; start = np; np -> next = save; }

} void delnode() { if(start == NULL) cout<<"underflow!!!\n"; else { ptr=start; start = start -> next; delete ptr; } } void modify(node* np) { while(np!=NULL) cout<<np -> info; np = np -> next; } void display(node* np) { while(np!= NULL) { cout<< np -> info <<"-> "; np= np -> next; } cout<<"!!!\n"; }

Output
what do you want to do? LL MENU 1. Insert element. 2. Delete element. 3. Modify element. Enter your choice(1-3).... 2 Enter the information of new node: 30 press Y to enter more nodes, N to exit... n The list is now 3 5 11 17 38 Want to delete first node(y/n)...y The list is now 5 11 17 38 Want to delete first node(y/n)...n

9:- WAP depending upon user s choice either push or pop an element in a stack array.

#include<iostream.h> #include<conio.h> #include<process.h> int pop(int[], int&); int push(int[], int&, int); void display(int[], int); void main() { int stack[50], item, res, top=-1, choice; cout<<"\n ARRAY STACK MENU"; cout<<"\n1. Insert."; cout<<"\n2. Delete."; cout<<"\n Enter your choice(1-2): "; cin>>choice; char ch = 'y'; if(choice==1) { clrscr(); while(ch=='y'||ch=='Y') { cout<<"\n Enter item for isertion: "; cin>>item; res=push(stack, top, item); if(res == -1) { cout<<"OVERFLOW!!! Aborting!!\n"; exit(1); } cout<<"\n The stack now is:\n"; display(stack,top); cout<<"\n Want to insert more elements?(y/n)... "; cin>>ch; } } if(choice==2) { clrscr(); while(ch=='y'||ch=='Y') { cout<<"\n Enter item for isertion: "; cin>>item; res=push(stack, top, item); if(res == -1) {

cout<<"OVERFLOW!!! Aborting!!\n"; exit(1); } cout<<"\n The stack now is:\n"; display(stack,top); cout<<"\n Want to insert more elements?(y/n)... "; cin>>ch; } cout<<"\nNow deletion of elements begins...\n"; ch='y'; while(ch=='y'||ch=='Y') { res=pop(stack,top); if(res== -1) { cout<<"\nUnderflow!!! Aborting!..\n" ; exit(1); cout<<"\nElement deleted is: "<<res<<endl; cout<<"\n The stack now is: \n"; display(stack,top); } cout<<"\nWant to delete more elements?(y/n): "; cin>>ch; } cout<<"the stack now is\n "; display(stack, top); } }

int push(int stack[], int& top, int ele) { if(top==50-1) return -1; else { top++; stack[top]=ele; } return 0; } int pop(int stack[], int& top) { int ret; if(top== -1) return -1; else { ret=stack[top];

top--; } return ret; } void display(int stack[], int top) { if(top== -1) return; cout<<stack[top]<<"\t"<<endl; for (int i=top-1; i>=0; i--) cout<<stack[i]<<endl; } /*

/*

Output
ARRAY STACK MENU 1. Insert. 2. Delete. Enter your choice(1-2): 1 Enter item for isertion: 15 The stack now is: 15 Want to insert more elements?(y/n)... y Enter item for isertion: 38 The stack now is: 38 15 Want to insert more elements?(y/n)... y Enter item for isertion: 42 The stack now is: 42 38 15 Want to insert more elements?(y/n)... y Enter item for isertion: 89 The stack now is: 89 42 38 15 Want to insert more elements?(y/n)... n ARRAY STACK MENU 1. Insert. 2. Delete. Enter your choice(1-2): 2 Enter item for isertion: 47

The stack now is: 47 Want to insert more elements?(y/n)... y Enter item for isertion: 36 The stack now is: 36 47 Want to insert more elements?(y/n)... y Enter item for isertion: 12 The stack now is: 12 36 47 Want to insert more elements?(y/n)... y Enter item for isertion: 158 The stack now is: 158 12 36 47 Want to insert more elements?(y/n)... n Now deletion of elements begins... Want to delete more elements?(y/n): the stack now is 12 36 47 */ n

10:- WAP depending upon user s choice stackpush() to isert nodes and stackpop() to delete nodes for a linked list implemented stack.

#include<iostream.h> #include<conio.h> #include<process.h> struct node { node void void void void { node *next; } *ptr, *save, *top, *newptr; *create_new_node(int); push(node*); pop(); display(node*); main()

clrscr(); int m, a; char ch = 'y'; top = NULL; while(ch=='y'||ch=='Y') { cout<<"\n Enter information for the new node...."; cin>>m; newptr = create_new_node(m); if(newptr == NULL) { cout<<"\n cannot create new node!!! aborting!!\n"; exit(1); } push(newptr); cout<<"press 'y' to enter more nodes..."; cin>>ch; } cout<<"\n What do you want to do?"; cout<<"\nSTACK NODES MENU"; cout<<"\n1. Insert element."; cout<<"\n2. Delete element."; cout<<"\n Enter your choice(1-2): "; cin>>a; if(a==1) display(top); exit(1); { clrscr(); do {

cout<<"\nNow the stack is"; display(top); cout<<"want to delete more node?(y/n)..."; cin>>ch; } while(ch=='y'||ch=='Y'); getch(); } getch(); } node *create_new_node(int n) { ptr = new node; ptr -> info = n; ptr -> next = NULL; return ptr; } void push(node* np) { if(top == NULL) top = np; else { save = top; top = np; np -> next = save; } } void display(node* np) { while(np != NULL) { cout<< np -> info <<"-> "; np = np -> next; } }

/*

Output
Enter press Enter press information for the new node.... 5 'y' to enter more nodes... y information for the new node....8 'y' to enter more nodes... n

What do you want to do? STACK NODES MENU 1. Insert element. 2. Delete element. Enter your choice(1-2): 2 Now the stack is 5 want to delete more node?(y/n)... n

*/

11:- WAP to search an element in an array with the techniques depending upon user s choice (Linear search and Binary search).

#include<iostream.h> #include<conio.h> int lsearch(int[], int, int); int bsearch(int[], int, int); void main() { int ar[50], item, n, index, ch; cout<<"Enter desired array size(maximum 50)... "; cin>>n; cout<<"\nEnter Array elements: "<<endl; for(int i=0; i<n; i++) cin>>ar[i]; cout<<"\nWhat do you want to do;"; cout<<"\nArray Search Menu."; cout<<"\n1. Linear search."; cout<<"\n2. Binary search."; cout<<"\nEnetr your choice(1-2)... "; cin>>ch; if(ch==1) { cout<<"\nEnter elements to be searched for: "; cin>>item; index=lsearch(ar, n, item); if(index==-1) cout<<"\nSorry!! given element could not be found.\n"; else cout<<"\nElement found at index: "<<index<<", position: "; cout<<(index+1); cout<<endl; } if(ch==2) { cout<<"\nEnter elements to be searched for: "; cin>>item; index=bsearch(ar,n,item); if(index==-1) cout<<"\nSorry!! given element could not be found.\n"; else

cout<<"\nElement found at index: "<<index<<", position: "; cout<<(index+1); cout<<endl; } else cout<<"\wrong choice"; } int lsearch(int ar[], int size, int item) { for(int i=0; i<size; i++) { if (ar[i]==item) return i; } return -1; } int bsearch(int ar[], int size, int item) { int beg, last, mid; beg=0; last=size-1; 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
Enter desired array size(maximum 50)... 5 Enter Array elements: 26 54 87 13 54 What do you want to do; Array Search Menu. 1. Linear search. 2. Binary search. Enetr your choice(1-2)... 2 Enter elements to be searched for: 87 Element found at index: 2, position: 3 */

12:- Write a function that passess the address of time structure and set the total time of two time variable.

#include<iostream.h> #include<conio.h> struct time { int hr, min, sec; } t1={3,50,44}, t2={5,4,51}; time *ptr, *nptr; void main() { clrscr(); ptr=&t1; nptr=&t2; cout<<"Printin time:\t"; cout<<"hour= "<<t1.hr<<" minute= "<<t1.min<<" second= "<<t1.sec; cout<<"\nPrinting time:\t"; cout<<"hour= "<<t2.hr<<" minute= "<<t2.min<<" second= "<<t2.sec; getch(); }

/*

Output
Printin time: Printing time: */ hour= 3 minute= 50 second= 44 hour= 5 minute= 4 second= 51

13:- Write a c++ program that uses the following path:


Person(name, age)------- student(rno, grade)-----graduate student(enrollno, subject)

with the data members as possible.

#include<iostream.h> #include<conio.h> #include<stdio.h> class person { char name[20]; int age; public: void read() { cout<<"Enter name:\t"; cin>>name; cout<<"Enter age:\t"; cin>>age; } void put() { cout<<"\nName: "<<name; cout<<"\nAge: "<<age; } }; class student:public person { int rno; char g[1]; public: void read() { person::read(); cout<<"Enter roll noumber: "; cin>>rno; cout<<"Enter grade of the student: "; cin>>g; } void put() { person::put(); cout<<"\nRoll number: "<<rno; cout<<"\nGrade: "<<g; } }; class graduate_student:public student {

int eno; int sub[5]; public: void read() { student::read(); cout<<"Enter enrollment number:\t "; cin>>eno; cout<<"Subject choose by student:"; cout<<"\nEnter subject code: "<<endl; for(int i=1; i<=5; i++) { cin>>sub[i]; } } void put() { student::put(); cout<<"\nErollment no.: "<<eno; { for(int i=1; i<=5; i++) cout<<"\nSubject code "<<(i+1)<<" : "<<sub[i]; } } }; void main() { clrscr(); graduate_student g; g.read(); g.put(); getch(); }

/*

Output
Enter name: Muhammad mohsin Enter age: 17 Enter roll noumber: 325 Enter grade of the student: A enter enrollment number: 102444 Subject choose by student: Enter subject code: 325 524 625 425 864 Name: Muhammad mohsin Age: 10 Roll number: 325 Grade: A Erollment no.: 102444 Subject code: 325 Subject code: 524 Subject code: 625 Subject code: 425 Subject code: 864 */

14:- WAP to show the working Calculate interest amount.

of

function overloading.

#include<iostream.h> #include<conio.h> void amount(float princ, int time, float rate) { cout<<"\nPrincipal amount: "<<princ; cout<<"\tTime: "<<time; cout<<"\tRate: "<<rate; cout<<"\nInterest Amount: "<<(princ*time*rate)<<"\n"; } void amount(float princ, int time) { cout<<"\nPrincipal amount: "<<princ; cout<<"\tTime: "<<time; cout<<"\tRate: 0.05"; cout<<"\nInterest Amount: "<<(princ*time*0.08)<<"\n"; } void amount(float princ, float rate) { cout<<"\nPrincipal amount: "<<princ; cout<<"\tTime: 5 years"; cout<<"\tRate: "<<rate; cout<<"\nInterest Amount: "<<(princ*5*rate)<<"\n"; } void amount(int time, float rate) { cout<<"\nPrincipal amount: 1500"; cout<<"\tTime: "<<time; cout<<"\tRate: "<<rate; cout<<"\nInterest Amount: "<<(1500*time*rate)<<"\n"; } void amount(float princ) { cout<<"\nPrincipal amount: "<<princ; cout<<"\tTime: 3 years "; cout<<"\tRate: 0.12"; cout<<"\nInterest Amount: "<<(princ*3*0.12)<<"\n"; } void main() { clrscr(); cout<<"Case1"; amount(1100.0); cout<<"Case2"; amount(3000.,2); cout<<"Case3";

amount(900,5,0.12); cout<<"Case4"; amount(8,0.5); cout<<"Case5"; amount(12,0.09); getch(); }

/*

Output
Case1 Principal amount: 1100 Interest Amount: 396 Case2 Principal amount: 3000 Interest Amount: 480 Case3 Principal amount: 900 Interest Amount: 540 Case4 Principal amount: 1500 Interest Amount: 6000 Case5 Principal amount: 1500 Interest Amount: 1620 */ Time: 3 years Rate: 0.12

Time: 2 Rate: 0.05 Time: 5 Rate: 0.12 Time: 8 Rate: 0.5 Time: 12 Rate: 0.09

15:- WAP to illustrate the working of function returning an object.

#include<iostream.h> #include<conio.h> class distance { int feet, inch; public: void getdata(int f, int i) { feet=f; inch=i; } void printit(void) { cout<<"\n"; cout<<feet<<" feet "<<inch<<" inches"<<endl; } distance sum(distance d2); }; distance distance::sum(distance d2) { distance d3; d3.feet=feet+d2.feet+(inch+d2.inch)/12; d3.inch=(inch+d2.inch)%12; return(d3); } void main() { clrscr(); distance length1, length2, total; length1.getdata(5,7); length2.getdata(5,8); total = length1.sum(length2); cout<<"\nMohsin gour's height:"; length1.printit(); cout<<"\nMajid Abul Rahman's height:"; length2.printit(); cout<<"\nSum of both height:"; total.printit(); getch(); }

/*

Output
Mohsin gour's height: 5 feet 7 inches Majid Abul Rahman's height: 5 feet 8 inches Total length: 11 feet 3 inches */

16:- WAP to calculate the two distance passed as reference arguments to a function.

#include<iostream.h> #include<conio.h> class distance { int feet, inch; public: void getdata(int f, int i) { feet=f; inch=i; } void printit(void) { cout<<"\n"; cout<<feet<<" feet "<<inch<<" inches"<<endl; } distance sum(distance d2); }; distance distance::sum(distance d2) { distance d3; d3.feet=feet+d2.feet+(inch+d2.inch)/12; d3.inch=(inch+d2.inch)%12; return(d3); } void main() { clrscr(); distance length1, length2, total; length1.getdata(5,7); length2.getdata(5,8); total = length1.sum(length2); cout<<"\nLength1 :"; length1.printit(); cout<<"\nLength2 :"; length2.printit(); cout<<"\nTotal length:\t"; total.printit(); getch(); }

/*

Output
Length1 : 5 feet 7 inches Length2 : 5 feet 8 inches Total length: 11 feet 3 inches */

17:- Define a class named HOUSING with the given data members.
Private members: reg_no, name, type, cost. Public members: Read_data() to read an object of housing. Display() to show details of an object. Draw_no() to calculate the cost depending on the type of house(2 room flat, 3 room flat or 4 room flat).

#include<iostream.h> #include<conio.h> #include<stdio.h> class housing { int reg_no,type; int cost; char name[50]; public: void readdata() { cout<<"Enetr registration number:\t"; cin>>reg_no; cout<<"Enter name fo the buyer:\t"; cin>>name; cout<<"There are three types of flat. Which one they want to purchase."; cout<<"\n1. 2 roomset flat."; cout<<"\n2. 3 roomset flat."; cout<<"\n3. 4 roomset flat."; cout<<"\nenter your choice(1-3)... "; cin>>type; } void draw() { if(type==1) cost=2000000; cout<<cost; if(type==2) cost=3000000; cout<<cost; if(type==3) cost=4000000; cout<<cost;

} void display() { cout<<"\nRegistration Number:\t"<<reg_no; cout<<"\nName:\t"<<name; cout<<"\nYou have chosen to buy a flat of type: "<<type; cout<<"\nCost of that flat is:"<<cost; draw(); } }; void main() { clrscr(); housing a; char ch='y'; while(ch=='y'||ch=='Y') { a.readdata(); cout<<"\nDo you want to register more flats(y/n)... "; cin>>ch; } a.display(); getch(); }

/*

Output
Enetr registration number: 101 Enter name fo the buyer: muhammad mohsin There are three types of flat. Which one they want to purchase. 1. 2 roomset flat. 2. 3 roomset flat. 3. 4 roomset flat. enter your choice(1-3)... 3 Do you want to register more flats(y/n)... n Registration Number: 101 Name: muhammad mohsin You have chosen to buy a flat of type: 3 Cost of that flat is:4000000 */

18:- WAP to calculate the sum of the following series 1+x/2+x^2/4+ x^n/2n

#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int x=0, z=0; cout<<"enter x: "; cin>>x; cout<<"enter z: "; cin>>z; for(int i=1; i<=x; i++) { z=z+(pow(x,i))/(2*i) ; cout<<"\n Result of equation is: \t"; cout<<(1+z); } getch(); }

/*

Output
1+x/2+x^2/4+x^3/6 */ x^n/2n

19:- WAP to calculate the sum of the following series 1+x+x^2/2!+x^3/3!+ x^n/n!

#include<iostream.h> #include<conio.h> #include<math.h> void main() { clrscr(); int i, j, x=0, f=1, z=0; for(i=1; i<=x; i++) { for(j=1; j>=i; j++) { f=f*j; z=z+(pow(x,i))/f; } cout<<"\n Result of equation is: \t"; cout<<(1+z); } getch(); }

/*

Output
1+x+x^2/2!+x^3/3!+x^4/4+ */ x^n/n!

20:- WAP to read and display information aboutemployees and managers.

#include<iostream.h> #include<conio.h> #include<stdio.h> class employee { private: int eno; char name[10]; char add[50]; char dept[10]; float sal; public: void get_data() { cout<<"Enter employer id number: "; cin>>eno; cout<<"Enter employer name: "; cin>>name; cout<<"Enter employer salary: "; cin>>sal; cout<<"Enter department: "; cin>>dept; cout<<"Enter employer's address: "; cin>>add; } void read_data() { cout<<"\nID NO.:\t"<<eno; cout<<"\nName:\t"<<name; cout<<"\nSalary:\t"<<sal; cout<<"\nDepartment:\t"<<dept; cout<<"\nAddress:\t"<<add; } }; class manager : public employee { private: char name[10]; public: void get_data() { cout<<"\nEnter name of manager:\t"; gets(name);

employee::get_data(); } void read_data() { cout<<"Manager name:\t"<<name; employee::read_data(); } }; void main() { clrscr(); manager m; m.get_data(); cout<<"\nDetails of the employee working under manager:"<<endl; m.read_data(); getch(); }

/*

Output
Enter Enter Enter Enter Enter Enter name of manager: mohsin gouri employer id number: 20122 employer name: asad employer salary: 10000 department: scince employer's address: jamia nagar

Details of the employee working under manager: Manager name: mohsin gouri ID NO.: 20122 Name: asad Salary: 10000 Department: scince Address: jamia nagar */

21:- WAP with a help of functions to encode a string that is passed to it. The string should get converted into an unrecognizable form.

#include<iostream.h> #include<conio.h> #include<stdio.h> change(m); void main() { clrscr(); long double d; char b[20]; cout<<"Enter the sentence... "; gets(b); d=change(b); cout<<d; getch(); } long double change(char m) { for(int i=0; i<=20; i++) { int c; a[i]=char(m[i]); c=2*a[i]; cout<<"the unrecognizable form is:"<<endl; cout<<c[i]; } return c; }

/*

Output
Enter the sentence... hi how are you the unrecognizable form is: 56413413213135634341 */

22:- WAP using function that takes a string reversed string.

and return a

#include<iostream.h> #include<conio.h> #include<string.h> #include<stdio.h> void main() { clrscr(); char string[50]; int len, i; cout<<"Enter the sentence maximum 50 letters... "; cin.getline(string,100); len=strlen(string); for(i=len; i>=0; i--) { cout<<"\nNow the string is"<<endl; cout.write(string[i],len); } getch(); }

/*

Output
Enter the sentenc maximum 50 letters... mohsin gouri Now the string is iroug nishom */

23:- WAP to illustrate the use of object pointer.

#include<iostream.h> #include<conio.h> class date { int dd,mm,yy; public: date() { dd=mm=yy=1; } void getdata(int i, int j, int k) { i=dd; j=mm; k=yy; } void putdata(void) { cout<<"\nDate is"<<endl; cout<<dd<<"/"<<mm<<"/"<<yy; } }; void main() { clrscr(); date *dp, d1; d1.getdata(12,1,2012); cout<<"\nDate:\t"; d1.putdata(); dp=d1; cout<<"\n Date:\t"; dp->putdata(); dp->getdata(13,1,2012); d1.putdata(); dp->putdata(); getch(); }

/*

Output
Date: 12/1/2012 Date 13/1/2012 */

24:- WAP that invokes a function to increase the basic salary of an employee if the employee has experience of 10 or more years.

#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> struct employee { int emp_no; char name[15]; float basic_sal; int exp; } emp, mgr, *ptr; void display(employee *emp); void increase(employee *emp); void main() { clrscr(); cout<<"Enter employee number: "; cin>>emp.emp_no; cout<<"Enter name: "; cin>>emp.name; cout<<"Enter basic salary: "; cin>>emp.basic_sal; cout<<"Enter year of experience: "; cin>>mgr.exp; *ptr=mgr; cout<<"\n Employee details before increment in salary\n"; display(ptr); increase(ptr); cout<<"\n Details of employee after increment in salary\n"; display(ptr); getch(); } void display(employee * age) { int len=strlen(emp -> name); cout<<"\n Employee number: "<<emp->emp_no; cout<<"\nName: "; cout.write(age->name,len); cout<<"\nBasic salary: "<<emp->basic_sal; cout<<"\nExperience: "<<emp->exp; } void increase()

{ if( emp-> exp > 10) emp->basic_sal +=500; }

/*

Output
Enter Enter Enter Enter employee number: 20081511 name: muhammad mohsin basic salary: 10000 experience: 11

Employee details before increment in salary Employee number: 20081511 Name: muhammad mohsin Basic salary: 10000 Experience: 11 Details of employee after increment in salary Employee number: 20081511 Name: muhammad mohsin Basic salary: 10500 Experience: 11 */

25:- WAP to search the name and address of person having age more than 30 years in the data list of person.
#include<iostream.h> #include<conio.h> #include<stdio.h> #include<string.h> struct person { int age; char name[15]; float basic_sal; char add[50]; } emp; void display(person *age); void increase(person *age); void main() { clrscr(); emp mgr *ptr ; cout<<"Enter name: "; cin>>emp.name; cout<<"Enter basic salary: "; cin>>emp.basic_sal; cout<<"Enter address: "; cin>>emp.add; cout<<"Enter age:"; cin>>emp.mgr.age; ptr=mgr; cout<<"\n Person details"; increase(ptr); display(ptr); getch(); } void display(person*age) { { int len=strlen(emp->name); cout<<"\nName: "; cout.write(age->name,len); cout<<"\nBasic salary: "<<age->basic_sal; cout<<"\nAddress: "<<age->add; cout<<"\nAge: "<<age->age; } void increase() { if(age->age=30) } }

/*

Output
Enter Enter Enter Enter age: 31 name: muhammad mohsin basic salary: 10000 address: jasola jamia nagar new delhi

Person details Age: 31 Name: muhammad mohsin Basic salary: 10000 Adddress: jasola jamia nagar new delhi */

26:- WAP to implement searching in a file that has records maintained through structures.

#include<fstream.h> #include<conio.h> struct student { int roll; char name[25]; char cl[4]; float marks; char grade; } s1; void main() { clrscr(); int rn; char found='n'; ifstream fin("student.dat", ios::in); cout<<"Enter roll number to be searched\t"; cin>>rn; while(!eof()) { fin.read((char*)&s1, sizeof(s1)); if(s1.roll==rn) { cout<<"details"; cout<<s1.name<<s1.roll<<s1.marks<<s1.grade; found='y'; break; } if(found==rn) cout<<"roll no. not found in file."; fin.close(); } getch(); }

/*

Output
Enter roll no. to be searched 325 deatais Muhammad mohsin 325 25 A Enter roll no. to be searched 0 roll no. not found in file. */

27:- WAP in which function transfer() in c++, that would copy all those records which are having area code as MUB from phone.txt to phonebook.txt.

#include<iostream.h> #include<conio.h> #include<fstream.h> class phonelist { char name[30]; int area_code; char add[100]; int phone_no; public: void register() { cout<<"Enter name..\t"; cin>>name; cout<<"Enter address..\t"; cin>>add; cout<<"Enter area code..\t"; cin>>area_code; cout<<"Enter phone number..\t"; cin>>phone_no; } void show() { cout<<"\nName:\t"<<name; cout<<"\nAddress:\t"<<add; cout<<"\nArea code:\t"<<area_code; cout<<"\nPhone number:\t"<<phone_no; } int checkcode(char ac[]) { return strcmp(area_code,ac); } void transfer() { ifstrean fin; ofstream fout; phonelist ph; fin.open("phone.dat", ios::in|ios::binary) fout.open("phonebook.txt", ios::out|ios::binary) while(!fin.eof()) { fin.read((char*)&ph, sizeof(ph)); if(ph.checkcode("mub")==o)

fout.write((char*)&ph, sizeof(ph)); } fin.close(); fout.close(); } }; void main() { clrscr(); phonelist p; p.register(); p.transfer(); p.show(); getch(); }

/*

Output
Enter Enter Enter Enter name.. muhammad mohsin address.. jasola jamia nagar new delhi area code.. 110025 phone number.. 9871048306

Name: muhammad mohsin Address: jasola jamia nagar new delhi Area code: 1100254 Phone number: 9871048306 */

28:- WAP to search for a given character inside a string and print the string from the point of match showing the function with the concept of function returning pointers.

#include<iostream.h> #include<conio.h> #include<string.h> char *match(char, char); void main() { clrscr(); char string[50], *cp, ch; cout<<"Enter a sentence of maximum 50 letters.\t "; cin.getline(string,50); cout<<"Enter a character to be searched.\t"; cin>>ch; cp=NULL; cp = match(ch,string); if(*cp) { cout<<"\n"; for(cp; cp!='\0'; cp++) cout<<cp; } cout<<"\nno match found"; getch(); } char *match(char c, char *s) { while((c!=*s)&&*s) s++; return (s); }

/*

Output
Enter a sentence maximum 50 letters. Hi, i am mohsin gouri Enter a character to be searched. i i am mohsin gouri */

29:- WAP in c++ to print the product of each row of a 2-D integer array passed as argument of the function.

#include<iostream.h> #include<conio.h> void rowproduct(a[][], b, c); void main() { clrscr(); int m[3][4]; cout<<"enter the array"; for(int i=1; i<=3; i++) { for(int j=1; j<=4; j++) { cin>>m[i][j]; } } d = rowproduct(m[][], 3, 4); cout<<"d"; getch(); } void rowproduct(int a[3][4], int r, int f) { int prod[r]; { for(int i=1; i<=r; i++) { prod[i]=1; for(int j=0; j<=f; j++) prod[i]*=a[i][j]; cout<<"prduct of row is"; cout<<prod[i]; } } }

/*

Output
Enter the 5 7 1 9 3 4 array 6 4 8 2 6 5

product of the array is 840 144 360 */

30:- WAP in c++ using a function swaparray() to swap the first row elements with the last row elements, for a 2-D integer array passed as the argument of the function.

#include<iostream.h> #include<conio.h> void swaparr(int a); void main() { clrscr(); int a,m,n, c[5][5]; cout<<"enter the array"; for(int i=1; i<=5; i++) { for(int j=1; j<=5; j++) cin>>c[i][j]; cout<<"the result is\n"; } a[m][n] = swaparr(c[m][n]); getch(); } void swaparr(int a[5][5]) { for(int i=0; i<=o; i++) { for(int j=0; j<=p; j++) B[1][k]=a[1][n-1]; A[1][n-1]=a[m-1][n-1]; A[m-1][n-1]=B[1][k]; cout<<a[i][j]; } }

/*

Output
Enter the 5 7 1 9 3 4 array 6 4 8 2 6 5

product of the array is 840 144 360 */

You might also like