Você está na página 1de 43

1Computer Science Archana Jain

Class XII

Practical Programs

1. Program to add two matrices A and B into 3rd matrix C #include<iostream.h> #include<stdio.h > #include<conio.h > void main() { int a[10][10],b[10][10],i,j,m,n; clrscr(); cout<<"Enter the order of the matrix A (M X N) :\n"; cin>>m>>n; cout<<"\nEnter the elements of the matrix A :\n"; for(i=0;i < m;i++) for(j=0;j < n;j++) cin >>a[i][j]; cout<<"\nYou have entered the following matrices "; cout<<"\n-------------------------------------------\n"; cout<<"\nMatrix A\n"; for(i=0;i < m;i++) {for(j=0;j < n;j++) cout<<a[i][j]<<" "; cout<<endl; } cout<<"\n\nThe transpose of matrix A is:\n\n"; for(i=0;i < m;i++) for(j=0;j < n;j++) b[j][i]=a[i][j]; for(i=0;i < n;i++) { for(j=0;j < m;j++) cout<<b[i][j]<<" "; cout<<endl; } } 2. Program to Multiply two matrices A and B into 3rd matrix C #include<iostream.h> #include<stdio.h > #include<conio.h > void main()

2Computer Science Archana Jain

Class XII

Practical Programs

{ int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q; clrscr(); cout<<"Enter the order of the matrix A (M X N) :\n"; cin>>m>>n; cout<<"Enter the order of the matrix B (P X Q ) :\n"; cin>>p>>q; if(n==p) { cout<<"Matrix can be Multiplied "; cout<<"\nEnter the elements of the matrix A :\n"; for(i=0;i < m;i++) for(j=0;j < n;j++) cin >>a[i][j]; cout<<"\nEnter the elements of the matrix B :\n"; for(i=0;i < p;i++) for(j=0;j < q;j++) cin>>b[i][j]; cout<<"\nYou have entered the following matrices "; cout<<"\n-------------------------------------------\n"; cout<<"\nMatrix A\n"; for(i=0;i < m;i++) {for(j=0;j < n;j++) cout<<a[i][j]<<" "; cout<<endl; } cout<<"\nMatrix B\n"; for(i=0;i < p;i++) {for(j=0;j < q;j++) cout<<b[i][j]<< " "; cout<<endl; } cout<<"\n\nThe Product of the matrix A and B is:\n\n"; for(i=0;i < m;i++) for(j=0;j < q;j++) { c[i][j]=0; for (int k=0;k< n ; k++) c[i][j]+=a[i][k]*b[k][j]; }

3Computer Science Archana Jain

Class XII

Practical Programs

for(i=0;i < m;i++) { for(j=0;j < q;j++) cout<<c[i][j]<<" "; cout<<endl; } } else cout<<"Matrix cannot be multiplied"; } 3. Program to Transpose a matrix a[M][N] #include<iostream.h> #include<stdio.h > #include<conio.h > void main() { int a[10][10],b[10][10],i,j,m,n; clrscr(); cout<<"Enter the order of the matrix A (M X N) :\n"; cin>>m>>n; cout<<"\nEnter the elements of the matrix A :\n"; for(i=0;i < m; i++) for(j=0;j < n;j++) cin >>a[i][j]; cout<<"\nYou have entered the following matrices "; cout<<"\n-------------------------------------------\n"; cout<<"\nMatrix A\n"; for(i=0;i < m;i++) {for(j=0;j < n;j++) cout<<a[i][j]<<" "; cout<<endl; } cout<<"\n\nThe transpose of matrix A is:\n\n"; for(i=0;i < m;i++) for(j=0;j < n;j++) b[j][i]=a[i][j]; for(i=0;i < n;i++) { for(j=0;j < m;j++) cout<<b[i][j]<<" ";

4Computer Science Archana Jain

Class XII

Practical Programs

cout<<endl; } } 4. Given that an EMPLOYEE class contains the following members: Data Members: Employee Number, Employee Name, Basic, DA, HRA ,Net_Sal Member Functions: to read data, to calculate Net_Sal and to print data members Write a C++ program to read data on n number of employees and compute the Net_Sal of each employee (DA = 52% of Basic and Income Tax = 30% of the gross salary) Hint #include <iostream.h> #include <conio.h> class EMPLOYEE { char emp_number[10], emp_name[10]; float basic,DA,HRA, net_sal; public: void Read_Data(); void Cal_net; //calculates the net salary void Display_Data(); //Displays the data }; void EMPLOYEE::Read_Data() { cout << "Enter the Emp Number and Name" << endl; cin >> emp_number >> emp_name; cout << "Enter the Basic Salary" << endl; cin >> basic; } void EMPLOYEE::Cal_net { float Gross_Sal; DA = (52*basic)/100; Gross_Sal = basic+DA; HRA = (30*Gross_Sal)/100; net_sal = Gross_Sal-HRA; } void EMPLOYEE::Display_Data() { cout << "Emp Name:" << emp_name << "\tEmp Number:" << emp_number; cout << "\t Net Salary:" << net_sal << endl;

