Anda di halaman 1dari 34

Q. WAP to illustrate single inheritance.

#include <iostream.h> #include <conio.h> #include <stdio.h> class baseemp { private: int empcode; char empname[25]; char empdesig[15]; public: void input_data(void); void display(void); }; class derivedemp: public baseemp { double salary, gross; float da, hra; public: void input_salary(void); double cal_da(void); double cal_hra(void); double cal_gross(void); void print_salary(void); }; void baseemp::input_data(void) { cout << "Enter employee code : "; cin >> empcode; cout << "Enter employee name : "; gets(empname);

cout << "Enter employee designation : "; gets(empdesig); } void baseemp::display(void) {

cout << "\nEmployee code : " << empcode; cout << "\nEmployee name : " << empname; cout << "\nDesignation : " << empdesig; } void derivedemp::input_salary(void) { input_data(); cout << "Enter salary : "; cin >> salary; da = cal_da(); hra = cal_hra(); gross = cal_gross(); } double derivedemp::cal_da(void) { return(1.20 * salary); } double derivedemp::cal_hra(void) { return(0.15 * salary); } double derivedemp::cal_gross(void) { return(salary + da + hra); } void derivedemp::print_salary(void)

{ display(); cout << "\nBasic salary : " << salary; cout << "\nDearness allowance : " << da; cout << "\nHouse rent allowance : " << hra; cout << "\nGross : " << gross; } void main() { clrscr(); derivedemp emp; emp.input_salary(); cout << "Here is the employee information : \n"; for (int i = 1; i <= 32; i++) cout << "="; emp.print_salary(); }

OUTPUT:

Q.WAP to illustrate multilevel inheritance

#include <iostream.h> #include <conio.h> #include <stdio.h> class Employee { private: int empcode; char empname[25]; char empdesig[15]; float empsalary; public: void empinput_data(void); void empdisplay(void); }; class Customer : public Employee { private: int custcode; char custname[25]; char custdesig[15]; double balance; public: void custinput_data(void); void custdisplay(void); }; class Emp_Cust : private Customer { public: void get_data(); void show_data();

}; void Emp_Cust::get_data(void) { empinput_data(); custinput_data(); } void Emp_Cust::show_data() { empdisplay(); custdisplay(); } void Employee::empinput_data(void) { cout << "\Enter employee information : \n"; cout << "Enter code : "; cin >> empcode; cout << "Enter name : "; gets(empname); cout << "Enter designation : "; gets(empdesig); cout << "Enter salary : "; cin >> empsalary; } void Customer::custinput_data(void) { cout << "\nEnter customer information : \n"; cout << "Enter code : "; cin >> custcode; cout << "Enter name : "; gets(custname); cout << "Enter designation : "; gets(custdesig);

cout << "Enter balance : "; cin >> balance; } void Employee::empdisplay(void) { cout << "\nHere is the employee information : \n"; for (int i=0; i<=35; i++) cout << "="; cout << "\nCode : " << empcode; cout << "\nName : " << empname; cout << "\nDesignation : " << empdesig; cout << "\nSalary : " << empsalary; } void Customer::custdisplay(void) { cout << "\nHere is the customer information : \n"; for (int i=0; i<=35; i++) cout << "="; cout << "\nCode : " << custcode; cout << "\nName : " << custname; cout << "\nDesignation : " << custdesig; cout << "\nBalance : " << balance; } void main() { clrscr(); Emp_Cust empcust; empcust.get_data(); empcust.show_data(); }

OUTPUT:

Q. WAP to illustrate selection search

#include <iostream.h> #include <conio.h> main() { int range[100], loc, lowest, T, N, i, j, x; clrscr(); cout << "Enter the number of elements in your array : "; cin >> N; cout << "Enter the array elements : \n"; for (i = 0; i < N; i++) { cout << "Location " << i + 1 << ". Value : "; cin >> range[i]; } for (i = 0; i < N - 1; i++) { lowest = range[i]; loc = i; for (j = i + 1; j < N; j++) { if (lowest > range[j]) { loc = j; lowest = range[j]; } } T = range[i]; range[i] = range[loc]; range[loc] = T; }

cout << "\nThe sorted list is ... \n"; for (i = 0; i < N; i++) cout << range[i] << endl; return 0; }

OUTPUT:

Q. WAP to swap two numbers using third variable

#include <iostream.h> main() { int a, b; void swap(int *x, int *y); cin >> a >> b; swap(&a, &b); cout << a << " " << b; return 0; } void swap(int *x, int *y) { int t; t = *x; *x = *y; *y = t; }

OUTPUT:

Q. WAP to illustrate Bubble Sort

#include <iostream.h> #include <conio.h> #include <stdio.h> int array[8] = {25, 57, 48, 37, 12, 92, 86, 33}; void bubble(int a[], int N); int main(void) { int i; putchar('\n'); cout << "Unsorted elements...\n"; for (i = 0; i < 8; i++) cout << array[i] << ' '; bubble(array, 8); putchar('\n'); cout << "Sorted elements...\n"; for (i = 0; i < 8; i++) cout << array[i] << '\t'; return 0; } void bubble(int a[], int N) { int i, j, temp; 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; } }

OUTPUT:

Q. WAP to illustrates the basic operation of add stack, delete stack and shows stack using linked list. The stack contains data of type integer.

