Practical File of Computer Science
Practical File of Computer Science
SCIENCE
PRACTICAL FILE
XII
1
Index
S.No
1.
Program
Function overloading
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
Linear search
Binary search
Selection sort
Bubble sort
Insertion sort
Merging of two arrays
Sum of rows of a 2-D array
Sum of columns of a 2-D array
Sum of Diagonal element of a 2-D array
Insertion,Deletionand traversal in array
queue
Insertion,Deletion and traversal in linked
queue
21.
Teachers Sign.
22.
23.
24.
25.
OUTPUT
#include<stdlib.h>
class Student { private:
int rollno;
char name[25] ;
float marks ;
char grade ;
public:
5
void readStudent( )
{
cout << " \n Enter rollno : " ;cin>>rollno;
cout << " \n Enter name : " ;cin>>name;
cout << " \n Enter marks : " ;cin>>marks;
}
void dispStudent( )
// accessor
{ calcGrade( ) ;
//invokefunction to determine grade
cout << " Roll no. : " << rollno << endl ;
cout << " Name : " << name << endl ;
cout <<"Marks : " << marks << endl ;
cout << " Grade : " << grade << endl ;
getch();
}
int getRollno( )
{ return rollno;
}
float getMarks( )
{ return marks ;
}
void calcGrade( )
{ if ( marks >= 75)
grade ='O' ;
else if ( marks >= 60)
grade ='A' ;
else if ( marks >= 50)
grade ='B' ;
else if ( marks >= 40)
grade ='C';
else
grade ='F' ;
}
};
int main( )
{int i;
Student XIIa[10] ;
for( i= 0; i< 5; ++i)
{
cout<< " \nEnter details of Student " << i+1 << " : " ;
XIIa[i].readStudent( ) ;
}
int choice, rno, pos = -1;
float highmarks = 0 ;
do
{
system (" cls ") ;
cout << " \n\n Main Menu\n " ;
6
OUTPUT
void display()
{ cout<<"size is"<<gsize;
cout<<"type is";
puts(gtype);
cout<<"fabric is";
puts(gfabric);
cout<<"code is";
puts(gcode);
cout<<"price is"<<gprice;
}
;
int main()
{ GARMENTS G;
G.input();
G.display();
getch();
}
OUTPUT
10
OUTPUT
12
OUTPUT
14
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 : ";
cin>>empname;
cout << "Enter designation : ";
cin>>empdesig;
cout << "Enter salary : ";
cin >> empsalary;
}
void Customer::custinput_data(void)
{
cout << "\nEnter customer information : \n";
cout << "Enter code : ";
cin >> custcode;
cout << "Enter name : ";
cin>>custname;
cout << "Enter designation : ";
cin>>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;
16
17
}
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 : ";
cin>>empname;
cout << "Enter designation : ";
cin>>empdesig;
cout << "Enter salary : ";
cin >> empsalary;
}
void Customer::custinput_data(void)
{
cout << "\nEnter customer information : \n";
cout << "Enter code : ";
cin >> custcode;
cout << "Enter name : ";
cin>>custname;
cout << "Enter designation : ";
cin>>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;
}
int main()
{
clrscr();
19
OUTPUT
20
Write a program get roll numbers and marks of the students of a class and
store these details into a file called Marks.dat
#include<iostream.h>
#include<fstream.h>
int main()
{ ofstream fileout;
// stream decided and declared-steps 1 & 2
fileout.open("marks.dat",ios::out|ios::app);
// file linked-step 3
char ans ='y';
//process as required-step 4 begins
int rollno; float marks;
while(ans=='y' ans=='y')
{ cout<<"\n Enter Rollno.:";
cin>>rollno;
cout<<"\n Enter Marks:";
cin>>marks;
fileout<<rollno<<'\n'<<marks<<'\n';
cout<<"\n Want to enter more records?(y/n)...";
cin>>ans;
}
fileout.close();
//delink the file-step 5
return 0;
}
OUTPUT
21
cout<<"Enter marks;";
cout<<"\n";
cin>>marks;
}
void student::display(void)
{ cout<<"Name:"<<name<<"\t"
<<"Grade:"<<grade<<"\t"
<<"Marks:"<<marks<<"\t" <<"\n";
}
int main()
{ system("cls");
students arts[3];
//declare array of 3 objects
fstream filin;
//input and output file
filin.open("stu.dat",ios::in ios::out);
if(!filin)
{ cout<<"cannot open file!!\n";
return 1;
}
cout<<"enter details for 3 students \n";
for(inti=0;i<3;i++)
{ arts[i].getdata();
fillin.write((char*)&arts[i],sizeof(arts[i]));
}
fill.seekg(0);
//seekg(0)resets the file to start,so that the file can be
//accessed from the beginning.
cout<<"The contents of stu.dat are shown below.\n";
for(i=0;i<3;i++)
{ filln.read((char*) & arts[i],sizeof(arts[i]));
arts[i].display();
}
fillin.close();
return 0;
}
OUTPUT
23
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{ cout << "Rollno:"; cin >> rollno;
cout << "Name:";
cin >> name;
cout << "Class:";
cin >> class;
cout << "Marks:";
cin >> marks;
if(marks >= 75)
grade = 'A';
else if(marks >= 60) grade = 'B';
else if(marks >= 50) grade = 'C';
else if(marks >= 40) grade = 'D';
else grade='F';
}
void putdata()
{ cout << name << ", rollno" << "has" << marks
<<" % marks and " << grade << "grate." << endl;
}
int getrno() //accessor function
{ return rollno;
}
}s1;
int main()
{
int rn; char found='n';
ifstream fi("stu.dat",ios::in);
//stu.dat must exist on disk
cout << "Enter rollno to be searched for:";
cin >> rn;
while(!fi.eof())
{ fi.read((char*)&s1, sizeof(s1);
if(s1.getrno() == rn)
{ s1.putdata();
found='y';
break;
}
}
if(found == 'n')
cout <<"Rollno not found in file!!"<<endl;
fi.close();
return 0;
}
OUTPUT
25
#include<stdio.h>
// for rename() and remove()
class stu{ int rollno;
char name[25];
char Class[4];
float marks;
char grade;
public:
void getdata()
{ cout << "Rollno:"; cin >> rollno;
cout << "Name:";
cin >> name;
cout << "Class:";
cin >> class;
cout << "Marks:";
cin >> marks;
if(marks >= 75)
grade = 'A';
else if(marks >= 60) grade = 'B';
else if(marks >= 50) grade = 'C';
else if(marks >= 40) grade = 'D';
else grade='F';
}
void putdata()
{ cout << "Rollno" << rollno << "\tName:" << name
<<" \n Marks:" << marks << "\tGrade:" << grate << endl;
}
int getrno() //accessor function
{ return rollno; }
}s1, stud;
int main()
{ ifstream fi("stu.dat",ios::in);
//stu.dat must exist on disk
ofstream fo("temp.dat",ios::out);
char last="y";
cout << "Enter details of student whose record is to be inserted\n";
s1.getdata();
while(!fi.eof())
{ fi.read((char*)&stud,sizeof(stud)); // s1 is new record, stud is record from file
if (s1.getrno() <=stud.getrno())
{ fo.write((char*) &s1, sizeof(s1));
last ='n';
break;
}
else
fo.write((char*)&stud, sizeof(stud));
}
if(last == 'y'
fo.write(char*)&s1, sizeof(s1));
else if(!fi.eof())
{ while(!fi.eof())
27
fi.read((char*)&stud,sizeof(stud));
fo.write((char*)&stud,sizeof(stud));
}
}
fi.close();
fo.close();
remove("stu.dat");
rename("temp.dat"),"stu.dat");
fi.open("stu.dat",ios::in);
cout<<"File now contains\n";
while(!fi.eof())
{ fi.read((char*)&stud,sizeof(stud));
if(fi.eof()) break;
stud.putdata();
}
fi.close();
return 0;
}
28
else
file.write((char*)&s1, sizeof(stud));
}
if(found == 'f')
cout<<"Record not found!!\n";
fio.close();
file.close();
remove("stu.dat");
rename("temp.dat"),"stu.dat");
fio.open("stu.dat",ios::in);
cout<<"Now the file contains\n";
while(!fio.eof())
{
fio.read((char*)&stud,sizeof(stud));
if(fio.eof())break;
stud.putdata();
}
fio.close();
return 0;
}
OUTPUT
30
OUTPUT
enter the desired array size(max 50)...10
enter the array elements(in asc order)
23
45
56
57
60
65
67
78
89
93
enter element to be searched for ...65
31
OUTPUT
Enter the total numbers in array : 5
Value 1 . 445
Value 2 . 67
Value 3 . 76
Value 4 . 89
Value 5 . 45
Sorted elements are :
Value 1 . 45
Value 2 . 67
Value 3 . 76
Value 4 . 89
Value 5 . 445
Enter the element to search in the above list 76
33
OUTPUT
Enter the length of your array : 6
Enter the array values
Value 1. 23
Value 2. 12
Value 3. 32
Value 4. 43
Value 5. 23
Value 6. 45
Enter the inserted value : 65
Enter the location to insert in array : 3
The resulted output is :
23
12
65
32
43
23
45
35
else
cout << "Your location is not in the array range to delete";
}
OUTPUT
Enter the length of your array : 5
Enter the array values
Value 1. 76
Value 2. 56
Value 3. 23
Value 4. 32
Value 5. 43
Enter the location to delete from the array : 2
The resulted output is :
76
23
32
43
The deleted value is : 56
37
range[loc] = T;
}
cout << "\nThe sorted list is ... \n";
for (i = 0; i < N; i++)
cout << range[i] << endl;
return 0;
}
OUTPUT
Enter the number of elements in your array : 6
Enter the array elements :
Location 1. Value : 19
Location 2. Value : 21
Location 3. Value : 31
Location 4. Value : 23
Location 5. Value : 45
Location 6. Value : 43
The sorted list is ...
19
21
23
31
43
45
39
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
OUTPUT
Enter the array elements : 25
57
48
37
12
92
86
33
Unsorted elements...
25 57 48 37 12 92 86 33
Sorted elements...
12 25 33 37 48
57
86
92
41
range[j + 1] = T;
for (x = 0; x < N; x++) // Displays the output after every insertion
cout << range[x] << " ";
cout << "\n";
}
cout << "\nThe sorted list is ... \n";
for (i = 0; i < N; i++)
cout << range[i] << endl;
}
OUTPUT
Enter the array elements : 6
4
7
3
7
9
4
7
2
5
4 6 7 3 7 9 4 7 2 5
4 6 7 3 7 9 4 7 2 5
3 4 6 7 7 9 4 7 2 5
3 4 6 7 7 9 4 7 2 5
3 4 6 7 7 9 4 7 2 5
3 4 4 6 7 7 9 7 2 5
3 4 4 6 7 7 7 9 2 5
2 3 4 4 6 7 7 7 9 5
2 3 4 4 5 6 7 7 7 9
The sorted list is ...
2
3
43
4
4
5
6
7
7
7
9
OUTPUT
enter elements of array1
2
3
4
5
6
array is
1
2
3
4
5
6
Sum of Rows : 1 is : 3
Sum of Rows : 2 is : 7
Sum of Rows : 3 is : 11
OUTPUT
first array is 1
Second array is 2
7
4
4
4
6
7
8
Write a program to display the sum of both the diagonal elements of a two
dimensional array
#include <iostream.h>
#include <conio.h>
const M = 6;
const N = 6;
void sum()
{
clrscr();
int MATRIX[M][N], i, j, s1 = 0, s2 = 0;
cout << "Input steps";
cout << "\nEnter the element in the array\n";
for(i=0; i<M; i++)
for(j=0; j<N; j++)
{
cin >> MATRIX[i][j];
}
// Display the array elements
for(i=0; i<M; i++)
{
47
OUTPUT
Input steps
Enter the element in the array
1
2
3
4
5
6
7
8
9
1
2
3
4
5
6
48
7
8
9
sum of main diagonal is 15
sum of other diagonal is
15
Write a program illustrates the basic operations add, delete and display in array
queue .
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
#define MAX 100 // Shows maximum array length
int queue[MAX]; // Declares array global variable
int front, rear;
// Declares integer front and read
// Function prototypes of add queue, delete queue, and show queue in array implementation
void add_Q(int queue[], int val, int &rear); // Add queue
int del_Q(int queue[], int &front, int rear); // Delete queue
void show_Q(int queue[], int front, int rear); // Show queue
void main()
{
int choice, val;
char opt = 'Y';
// To continue the do loop in case
rear = -1;
// Initialization of Queue
front = -1;
clrscr();
49
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;
add_Q(queue, val, rear);
cout << "Do you want to add more elements <Y/N> ? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
val = del_Q(queue, front, rear);
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(queue, front, rear);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
// Function body for add queue with array
void add_Q(int queue[], int val, int &rear)
{
if (rear == MAX)
{
50
OUTPUT
Main Menu
1. Addition of Queue
51
52
Write a program to illustrates the basic operation add , delete and display in
linked queue .
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
// Declares a queue structure
struct node
{
int data;
node *link;
53
};
// Functions prototype for add queue, delete queue, and show queue
node *add_Q(node *rear, int val); // Add queue
node *del_Q(node *front, int &val); // Delete queue
void show_Q(node *front); // Show queue
// Main programming logic
void main()
{
node *front, *rear;
int val, choice;
char opt = 'Y';
// To continue the do loop in case
front = rear = NULL; // Initialization of Queue
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'; // Initialize for the second loop
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;
54
clrscr();
cout << "The Queue values are";
while (temp != NULL)
{
cout <<"\n"<< temp->data;
temp = temp->link;
}
}
OUTPUT
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Enter your choice from above 1
Enter the value to be added in the queue 56
Do you want to add more elements <Y/N> ? y
Enter the value to be added in the queue 78
Do you want to add more elements <Y/N> ? y90
Enter the value to be added in the queue
Do you want to add more elements <Y/N> ? n
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Enter your choice from above 1
Enter the value to be added in the queue 56
Do you want to add more elements <Y/N> ? y
Enter the value to be added in the queue 78
Do you want to add more elements <Y/N> ? y
Enter the value to be added in the queue 90
Do you want to add more elements <Y/N> ? n
56
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Enter your choice from above
Value deleted from Queue is 56
Do you want to delete more elements <Y/N> ? n
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Enter your choice from above 3
The Queue values are
78
90
Main Menu
1. Addition of Queue
2. Deletion from Queue
3. Traverse of Queue
4. Exit from Menu
Enter your choice from above 4
char ch='y';
while(ch=='y'||ch=='Y')
{cout<<"enter the item for insertion";
cin>>item;
res=push(stack,top,item);
if(res==-1)
{cout<<"overflow;
exit(1);
}
cout<<"the stack now is";
display(stack,top);
cout<<"want to insert more elements?(y/n)";
cin>>ch;
}
cout<<"now deletion of elements begins...\n";
ch='y';
while(ch=='y'||ch=='Y')
{res=pop(stack,top);
if(res==-1)
{cout<<"underflow";
exit(1);
}
else{cout<<"elements deleted is"<<res<<endl;
cout<<"the stack now is";
display(stack,top);
}
cout<<"want to delete more elements ?(y/n)";
cin>>ch;
}
return 0;
}
int push(int stack[],int &top,int ele)
{if(top==size-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];
58
top--;
}
return ret;}
void display(int stack[],int top)
{ if(top==-1)
return;
cout<<stack[top]<<"<--"<<endl;
for(int i=top-1;i>=0;i--)
cout<<stack[i]<<endl;
}
OUTPUT
59
np = np -> next;
}
cout << !!!\n;
}
OUTPUT
Enter INFOrmation for the new node5
Now the linked-stack is :
5 -> !!!
Press Y to enter more nodes, N to exity
Enter INFOrmation for the new node8
Now the linked-stack is :
8 -> 5 -> !!!
Press Y to enter more nodes, N to exity
Enter INFOrmation for the new node15
Now the linked-stack is :
15 -> 8 -> 5 -> !!!
Press Y to enter more nodes, N to exit.
61