5Computer Science Archana Jain

Class XII

Practical Programs

} void main() {int n,i; clrscr(); cout << "Enter the number of emps: " ; cin >> n; cout << endl; EMPLOYEE Emp[10]; cout << "Enter emp data" << endl; for(i=0; i<n; i++) Emp[i].Read_Data(); for(i=0;i<n;i++) { Emp[i].Cal_net; Emp[i].Display_Data(); } } 5. Define a STUDENT class with Name, and Marks in 3 tests of a subject. Declare an array of 10 STUDENT objects. Using appropriate functions, find the average of the two better marks for each student. Print the roll no, Name and the average marks of all the students. #include <iostream.h> #include <stdio.h> #include <conio.h> class STUDENT { private: char rno[10], Name[10]; float marks1, marks2, marks3; //marks for the three subjects float average_marks; //Average marks for the best two public: void Read_Data(); void cal_avg(); void Display_Data(); }; void STUDENT::Read_Data() {cout << "Enter the name and rno :" ; cin >> Name >> rno; cout << endl; cout << "Enter marks1, marks2, marks3 : " ; cin >> marks1 >> marks2 >> marks3; cout << endl; }

6Computer Science Archana Jain

Class XII

Practical Programs

void STUDENT::cal_avg() { int smallest; if( (marks1 < marks2) && (marks1 < marks3) ) average_marks = (marks2 + marks3)/2; else if (marks2 < marks3) average_marks = (marks1 + marks3)/2; else average_marks = (marks1 + marks2)/2; } void STUDENT::Display_Data() { cout << "Rno:" << rno << "\tName:" << Name << "\tAverage Marks:"; printf("%0.2f\n",average_marks); } void main() { STUDENT student[10]; clrscr(); int n; cout<<"Enter the number of students : "; cin>>n; for(int i=0;i<n;i++) student[i].Read_Data(); for(i=0;i<n;i++) {student[i].cal_avg(); student[i].Display_Data(); } } 6. Write a C++ program to create a class called STUDENT with data members USN, Name and Age. Using inheritance, create the classes UGSTUDENT and PGSTUDENT having fields as Semester, Fees and Stipend. Enter the data for at least 5 students. Find the semester-wise average age for all UG and PG students separately. Hint #include<iostream.h> #include<conio.h> class student {

7Computer Science Archana Jain

Class XII

Practical Programs

char name[20]; int marks; public: student(); ~student(); void getsdata(); int getmarks(); }; student::student() {} student::~student() {} int student::getmarks() { return marks; } void student::getsdata() { cout<<endl<<"Name:"; cin>>name; cout<<"marks:"; cin>>marks; } class ugstudent:public student { private: int sem,fee; public: ugstudent(); ~ugstudent(); void getugdata(); int getsem(); }; ugstudent::ugstudent() {} ugstudent::~ugstudent() {} void ugstudent::getugdata() {

8Computer Science Archana Jain

Class XII

Practical Programs

getsdata(); cout<<"Semester:"; cin>>sem; cout<<"Fee:"; cin>>fee; } int ugstudent::getsem() { return sem; } class pgstudent:public student { private: int sem,fee; public: pgstudent(); ~pgstudent(); void getpgdata(); int getsem(); }; pgstudent::pgstudent() {} pgstudent::~pgstudent() {} void pgstudent::getpgdata() { student::getsdata(); cout<<"Semester:"; cin>>sem; cout<<"Fee:"; cin>>fee; } int pgstudent::getsem() { return sem; } main() { ugstudent u[10];

9Computer Science Archana Jain

Class XII

Practical Programs

pgstudent p[10]; int i,n; clrscr(); cout<<endl<<"Enter number of students:"; cin>>n; for(i=1;i<=n;i++) { cout<<endl<<"Enter the details of UG student"<<i<<endl; u[i].getugdata(); } for(int s=1;s<=8;s++) { float sum=0; int flag=0,cou=0; for(i=1;i<=n;i++) if(u[i].getsem()==s) {sum=sum+u[i].getmarks(); flag=1; cou++; } if(flag==1) cout<<endl<<s<<"\nSemester "<<" average marks is "<<sum/cou; } for(i=1;i<=n;i++) { cout<<endl<<"\nEnter the details of PG student"<<i<<endl; p[i].getpgdata(); } for(s=1;s<=8;s++) { float sum=0; int flag=0,cou=0; for(i=1;i<=n;i++) if(p[i].getsem()==s) { sum=sum+p[i].getmarks(); flag=1; cou++; } if(flag==1)

10Computer Science Archana Jain

Class XII

Practical Programs

cout<<endl<<s<<" Semester "<<" average marks is "<<sum/cou; } getch(); return 0; } 7. Write a program to delete an element from the array located at the position pos Hint #include<iostream.h> #include<conio.h> #define size 10 void main() { int i,n,pos,t,back,list[size]; clrscr(); cout<<"\nEnter the number of elements:"; cin>>n; cout<<"\nEnter the Array elements :"; for (i=0; i<n ; i++) cin>>list[i]; cout<<"\nEnter the position from where value to be deleted:"; cin>>pos; pos--; if (pos>n) cout<< " Invalid location : value cannoy be deleted "; else { t = list[pos]; back= pos; while (back<n) { list[back]= list[back+1]; back++; } } n--; cout<< "\n The deleted value is :"<<t; cout<<"\n\nFinal array after deletion is :"; for (i=0;i<n; i++)