#include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ctype.h> 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 void main() { node *top; int val, choice; char opt = 'Y'; top = NULL; clrscr(); do { cout << "\n\t\t Main Menu"; cout << "\n\t1. Addition of Stack"; cout << "\n\t2. Deletion from Stack"; cout << "\n\t3. Traverse of Stack"; cout << "\n\t4. Exit from Menu"; cout << "\n\nEnter your choice from above "; cin >> choice;

switch (choice) { case 1: do { cout << "Enter the value to be added in the stack "; cin >> val; top = push(top, val); cout << "\nDo you want to add more elements <Y/N> ? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 2: opt = 'Y'; do { top = pop(top,val); if (val != -1) cout << "Value deleted from Stack is " << val; cout << "\nDo you want to delete more elements <Y/N> ? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 3: show_Stack(top); break; case 4: exit(0); } } while (choice != 4); }

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); } node *pop(node *top,int &val) { node *temp; clrscr(); if (top == NULL ) { cout << "Stack Empty "; val = -1; } else { temp = top; top = top->link; val = temp->data; temp->link = NULL; delete temp; }

return (top); } void show_Stack(node *top) { node *temp; temp = top; clrscr(); cout << "The values are \n"; while (temp != NULL) { cout << "\n" << temp->data; temp = temp->link; } }

OUTPUT:

Q. WAP illustrates the basic operation of add queue, delete queue and show queue using linked list. The queue contains data of type integer.

#include <iostream.h> #include <stdio.h> #include <conio.h> #include <stdlib.h> #include <ctype.h> struct node { int data; node *link; }; node *add_Q(node *rear, int val); node *del_Q(node *front, int &val); void show_Q(node *front); void main() { node *front, *rear; int val, choice; char opt = 'Y'; front = rear = NULL; clrscr(); do { cout << "\n\t\t Main Menu"; cout << "\n\t1. Addition of Queue"; cout << "\n\t2. Deletion from Queue"; cout << "\n\t3. Traverse of Queue"; cout << "\n\t4. Exit from Menu"; cout << "\n\nEnter your choice from above "; cin >> choice;

switch (choice) { case 1: do { cout << "Enter the value to be added in the queue "; cin >> val; rear = add_Q(rear, val); if (front == NULL) front = rear; cout << "\nDo you want to add more elements <Y/N> ? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 2: opt = 'Y'; do { front = del_Q(front, val); if (front == NULL) rear = front; if (val != -1) cout << "Value deleted from Queue is " << val; cout << "\nDo you want to delete more elements <Y/N> ? "; cin >> opt; } while (toupper(opt) == 'Y'); break; case 3: show_Q(front); break; case 4: exit(0);

} } while (choice != 4); } node *add_Q(node *rear, int val) { node *temp; temp = new node; temp->data = val; temp->link = NULL; rear->link = temp; rear = temp; return (rear); } node *del_Q(node *front, int &val) { node *temp; clrscr(); if (front == NULL) { cout << "Queue Empty "; val = -1; } else { temp = front; front = front->link; val = temp->data; temp->link = NULL; delete temp; } return (front);

} void show_Q(node *front) { node *temp; temp = front; clrscr(); cout << "The Queue values are"; while (temp != NULL) { cout <<"\n"<< temp->data; temp = temp->link; } }

OUTPUT:

Q. WAP to demonstrate a deletion program of employee structure using emp_code.

#include <fstream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> struct Employee { int empcode; char empname[20]; char empdesig[15]; float empsalary; }; Employee emp; void main() { clrscr(); int xemp_code; int flag = 0; fstream empfile,tempfile; empfile.open("EMP.dat", ios::binary||ios::nocreate); tempfile.open("TEMP.dat", ios::out); clrscr(); cout << "\nEnter employee code to Delete "; cin >> xemp_code; while (empfile) { if (!empfile) exit(0); empfile.read((char *) &emp, sizeof(emp)); if (emp.empcode == xemp_code)

{ flag = 1; } else tempfile.write((char *) &emp, sizeof(emp)); } empfile.close(); tempfile.close(); if (flag == 1) cout << "Record deleted !!! "; else cout << "Record not found !!!"; fstream xfile,yfile; xfile.open("TEMP.dat", ios::binary||ios::nocreate);

yfile.open("EMP.dat", ios::out); while (xfile) { if (!xfile) exit(0); xfile.read((char *) &emp, sizeof(emp)); yfile.write((char *) &emp, sizeof(emp)); } xfile.close(); yfile.close(); }

OUTPUT:

Q. WAP to illustrate palindrome

#include<conio.h> int main() { clrscr(); char string[80],c; cout<<"Enter a string(max.79 characters)\n"; cin.getline(string,80); for(int len=0;string[len]!='\0';++len); int i,j,flag=1; for(i=0,j=len-1;i<len/2;++i,--j) { if(string[i]!=string[j]) { flag=0; break; } } if(flag) cout<<"It is palindrome:\n"; else cout<<"It is not a palindrome:\n"; return 0; }

OUTPUT:

Q. WAP to input no. of weeks day and translate to its equivalent name of the week.

#include<iostream.h> #include<conio.h> void main() { int daw; cout<<"Enter number of week's day"; cin>>daw; switch(daw) { case 1:cout<<"\n"<<"sunday"; break; case 2:cout<<"\n"<<"monday"; break; case 3:cout<<"\n"<<"tuesday"; break; case 4:cout<<"\n"<<"wednesday"; break; case 5:cout<<"\n"<<"thursday"; break; case 6:cout<<"\n"<<"friday"; break; case 7:cout<<"\n"<<"saturday"; break; default:cout<<"\n"<<"Wrong number of day"; break; } getch(); }

OUTPUT:

Anda mungkin juga menyukai