11Computer Science Archana Jain

Class XII

Practical Programs

cout <<endl<<list[i]; } 8. Write a C++ program to perform the following string operations using pointers according to the users choice : a. To calculate the length of the string. b. To count the number of vowels in a string c. To compare two strings d. To copy one sting into another e. Exit Hint #include<iostream.h> #include<stdio.h> #include<conio.h> #include<process.h> char *str1, *str2; void main() { int choice; int count; do { clrscr(); cout<<"1. Calculate the length of the string \n"; cout<<"\n2. To count the number of vowels in the given string\n"; cout<<"\n3. To compare two given strings\n"; cout<<"\n4. To copy one string to another string \n"; cout<<"\n5. Exit \n"; cout<<"\nEnter your choice :"; cin>>choice; switch (choice) { case 1: cout << "Enter a String 1:"; gets(str1); for ( ; *str1!='\0'; *str1++) count++; cout<<"\nThe length of the string is :"<< count; getch();

12Computer Science Archana Jain

Class XII

Practical Programs

break; case 2: cout << "Enter a String 1:"; gets(str1); int vowels = 0; for (char *Ptr = str1; *Ptr!='\0'; Ptr++) { switch (*Ptr) { case 'A': case 'a': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': vowels++; break; } } cout <<str1 << " has " << vowels << " vowels" << endl; getch(); break; case 3: cout << "Enter a String 1:"; gets(str1); cout<<"Enter String 2:"; gets(str2); char *ptr1 , *ptr2; for (ptr1=str1,ptr2=str2;(*ptr1==*ptr2 && *ptr1!='\0'&& ='\0');ptr1++,ptr2++); if (*ptr1=='\0'&& *ptr2=='\0') cout<<"\nTwo strings are identical"; else cout<<"\nStrings are not identical"; getch(); break; case 4: cout << "Enter a String 1:"; gets(str1); char *p=new char; str2=p;

*ptr2!

13Computer Science Archana Jain

Class XII

Practical Programs

while (*str1!='\0') *p++ = *str1++; *p = '\0'; cout<<"The copied string is "<<str2; getch(); break; case 5: exit(0); } }while (choice!=5); } 9. Linked Queue implementation Hint #include <iostream.h> #include<conio.h> struct node { int data; node *next; }; node *front,*rear; void enqueue(int val) { node *x; x=new node; x->data=val; x->next=NULL; if (front==NULL) front=rear=x; else { rear->next=x; rear= x; } } int dequeue() {

14Computer Science Archana Jain

Class XII

Practical Programs

if (front==NULL) { cout<"\nQUEUE EMPTY"; return NULL; } node *x; int val; x = front; val=front->data; front=front-> next; delete x; return val; } void display() { if(front==NULL) return; node *x; x=front; cout <<"\nThe content of the Queue are :\n"; while(x!=NULL) { cout<<x->data<<" "; x=x->next; } }; int main() { front=NULL; rear=NULL; clrscr(); enqueue(5); enqueue(15); enqueue(20); enqueue(25); display(); cout<<"\nItem deleted = "<<dequeue()<<endl; cout<<"\nItem deleted= "<<dequeue()<<endl; cout<<"\nItem deleted = "<<dequeue()<<endl; display();

15Computer Science Archana Jain

Class XII

Practical Programs

return 0; } 10. Write a program to implement a class Stack implementation using arrays . #include<iostream.h> #include<conio.h> #include<stdio.h> #define MAX 20 class stack { int top; int stack[MAX]; public: stack() { top=-1; } void push(int val); int pop(); void display(); }; void main() { stack s; int value,choice; do { clrscr(); cout<<"1. Push in the stack \n"; cout<<"\n2. Pop from the stack\n"; cout<<"\n3. Display the content of stack\n"; cout<<"\n4. Quit\n"; cout<<"\nEnter your choice :"; cin>>choice; switch (choice) { case 1: cout<<"\nEnter the value to be pushed :\n"; cin>>value;

16Computer Science Archana Jain

Class XII

Practical Programs

s.push(value); break; case 2: value=s.pop(); cout <<"\n The popped value is : "<<value; break; case 3: cout <<"\n\n The stack is \n"; s.display(); break; } } while (choice!=4); } void stack::push(int val) { if (top==MAX-1) { cout<<" \nStack is full " ; return; } else { top++; stack[top] = val; } } int stack::pop ( ) { int value; if (top== -1) { cout<<" \nStack is Empty " ; return 0; } else { value= stack[top]; top--; } return value; } void stack::display() { if (top>=0) { for (int i=top;i>=0;i--)

17Computer Science Archana Jain

Class XII

Practical Programs

cout<<stack[i]<<"

";

} else cout<<"\nStack is empty "; cout <<"\n\nPress any key to continue"; getch(); fflush(stdin); } 11. Write a Program to implement Stack using linked list Hint #include <iostream.h> struct node { int data; node *next; }; node* top;

void push(int val) { node *x; x=new node; x->data=val; x->next=top; top= x; } int pop() { if (top==NULL) { cout<<"\nSTACK EMPTY"; return NULL; } node *x;

18Computer Science Archana Jain

Class XII

Practical Programs

int val; x = top; val=x->data; top=top-> next; delete x; return val; } void display() { if(top==NULL) return; node *x; x=top; while(x!=NULL) { cout<<x->data<<" "; x=x->next; } }; int main() { push(11); push(101); push(99); push(78); cout<<"Item Popped = "<<pop()<<endl; cout<<"Item Popped = "<<pop()<<endl; cout<<"Item Popped = "<<pop()<<endl; return 0; } program to implement Queue with Array implementation

12.

#include<iostream.h> #include<conio.h> #include<stdio.h> #define MAX 20 class queue {

19Computer Science Archana Jain

Class XII

Practical Programs

int front ,rear; int queue[MAX]; public: queue() { front=-1; rear=-1; } void enqueue(int val); int dequeue(); void display(); }; void main() { int value,choice; queue Q; do { clrscr(); cout<<"1. Add element in the Queue \n"; cout<<"\n2. Delete element from the Queue\n"; cout<<"\n3. Display the content of Queue\n"; cout<<"\n4. Quit\n"; cout<<"\nEnter your choice :"; cin>>choice; switch (choice) { case 1: cout<<"\nEnter the value to be added :\n"; cin>>value; Q.enqueue(value); break; case 2: value=Q.dequeue(); cout <<"\n The deleted value is : "<<value; break; case 3: cout <<"\n\n The queue is \n"; Q.display(); break; } } while (choice!=4);

20Computer Science Archana Jain

Class XII

Practical Programs

} void queue::enqueue(int val) { if (rear==MAX-1) { cout<<"\n Queue is full " ; return; } if (front== -1) front =0; rear++; queue[rear] = val; } int queue::dequeue ( ) { int value; if ( front!=rear) { value= queue[front++]; cout<<value <<" is deleted from the queue"; return value; } else { cout<< " Queue is empty"; return 0; } } void queue::display() { if (front<rear) { cout<<"\n"; for (int i=front ;i<=rear; i++) cout<<queue[i]<<" "; } else cout<<"\nQueue is empty "; cout <<"\n\nPress any key to continue"; getch(); fflush(stdin);

21Computer Science Archana Jain

Class XII

Practical Programs

} 13. To implement operation of circular queue using a class Cqueue #include<iostream.h> #include<conio.h> #define MAX 5 class cqueue { int a[MAX],front,rear; public : cqueue() { front=rear=-1; } void enqueue_CQ(int ); int dequeue_CQ(); void display(); }; void cqueue :: enqueue_CQ(int val) { if((front==0 && rear==MAX-1) || (rear+1==front)) cout<<" Circular Queue is Full"; else { rear=(rear+1) % MAX; a[rear]=val; } if(front==-1) front=0; } int cqueue :: dequeue_CQ() { int k; if(front==-1) cout<<"Circular Queue is Empty"; else { k=a[front]; if(front==rear)

22Computer Science Archana Jain

Class XII

Practical Programs

front=rear=-1; else front = (front +1) %MAX; } return k; } void cqueue :: display() { int i; if(front==-1) cout<<"Circular Queue is Empty"; else { if(rear < front) { for(i=front;i<=MAX-1;i++) cout<<a[i]<<" "; for(i=0;i<=rear;i++) cout<<a[i]<<" "; } else { for(i=front;i<=rear;i++) cout<<a[i]<<" "; cout<<endl; } } } void main() { cqueue c1; int ch,val; char op; do { clrscr(); cout<<"-----------Menu-------------"; cout<<"\n1.Insertion \n"; cout<<"\n2.Deletion \n";

23Computer Science Archana Jain

Class XII

Practical Programs

cout<<"\n3.Display \n"; cout<<"\n4.Exit\n"; cout<<"Enter Your Choice <1..4> ?"; cin>>ch; switch(ch) { case 1 : cout<<"Enter Element to Insert ?"; cin>>val; c1.enqueue_CQ(val); break; case 2 : val=c1.dequeue_CQ(); cout<<"Deleted Element :"<<val<<endl; break; case 3 : c1.display(); break; } cout<<"Do you want to continue<Y/N> ?"; cin>>op; } while(op=='Y' || op=='y'); getch(); } 14. Write a program to search an element in the given array using Binary search algorithm. Ans #include<iostream.h> #define size 10 void main() { int i,pos,t,first,last,mid,list[size]; cout<<"Enter the Array elementsin sorted order:"; for (i=0; i<size ; i++) cin>>list[i]; cout<<"\n Enter the value to be searched : "; cin>>t; first=0; last=size-1; pos=-1; while (first<=last) {

24Computer Science Archana Jain

Class XII

Practical Programs

mid = (first+last)/2; if (list[mid]==t) { pos = mid; break; } else if (list[mid]<t) first=mid + 1; else last=mid - 1; } if (pos>-1) cout<<"\n\nElement found at position: "<<pos+1; else cout<< "\nElement not found"; } 15. Write a program to sort the given array using bubble sort algorithm. Ans //Example : Program for bubble sort // a is the array of n elements #include <iostream.h> #include <conio.h> #include <stdio.h> #define size 20 int main(void) { int i,n,temp, flag=0,list[size]; clrscr(); cout<< "Enter the number of elements in the array : "; cin>>n; cout<<"\n\nEnter the elements of the array :\n"; for (i=0; i<n ; i++) cin>>list[i]; cout << "Unsorted elements of the array are \n\n"; for (i = 0;i<n;i++) cout <<list[i] <<" "; for (i = 0;((i<n-1)&&!flag); i++) {

25Computer Science Archana Jain

Class XII

Practical Programs

flag=1; for (int j = 0; j<n-i-1; j++) if (list[j]>list[j+1]) { temp = list[j]; list[j] = list[j+1]; list[j+1] = temp; flag=0; } } cout << "\n\n\nSorted elements...\n\n"; for (i = 0; i < n; i++) cout <<list[i]<<" "; return 0; } 16. Write a program to sort the given array using insertion sort Hint #include<iostream.h> #include<conio.h> void main() { clrscr(); int list[30]; int i,j,k,n,temp; cout<<"\nEnter the size of list \n"; cin>>n; cout<<"\nEnter the elements of the array:\n"; for (i=0 ; i<n;i++) cin>>list[i]; for (i=1;i<n;i++) { j=i-1; temp=list[i]; while ((temp>=list[j])&&(j>=0)) // scan for the proper place { list[j+1]=list[j]; j=j-1; } list [j+1] =temp; // copying at the proper position of the array

26Computer Science Archana Jain

Class XII

Practical Programs

} cout<<"The sorted list in descending order is \n\n"; for (i=0; i<n ; i++) cout<<list[i]<<"\n"; } 17. Write a C++ program to create a class called LIST (linked list) with member functions to insert an element at the front as well as to delete an element from the front of the list. Demonstrate all the functions after creating a list object. Hint #include <iostream.h> #include <process.h> #include <string.h> #include <conio.h> class NODE { public: int info; NODE *next; }; class LIST { NODE *start; public: LIST() { start = NULL; } void InsertF(); void DeleteF(); void Display_List(); }; void LIST::InsertF() { NODE *temp; int item; cout << "Enter the data: "; cin >> item; cout << endl; temp = new NODE; temp->info = item; temp->next = NULL; if(start == NULL)

27Computer Science Archana Jain

Class XII

Practical Programs

start = temp; else temp->next = start; start = temp; } void LIST::DeleteF() { NODE *temp; if(start == NULL) cout << "No data is present" << endl; else {temp =start; start = start->next; cout << "The deleted data is: " << temp->info << endl; delete temp; }} void LIST::Display_List() {LIST *temp; if(start == NULL) cout << "No data is present" << endl; else for(temp = start; temp != NULL; temp =temp->next) cout << temp->info << "->" << endl; } void main() { LIST s1; clrscr(); int ch=1; while(ch) {cout << " 1 Insert_front 2 Delete_front 3 Display List 0 Exit\n"; cin >> ch; switch(ch) {case 1 : s1.InsertF(); break; case 2 : s1.DeleteF(); break; case 3 : s1.Display_List(); break;

28Computer Science Archana Jain

Class XII

Practical Programs

default: cout << "Wrong choice!" << endl; cout << "Enter the choice again, with 0 to quit" << endl; cin >> ch; }//end switch } //end while(ch) }//end main() 18. Write a Program to create a file Video.dat containing the information about a CD. Hint #include <fstream.h> #include <conio.h> #include <stdio.h> struct CD { int code; char title[20]; char category[15]; float price; }; CD cd ; // Global declaration of structure void main() { fstream cdfile; cdfile.open("VIDEO.dat",ios::app); char ans; ans = 'y'; clrscr(); do { clrscr(); gotoxy(25, 2); cout << "Enter the CD Details"; gotoxy(20, 4); cout << "CD code = > "; cin >> cd.code; gotoxy(20, 6); cout << "CD Title => "; gets(cd.title); gotoxy(20, 8);

29Computer Science Archana Jain

Class XII

Practical Programs

cout << "CD Category => "; gets(cd.category); gotoxy(20,10); cout << "CD Price => "; cin >> cd.price; cdfile.write((char *) &cd, sizeof(cd)); gotoxy(20,12); cout << "Do you want to continue... "; cin >> ans; } while(ans == 'y'); cdfile.close(); 19. Hint } Write a program to search a CD record in the file Video.dat. #include <fstream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> // Declaring structure at here struct CD { int code; char title[20]; char category[15]; float price; }; CD cd; // Global declaration of structure void main() { clrscr(); int tempcode; // Temporary declaration for CD code int flag = 0; // Check a flag for delete sure; fstream cdfile,tempfile; // cdfile is opened for data reading cdfile.open("video.dat", ios::binary||ios::nocreate); // temp file is opened for coping all the records including modified record tempfile.open("Temp.dat", ios::out); clrscr(); cout << "\nEnter CD code :"; cin >> tempcode;

30Computer Science Archana Jain

Class XII

Practical Programs

while (cdfile) { cdfile.read((char *) &cd, sizeof(cd)); if (!cdfile) exit(0); cout <<"\nDetails of the CD are :"; cout<<"\n CD code CD title CD category CD price"; if (cd.code == tempcode) { cout<<"\n"<<" "<<cd.code<<" "<<cd.title<<" "<<cd.category<<" "<<cd.price; flag = 1; } } cdfile.close(); if (flag!= 1) cout << "Record not found !!!"; getch(); } 20. Program to modify the cd record of the video .dat file. #include <fstream.h> #include <conio.h> #include <stdio.h> #include <stdlib.h> #include <iomanip.h> struct CD { int code; char title[20]; char category[15]; float price; }; CD cd; // Global declaration of structure int recordno(int temp_code); // Function to find total records in EMP.dat void main() { fstream cdfile; int location = 0; int temp_no ;

31Computer Science Archana Jain

Class XII

Practical Programs

int totrec = 0; cdfile.open("VIDEO.dat",ios::binary||ios::nocreate); char ans; clrscr(); cout << "Enter CD code to modify : "; cin >> temp_no; totrec = recordno(temp_no); cout << totrec; location = (totrec-1) * sizeof(CD); // Search the position of file cdfile.seekp(location); // Read the contents from VIDEO.dat data file cdfile.read((char *)&cd, sizeof(CD)); cout << "Old CD Master Record\n"; cout << "CD code : " << cd.code << endl; cout << "CD name : " << cd.title << endl; cout << "CD designation : " << cd.category << endl; cout << "CD salary : " << cd.price << endl; cdfile.close(); // Entering new records cout << "\nEnter new data for CD : " << endl; cout << "CD Title : " ; gets(cd.title); cout << "CD Category : " ; gets(cd.category); cout << "CD Price : "; cin >> cd.price ; cout << "\nAre you sure to modify the above record : "; cin >> ans; if (ans == 'y' || ans == 'Y') { cdfile.open("VIDEO.dat", ios::out|ios::ate); // Search the position of file cdfile.seekp(location); // Write the modified contents into EMP.dat data file cdfile.write((char *)&cd, sizeof(CD)); cdfile.close(); cout << "Record modified "; getch(); }

32Computer Science Archana Jain

Class XII

Practical Programs

} int recordno(int temp_no) { fstream file; file.open("VIDEO.dat", ios::in); file.seekg(0, ios::beg); int count = 0; // Counts the total records in VIDEO.dat data file while (file.read((char *)&cd, sizeof(CD))) { count++; if (temp_no == cd.code) break; } file.close(); return count; } 21. file handling : Write a program to create a file containing student marks details . Create another file result.dat which contains the result of the student : percentage and grade as per the given criteria. : if(percentage >=75) then grade='A'; if((percentage >=60)&&(percentage<75)) then grade='B'; if((percentage>=50)&&(percentage<60)) then grade='C'; if((percentage>=40)&&(percentage<50)) then grade='D'; if(percentage<40) then grade='F'; Ans #include<iostream.h> #include<conio.h> #include<stdio.h> #include<fstream.h> struct stud { public: int rno; char name[50]; int cls; float marks[5];

33Computer Science Archana Jain

Class XII

Practical Programs

}; struct result { int rnos; float total; char grade; float perc; }; void main() { clrscr(); float tm; int i,ma; stud st; result rt; ofstream file1("stud.dat",ios::binary|ios::trunc); ofstream file2("result.dat",ios::binary|ios::trunc); if(!file1||!file2) cout<<"File Cannot Be Created"<<endl; int n; if(file1&&file2) { cout<<"Enter total number of students : "<<endl; cin>>n; for(i=0;i<n;i++) { cout<<"Enter Student Roll No"<<endl; cin>>st.rno; cout<<"Enter Student Name"<<endl; gets(st.name); cout<<"Enter Student Class"<<endl; cin>>st.cls; for(ma=0;ma<5;ma++) { cout<<"Enter Marks in subject "<<ma+1<<endl; cin>>st.marks[ma];

34Computer Science Archana Jain

Class XII

Practical Programs

} file1.write((char*)&st,sizeof(st)); tm=st.marks[0]+st.marks[2]+st.marks[1]+st.marks[3]+st.marks[4]; rt.perc=tm/5; if(rt.perc>=75) rt.grade='A'; if((rt.perc>=60)&&(rt.perc<75)) rt.grade='B'; if((rt.perc>=50)&&(rt.perc<60)) rt.grade='C'; if((rt.perc>=40)&&(rt.perc<50)) rt.grade='D'; if(rt.perc<40) rt.grade='F'; rt.rnos=st.rno; file2.write((char*)&rt,sizeof(rt)); }} file1.close(); file2.close(); int r,k=0; ifstream fin("result.dat",ios::binary); if(!fin) { cout<<"File opening Error"<<endl; } else { fin.seekg(0); cout<<"\nDisplaying Progress Report \n"; cout<<"\n___________________________\n\n"; cout<<endl<<"ENTER YOUR ROLL NO"<<endl; cin>>r;

35Computer Science Archana Jain

Class XII

Practical Programs

while(fin.read((char*)&rt,sizeof(rt))) { if(r==rt.rnos) { k++; cout<<" Roll No :"<<rt.rnos<<endl<<" Percentage :"<<rt.perc<<endl<<" Grade :"<<rt.grade; } } if(k==0) cout<<"Roll no not present in the file"<<endl; fin.close(); } getch(); } 22. Write a program to create & display the list of n number of nodes containing the salary & name of n number of employees. Now display the name of the employee whose salary is equal to the given target salary. Hint #include <iostream.h> #include<conio.h> #include<stdio.h> #include<iomanip.h> struct node { char name[20]; float salary; node *next; }; node *START, *LAST, *ptr, *t; void main() { clrscr(); int n; START = LAST = NULL; cout<<"Enter the number of nodes in the list :"; cin>>n; for (int i=1; i<=n ; i++)

36Computer Science Archana Jain

Class XII

Practical Programs

{ ptr=new node; cout<<"\nEnter the name of the employee : "; gets(ptr->name); cout<<"Enter the salary of the employee :"; cin>>ptr->salary; ptr->next= NULL; if (START==NULL) START=LAST=ptr; else { LAST->next= ptr; LAST=ptr; } } t=START; cout<< "\nThe linked list contains the following nodes : \n\n"; while (t!=NULL) { cout<<t->name<<" " ; cout<< t->salary <<" -> "; t=t->next; } //Searching of the node float sal; cout<<"\n\nEnter the salary of the employee that you are looking for :"; cin>>sal; t=START; int found=0; while ((t!=NULL)&&(!found)) { if (t->salary==sal) found=1; else t = t->next; } if (found==1) cout<<"\nThe name of the employee whose salary is"<<sal << ": "<<t->name; else

37Computer Science Archana Jain

Class XII

Practical Programs

cout<<"\nUnsuccessful search !!!" ; getch(); }


23. A bank maintains a list of client name and his balance. Create a linked list with the above information count and display the number of clients who worth more than Rs. 1,00,000.

Hint #include <iostream.h> #include<conio.h> #include<stdio.h> #include<iomanip.h> struct node { char name[20]; float balance; node *next; }; node *START, *LAST, *ptr, *t; void main() { clrscr(); int n; START = LAST = NULL; cout<<"Enter the number of nodes in the list :"; cin>>n; for (int i=1; i<=n ; i++) { ptr=new node; cout<<"\nEnter the name of the client : "; gets(ptr->name); cout<<"Enter the balance of the client :"; cin>>ptr->balance; ptr->next= NULL; if (START==NULL) START=LAST=ptr; else { LAST->next= ptr; LAST=ptr;

38Computer Science Archana Jain

Class XII

Practical Programs

} } t=START; cout<< "\nThe linked list contains the following nodes : \n\n"; while (t!=NULL) { cout<<t->name<<" " ; cout<< t->balance <<" -> "; t=t->next; } //Searching of the node cout<<"\n\nThe list of employees who has balance more than 100000 are : \n"; t=START; int found=0; while (t!=NULL) { if (t->balance>=100000) { found=1; cout<<"\n"<<t->name<<" : " <<t->balance; } t = t->next; } if (found==0) cout<<"\nUnsuccessful search !!!" ; getch(); } 24. Write a Program to merge two arrays A and B which are sorted in ascending and descending order respectively. Hint // A is the array of m elements sorted in ascending order //B is the array of n elements sorted in descending order #include <iostream.h> #include <conio.h> #include <stdio.h> #define size 20 int main(void) {

39Computer Science Archana Jain

Class XII

Practical Programs

int i,n,m, temp, flag=0,A[size],B[size],C[size+size]; clrscr(); cout<< "Enter the number of elements in the array A: "; cin>>m; cout<<"\n\nEnter the elements of the array A in sorted order :\n"; for (i=0; i<m ; i++) cin>>A[i]; cout<< "Enter the number of elements in the array B in sorted order: "; cin>>n; cout<<"\n\nEnter the elements of the array B:\n"; for (i=0; i<n ; i++) cin>>B[i]; cout << " Sorted elements of Both the arrays are \n\n"; cout<< "\nArray A \n"; for (i = 0;i<m;i++) cout <<A[i] <<" "; cout<< "\nArray B \n"; for (i = 0;i<n;i++) cout <<B[i] <<" "; int ctr1, ctr2, ctr3; ctr1 = 0; ctr2 = 0; ctr3 = 0; while (ctr1 < m && ctr2 < n) { if (A[ctr1] <= B[ctr2]) C[ctr3++] = A[ctr1++]; else C[ctr3++] = B[ctr2++]; } while (ctr1 < m) C[ctr3++] = A[ctr1++]; while (ctr2<n) C[ctr3++] = B[ctr2++]; cout << "\n\n\nSorted elements...\n\n";

40Computer Science Archana Jain

Class XII

Practical Programs

for (i = 0; i < m+n; i++) cout <<C[i]<<" "; return 0; } 25. Write a C++ program to create a class called DLIST (doubly Linked List) with member functions to insert a node at a specified position and delete a node from a specified position of the list. Demonstrate the operations by displaying the content of the list after every operation. Ans #include <iostream.h> #include <iomanip.h> #include <conio.h> #include <stdlib.h> struct node {int data; node *left,*right; }; class dlist { private: struct node *start; public: dlist(); ~dlist(); void create(); void addatpos(); void delatpos(); void dis(); }; dlist::dlist() { start=NULL; } dlist::~dlist() { delete start; } void dlist::create() {int no,i; struct node *n; cout<<endl<<"Enter number of elements:"; cin>>no; for(i=1;i<=no;i++)

41Computer Science Archana Jain

Class XII

Practical Programs

{ n=new node; n->right=NULL; n->left=NULL; cout<<endl<<"Enter the data:"; cin>>n->data; if(start==NULL) start=n; else { n->right=start; start->left=n; start=n; }} return; } void dlist::addatpos() {struct node *n; int p; cout<<endl<<"Enter position:"; cin>>p; n=new node; n->left=NULL; n->right=NULL; cout<<endl<<"Enter data:"; cin>>n->data; if(p==1) { n->right=start; start->left=n; start=n; }else {int i=1; struct node *t; t=start; while(i<=p-2) {t=t->right; i++; } n->right=t->right; t->right->left=n; n->left=t; t->right=n; } return; }

42Computer Science Archana Jain

Class XII

Practical Programs

void dlist::delatpos() {struct node *t; int i,p; cout<<endl<<"Enter position:"; cin>>p; if(p==1) { start=start->right; start->left=NULL; return; }t=start; i=1; while(i<p&&t!=NULL) { t=t->right; i++; }if(t->right==NULL) t->left->right=NULL; else {t->left->right=t->right; t->right->left=t->left; } return; } void dlist::dis() {struct node *t; t=start; if(t==NULL) cout<<endl<<"Empty list:"; else {cout<<endl<<"Content of the list"<<endl; while(t!=NULL) {cout<<setw(5)<<t->data; t=t->right; }} return; } main() { dlist d; int ch=1; clrscr(); while(ch) { cout<<endl<<"Press 1 To create double linked list:"; cout<<endl<<"Press 2 To add element at position:";

43Computer Science Archana Jain

Class XII

Practical Programs

cout<<endl<<"Press 3 to delete an element at a position:"; cout<<endl<<"Press 0 to Quit:"; cout<<endl<<"Enter your choice:"; cin>>ch; switch(ch) {case 1: d.create(); d.dis(); break; case 2: d.addatpos(); d.dis(); break; case 3: d.delatpos(); d.dis(); break; }} getch(); return 0; }

Você também pode gostar