Professional Documents
Culture Documents
Cs
Cs
Class XII
Session 2019-20
Computer Science with C++
(Code – 283)
Index
S. No. Practical Page Sign. of Teacher
No.
1. Write a complete program in C++ to the
passing of arguments to parameterized
constructor
2. Write a complete program in C++ to illustrate
copy constructor and constructor overloading
3. Write a complete program in C++ to the
concept of base and derived class
4. Write a complete program in C++ to illustrate
the concept of container-ship
5. Write a complete program in C++ to illustrate
use of open() member function to open a text
file for writing purpose and insert text in the
file.
6. Write a complete program in C++ to open a
text file for writing purpose using constructor
and insert text in the file.
7. Write a complete program in C++ to illustrate
the use of getline() to read a string from a text
file.
Programming Problem:
Write a complete program in C++ to the passing of
arguments to parameterized constructor
// C++ Program to illustrate the passing of
// arguments to parameterized constructor
#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
class person
{
private:
long UID; // Aadhar ID
char name[20];
char gender;
float annual_income;
public:
person(); // default constructor
person(long uid, char n[], char gen, float a_i); // parameterized constructor
void get_data();
void display();
};
}
// Definition of display function, defined outside the class
void person::display()
{
cout << "Person's details:" << endl;
cout << "Aadhar Number (UID) : " << UID << endl;
cout << "Name : " << name << endl;
cout << "Gender : " << gender << endl;
cout << "Annual income(in Rs.) : " << annual_income << endl;
}
// main function
int main()
{
person P1; // implicit call to default constructor
P1.display();
return 0;
Output:
Person's details:
Aadhar Number (UID) : 0
Name : Mr. X
Gender :M
Annual income(in Rs.) : 0
Person's details:
Aadhar Number (UID) : 12121212
Name : Chaman Lal
Gender :M
Annual income(in Rs.) : 60000
Person's details:
Aadhar Number (UID) : 13131313
Name : Madan Lal
Gender :M
Annual income(in Rs.) : 120000
Programming Problem:
Write a complete program in C++ to illustrate copy
constructor and constructor overloading
// C++ program to illustrate a copy constructor
// and constructor overloading
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class student
{
private:
int roll_no;
char name[20];
float perc_marks;
public:
student(); // default constructor
student(int rno, char n[], float p); //parameterized constructor
student(student &s); // copy constructor
student get_data(); // member function to get data
void display();
};
student::student()
{
roll_no = 0;
strcpy(name, "Unknown");
perc_marks = 0.0;
}
student::student(int rno, char n[], float p)
{
roll_no = rno;
strcpy(name, n);
perc_marks = p;
}
student::student(student &s)
{
roll_no = s.roll_no;
strcpy(name, s.name);
perc_marks = s.perc_marks;
}
// member function returns an object
student student::get_data()
{
cout << "Enter the details of the student:" << endl;
student s;
cout << "Roll No. : "; cin >> s.roll_no;
cout << "Name : "; cin >> s.name;
fflush(stdin);
cout << "Percentage marks: "; cin >> s.perc_marks;
return s;
}
void student::display()
{
// main function
int main()
{
student s1; // default constructor is called implicitly
s1.display();
student s5;
s5 = s1.get_data(); //copy constructor is called while get_data()returns an
// object
s5.display();
return 0;
}
Output:
The details of the student:
Roll No. :0
Name : Unknown
Percentage marks: 0
The details of the student:
Roll No. :2
Name : Rishabh Kumar
Percentage marks: 89
The details of the student:
Roll No. :2
Name : Rishabh Kumar
Percentage marks: 89
The details of the student:
Roll No. :2
Name : Rishabh Kumar
Percentage marks: 89
Enter the details of the student:
Roll No. :3
Name : Akash
Percentage marks: 95
The details of the student:
Roll No. :3
Name : Akash
Percentage marks: 95
Programming Problem:
Write a complete program in C++ to the concept of
base and derived class
// C++ program to illustrate the concept of base and derived class
#include <iostream>
#include <stdio.h>
using namespace std;
class person
{
protected:
char name[30];
char address[50];
char phone_no[11];
public:
void get_data()
{
cout << "Enter the details of the person:" << endl;
cout << "Name : "; cin.getline(name, 30);
fflush(stdin);
cout << "Address : "; cin.getline(address, 50);
fflush(stdin);
cout << "Phone No. : "; cin >> phone_no;
}
void show_data()
{
cout << "The details of the person:" << endl;
cout << "Name : " << name << endl;
cout << "Address : " << address << endl;
cout << "Phone No. : " << phone_no << endl;
}
};
// main function
int main()
{
cout << "Base class:" << endl;
person P;
P.get_data();
P.show_data();
return 0;
}
Base class:
Enter the details of the person:
Name : Ramesh Kumar
Address : 12 F, DDA Colony, Model Town, Delhi
Phone No. : 01123967788
The details of the person:
Name : Ramesh Kumar
Address : 12 F, DDA Colony, Model Town, Delhi
Phone No. : 01123967788
Derived class:
Enter the details of the person:
Name : Rahul Kumar
Address : 34 D, DA Colony Timarpur, Delhi-110054
Phone No. : 01123968899
Admission No. : 1002
Class(1-12) : 12
Section(A/B/C/D) : D
The details of the person:
Name : Rahul Kumar
Address : 34 D, DA Colony Timarpur, Delhi-110054
Phone No. : 01123968899
Admission No. : 1002
Class(1-12) : 12
Section(A/B/C/D) : D
Programming Problem:
Write a complete program in C++ to illustrate the
concept of container-ship
// C++ program to illustrate the concept of container-ship
#include <iostream>
#include <stdio.h>
using namespace std;
class date
{
private:
int dd;
int mm;
int yyyy;
public:
void get_date()
{
cin >> dd >> mm >> yyyy;
}
void show_date()
{
cout << dd << "/" << mm << "/" << yyyy << endl;
}
};
class student
{
private:
int roll_no;
char name[20];
date dob;
public:
date doj;
void get_data()
{
cout << "Enter details:\n";
cout << "Roll no.: ";
cin >> roll_no;
fflush(stdin);
cout <<"Name: ";
cin.getline(name, 20);
cout <<"Date of birth (dd mm yyyy): ";
dob.get_date();
}
void display()
{
cout << "Roll No. : " << roll_no << endl;
cout << "Name : " << name << endl;
cout << "Date of birth : ";
dob.show_date();
}
};
int main()
{
student s;
s.get_data();
s.display();
return 0;
}
Enter details:
Roll no.: 1
Name: Ramesh Gupta
Date of birth (dd mm yyyy): 23 12 2002
Roll No. :1
Name : Ramesh Gupta
Date of birth : 23/12/2002
Date of joining(dd mm yyyy): 01 04 2013
1/4/2013
Programming Problem:
Write a complete program in C++ to illustrate use of
open() member function to open a text file for writing
purpose and insert text in the file.
int main()
{
char fileName[80];
ofstream fout;
return 0;
}
Output:
Programming Problem:
Write a complete program in C++ to open a text file
for writing purpose using constructor and insert text
in the file.
int main()
{
ofstream myFile("poem.txt");
myFile.close();
return 0;
Programming Problem:
Write a complete program in C++ to illustrate the use
of getline() to read a string from a text file.
//Program to illustrate the use of getline() to read from a string from a text file
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
const int size = 100;
char arr[size];
myFile.close();
return 0;
}
Output:
Programming Problem:
Write a complete program in C++ to illustrate the use
of write() to write a string to a text file.
int main()
{
char buffer[100] ="Where there is a will, there is a way";
aFile.close();
return 0;
}
Programming Problem:
Write a complete program in C++ to open a file in
binary mode and use read() function to read its
content.
//Program to illustrate the use of read() function
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
char buffer[100];
myFile.read(buffer, 100);
myFile.close();
return 0;
}
Output:
Programming Problem:
Write a complete program in C++ to illustrate the use
of binary file, creating a binary file using a structure
student.
//Program to illustrate the use of binary file, creating a binary file "stu.dat"
using structure
#include<iostream>
#include<fstream>
using namespace std;
struct student
{
char name[20];
float percent;
};
int main()
{
ofstream fout;
fout.open("stu.dat", ios::out | ios::binary);
if(!fout)
{
cout<<"\nFile cannot be opened!";
return -1;
}
char ch;
student s;
do
{
fflush(stdin);
cout<<"Enter the name of the student : ";
cin.getline(s.name, 20);
cout<<"\nEnter percentage : ";
cin>>s.percent;
fout.write((char *) &s, sizeof(s));
return 0;
}
Output:
Enter percentage : 99
Enter percentage : 88
Enter percentage : 77
More records (y/n) : n
Programming Problem:
Write a complete program in C++ read the details (roll
no., name, marks in 5 subjects) of students from
console and write them to a binary file and create
another binary file to contain the percentage and
grade of the students along with their roll numbers.
//student record
struct stud
{
int rno;
char name[50];
int cls;
float marks[5];
};
//result record
struct res
{
int rnos;
float total;
char grade;
float perc;
};
int main()
{
float tm;
int i,ma;
stud st;
res rt;
ofstream sfile("stud.dat",ios::binary|ios::trunc);
ofstream rfile("result.dat",ios::binary|ios::trunc);
if(!sfile||!rfile)
cout<<"File Cannot Be Created"<<endl;
int n;
if(sfile&& rfile)
{
cout<<"How many Students You Want to Enter ? : ";
cin>>n;
for(i=0;i<n;i++)
{
fflush(stdin);
cout<<"Enter Name : ";
cin.getline(st.name, 50);
cout<<"Enter Roll No : ";
cin>>st.rno;
cout<<"Enter Class : ";
cin>>st.cls;
for(ma=0;ma<5;ma++)
{
cout<<"Enter Marks in subj"<<ma+1<< " : ";
cin>>st.marks[ma];
}
sfile.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;
rfile.write((char*)&rt,sizeof(rt));
}
}
sfile.close();
rfile.close();
int r,k=0;
ifstream ifile("result.dat",ios::binary);
if(!ifile)
{
cout<<"File Error"<<endl;
}
else
{
ifile.seekg(0);
cout<<endl<<"ENTER ROLL NO : ";
cin>>r;
while(ifile.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"<<endl;
rfile.close();
}
cout<<"\n\n";
return 0;
}
Output:
How many Students You Want to Enter ? : 2
Enter Name : raman
Enter Roll No : 1
Enter Class : 12
Enter Marks in subj1 : 77
Enter Marks in subj2 : 77
Enter Marks in subj3 : 77
Enter Marks in subj4 : 77
Enter Marks in subj5 : 88
Enter Name : chaman
Enter Roll No : 2
Enter Class : 12
Enter Marks in subj1 : 55
Enter Marks in subj2 : 55
Enter Marks in subj3 : 55
Enter Marks in subj4 : 66
Enter Marks in subj5 : 77
ENTER ROLL NO : 1
Roll no :1
Percentage :79.2
Grade :A
Programming Problem:
Assume a text file “BOOK.txt” is already created.
Write a complete C++ program to count the number
of alphabets in the file.
#include <iostream>
#include <fstream>
#include <ctype.h>
using namespace std;
int main()
{
ifstream afile("BOOK.txt");
if(!afile)
{
cout << "Error: file cannot be opened." << endl;
return -1;
}
int acount;
acount = 0;
char ch;
afile.get(ch);
while ( !afile.eof())
{
if(isalpha(ch))
acount++;
afile.get(ch);
}
cout << "The number of alphabets = " << acount << endl;
afile.close();
return 0;
}
Output:
Programming Problem:
Assume a text file “book.txt” is already created. Write
a complete C++ program to count the number of
words having first character capital.
//Assume a text file “BOOK.txt” is already created. Using this file create a C++
function
//to count the number of words having first character capital.
#include<iostream>
#include<fstream>
using namespace std;
int main()
{
ifstream Fin("BOOK.txt");
char ch[25];
if(!Fin)
{
cout << "Error: file cannot be opened." << endl;
return -1;
}
int count=0;
Fin >> ch;
while(!Fin.eof())
{
if (isupper(ch[0]))
count++;
Fin>>ch;
}
Fin.close( );
cout<<"\nWords starting with Capital letter = "<< count<<endl;
return 0;
}
Output:
Programming Problem:
Assume a text file “BOOK.txt” is already created.
Write a complete C++ program to count the number
of lowercase and uppercase characters in it.
// Problem : Write a program in a C++ to count the number of lowercase and
// uppercase alphabets
// present in a text file
// “BOOK.txt”.
#include<iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream Fin("BOOK.txt");
char ch;
int count1 = 0, count2 = 0;
Fin.get(ch);
while(!Fin.eof())
{
if (islower(ch))
count1++;
if (isupper(ch))
count2++;
Fin.get(ch);
}
Fin.close();
cout<<"Number of lowercase characters = " <<count1<<endl;
cout<<"Number of uppercase characters = " <<count2<<endl;
return 0;
}
Output:
Programming Problem:
Each node of a stack contains the following information, in addition to a pointer
field:
(i) EmpCode of employee
(ii) EName of employee
Write a complete program in C++ to implement this linked list version of stack
and also implement the following functions:
(a) push() - To push a node into the stack, which is allocated dynamically
(b) pop()- To remove a node from the stack and release the memory
(c) display() - To display the content of the stack at any stage.
#include <iostream>
#include <string.h>
using namespace std;
struct node
{
emp employee;
node * next;
};
class stack
{
private:
node * top;
public:
stack();
void push(emp x);
void pop();
void display();
};
stack::stack()
{
top = NULL;
}
p = new node;
if( p == NULL)
{
cout << "Stack is full.\n";
return ;
}
p->employee.EmpCode = ele.EmpCode;
strcpy(p->employee.EName, ele.EName);
p->next = top;
top = p;
void stack::pop()
{
if( top == NULL)
{
cout <<"Stack is empty.\n";
return;
}
else
{
node * p;
p = top;
top = p->next;
cout << p->employee.EmpCode << " deleted\n";
delete p;
}
void stack::display()
{
if(top != NULL)
{
node * p;
p = top;
cout<<"The Stack :\n";
while( p != NULL)
{
cout<<p->employee.EmpCode<<"\t";
cout<<p->employee.EName<<endl;
p = p->next;
}
}
else
cout << "Stack is empty.\n";
}
int main()
{
stack s;
emp e1;
e1.EmpCode = 101;
strcpy(e1.EName, "Ram");
s.push(e1);
emp e2;
e2.EmpCode = 102;
strcpy(e2.EName, "Shyam");
s.push(e2);
emp e3;
e3.EmpCode = 103;
strcpy(e3.EName, "Madan Lal");
s.push(e3);
s.display();
s.pop();
s.display();
return 0;
}
//////////////////////End of Program//////////////////////////////////
Output:
The Stack :
103 Madan Lal
102 Shyam
101 Ram
103 deleted
The Stack :
102 Shyam
101 Ram
Programming Problem:
///////////////////////Student.cpp///////////////////////////////////////////////////
#include<iostream>
#include<fstream>
#include<iomanip>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
class Student
{
char S_Admno[10];
char S_Name[30];
int Percentage;
public:
void enterdata();
void DisplayData();
int ReturnPercentage();
};
void Student::enterdata()
{
cout<<"Enter the admission number : ";
fflush(stdin);
cin.getline(S_Admno, 10);
cout<<"Enter the name of the student : ";
cin.getline(S_Name, 30);
cout<<"Enter the percentage marks : ";
cin>>Percentage;
}
void Student::DisplayData()
{
cout<<setw(12)<<S_Admno;
cout<<setw(32)<<S_Name;
cout<<setw(3)<<Percentage<<endl;
}
int Student::ReturnPercentage()
{
return Percentage;
}
int main()
{
ofstream fout;
fout.close();
ifstream fin;
return 0;
}
Output:-
struct emp
{
int EmpCode;
char EName[30];
};
struct node
{
emp employee;
node * next;
};
class queue
{
private :
node * front, * rear;
public:
queue();
~queue();
void insert(emp e);
void del();
void display();
};
queue::queue()
{
front = rear = NULL;
}
void queue::insert(emp e)
{
node * t = new node;
if(t ==NULL)
cout<<"Memory is not available i.e., queue is full"<<endl;
else
{
t->employee.EmpCode= e.EmpCode;
strcpy(t->employee.EName, e.EName);
t-> next = NULL;
if(rear == NULL)
{
rear=front=t;
}
else
{
rear -> next = t;
rear = t;
}
}
}
if(front == NULL)
{
cout<<"Queue is empty\n";
return;
}
else
{
node *t = front;
front= front->next;
cout << t->employee.EmpCode << " deleted\n";
delete t;
}
}
queue::~queue()
{
node * t;
while( front != NULL)
{
t = front;
front = front->next;
delete t;
}
}
void queue::display()
{
node *t;
t = front;
cout<<endl;
while( t != NULL)
{
cout<<t->employee.EmpCode<<" , "<<t->employee.EName<<"----->";
t = t->next;
}
}
int main()
{
queue Q;
emp e1;
e1.EmpCode = 101;
strcpy(e1.EName, "Ram");
Q.insert(e1);
emp e2;
e2.EmpCode = 102;
strcpy(e2.EName, "Shyam");
Q.insert(e2);
emp e3;
e3.EmpCode = 103;
strcpy(e3.EName, "Shyam Lal");
Q.insert(e3);
Q.display();
Q.del();
Q.display();
return 0;
}
Output:
///////OOP Stack/////////////////////////////////////////////
//OPP implementation of stack
#include <iostream>
using namespace std;
stack::stack()
{
top = -1;
}
void stack::push(int x)
{
if(top == size - 1)
{
cout <<"Stack is full!\n";
return ;
}
top++;
a[top] = x;
}
int stack::pop()
{
if(top == -1)
{
cout << "Stack is empty!\n";
return -1;
}
int x;
x = a[top];
top--;
return x;
}
void stack::show()
{
cout << "\nContents of stack: \n";
for(int i = top; i >= 0; i--)
cout << a[i] << endl;
}
stack::~stack()
{
cout << "Stack is being destroyed!\n";
}
int main()
{
stack s;
s.push(1);
s.push(3);
s.push(5);
s.push(7);
s.show();
s.pop();
s.pop();
s.show();
return 0;
}
Output:
Contents of stack:
7
5
3
1
Contents of stack:
3
1
Programming Problem:
//////////////OOP Queue/////////////////////////////////////////////////////
//Array and OOP implementation of Queue
#include <iostream>
using namespace std;
}
}
int queue::deletion()
{
if(front == -1)
{
cout << "Queue is empty!\n";
return -1;
}
int x;
x = a[front];
front++;
if(front == rear + 1)
front = rear = -1;
return x;
}
void queue::display()
{
if(front == -1)
{
cout <<"\nQueue is empty!\n";
return;
}
cout << "\n\nThe contents of the queue :\n";
for(int i = front; i <= rear; ++i)
cout << a[i] <<"->";
}
queue::~queue()
{
cout <<"\nQueue is being destroyed!" << endl;
}
int main()
{
queue Q;
Q.display();
Q.insert(11);
Q.insert(22);
Q.insert(33);
Q.insert(44);
Q.insert(55);
Q.display();
Q.deletion();
Q.deletion();
Q.display();
Q.deletion();
Q.display();
Q.deletion();
Q.deletion();
Q.display();
Q.deletion();
return 0;
}
Output:
Queue is empty!
/////////////////////////////Matrix Multiplication////////////////////
#include<iostream>
#include<windows.h>
#include<stdlib.h>
using namespace std;
int main()
{
int a[MAX][MAX], b[MAX][MAX];
int i, j, k;
gotoxy(1,1);
cout<<"Enter the number of rows of Matrix A : ";
int m;
cin>>m;
gotoxy(1,2);
cout<<"Enter the number of columns of Matrix A: ";
int n;
cin>>n;
gotoxy(1,3);
cout<<"Enter the number of rows of Matrix B : ";
int p;
cin>>p;
gotoxy(1,4);
cout<<"Enter the number of columns of Matrix B : ";
int q;
cin>>q;
if ( n != p)
{
cout<<"The matrices are not compatible for multiplication!\n";
system("pause");
exit(0);
}
gotoxy(1,5);
cout<<"Enter the elements of first matrix A :\n ";
for(i = 0; i <m; i++)
for(j = 0; j <n; j++)
{
gotoxy(5*(j+1), 7+i);
cin>>a[i][j];
}
gotoxy(1,8+m);
cout<<"Enter the elements of second matrix B :\n ";
for(i = 0; i <p; i++)
for(j = 0; j <q; j++)
{
gotoxy(5*(j+1), 10+m+i);
cin>>b[i][j];
}
int c[MAX][MAX];
for( i = 0; i < m; i++)
for( j = 0; j < q; j++)
{
int s = 0;
for( k = 0; k < n; k++)
s = s + a[i][k]*b[k][j];
c[i][j] = s;
}
gotoxy(1,12+m + p);
cout<<"Product of the matrices :\n ";
return 0;
}
3 4 5
4 2 1
4 5 0
4 3 7 8
6 7 8 2
1 0 5 -3
41 37 78
29 26 49
46 47 68
Programming Problem:
Write a complete program in C++ that accepts 2 two-dimensional arrays
(matrices) and display the sum and difference of the two matrices.
///////////////Matrix addition and subtraction////////////////////////////
#include<iostream>
#include<windows.h>
using namespace std;
int main()
{
int a[MAX][MAX], b[MAX][MAX];
int i, j;
cout<<"Enter the number of rows : ";
int m;
cin>>m;
cout<<"Enter the number of columns : ";
int n;
cin>>n;
int c[MAX][MAX];
int d[MAX][MAX];
return 0;
}
3 4
5 5
6 4
3 6
4 8
11 5
6 10
9 13
17 9
0 -2
1 -3
-5 -1
Programming Problem:
Write a complete program in C++ to implement insertion sort algorithm. The
program should implement a function InsertSort(int a[], int n) to sort the
integer array a[] of size n.
///////////////insertion sort////////////////////////////
#include<iostream>
#include<stdlib.h>
using namespace std;
int main ()
{
int s[MAX];
cout<<"How many elements are there in the array ? ";
int n;
cin>>n;
cout<<"Now, enter the elements of the array :"<<endl;
int i;
for(i = 0; i<n; i++)
cin>>s[i];
InsertSort(s,n);
return 0;
}
Output:
#include<iostream>
#include<stdlib.h>
using namespace std;
x=y;
y=temp;
int main ()
{
int s[MAX];
cout<<"How many elements are there in the array ? ";
int n;
cin>>n;
SelectSort(s , n);
return 0;
}
Output:-
How many elements are there in the array ? 10
Now, enter the elements of the array :
56 3 45 2 1 3 44 66 33 8
Iteration : 1
1 3 45 2 56 3 44 66 33 8
Press any key to continue . . .
Iteration : 2
1 2 45 3 56 3 44 66 33 8
Press any key to continue . . .
Iteration : 3
1 2 3 45 56 3 44 66 33 8
Press any key to continue . . .
Iteration : 4
1 2 3 3 56 45 44 66 33 8
Press any key to continue . . .
Iteration : 5
1 2 3 3 8 45 44 66 33 56
Press any key to continue . . .
Iteration : 6
1 2 3 3 8 33 44 66 45 56
Press any key to continue . . .
Iteration : 7
1 2 3 3 8 33 44 66 45 56
Press any key to continue . . .
Iteration : 8
1 2 3 3 8 33 44 45 66 56
Press any key to continue . . .
Iteration : 9
1 2 3 3 8 33 44 45 56 66
Press any key to continue . . .
Programming Problem:
Write a complete program in C++ to sort an array a[] of size n using selection
sort and search a given element using Binary Search method. The program
should implement a function SelectSort(int a[], int n) to sort the integer array
a[] of size n and a function BinarySearch(int a[], int n, int ele) to seach the
element ele in the array a[].
///////////////Binary Search////////////////////////////
#include<iostream>
#include<stdlib.h>
using namespace std;
x=y;
y=temp;
}
void display(int s[], int n)
{
if ( s[mid] == ele)
{
found = 'y';
cout<<"Eureka! your element is found at the loaction "<<mid +1<<endl;
break;
}
if( ele < s[mid] )
last = mid - 1;
else first = mid + 1;
}
if ( found == 'n')
int main ()
{
int s[MAX];
cout<<"How many elements are there in the array ? ";
int n;
cin>>n;
return 0;
}
Output:
‘
Programming Problem:
Write a program in C++ to implement the following class:
class student{int rollno;
char name[40];
char Class;
char grade;
float marks;
public:
void getdata(); //to get the data of students
void display(); //to display the data
int getrno(); //to return the roll no.
};
and write the objects of the following class in a file student.dat and assign the
grade as follows:
if (marks>=75 ) then grade='A'
if(marks >= 60 and marks <75) then grade='B'
if(marks>=50 and marks <60) then grade='C';
if (marks>=40 and marks < 50) then grade='D';
if(marks < 40) then grade='F'
////////////////////////////////////////////////////////////////////////////////
#include<iostream>
#include<fstream>
#include<stdio.h>
using namespace std;
int main()
{
ofstream filin;
filin.open("student.dat",ios::out| ios::binary);
if(!filin)
{
cout<<"Cannot open file!!!\n";
return 1;
}
cout<<"Enter data for students : \n";
char ans='y';
student s;
while (ans=='y')
{
s.getdata();
filin.write((char*)&s, sizeof(s));
cout<<"\nRecord written to file.\n";
cout<<"More records?(y/n) ";
cin>>ans;
}
filin.close();
ifstream fin;
s.display();
}
fin.close();
return 0;
}
Output:
Enter data for students :
Enter roll no.1
Enter name:Ram
Enter class:12
Enter marks:89
struct Game
{
char GameName[30];
int NumberOfPlayers;
Game * next;
};
class stack
{
private :
Game * top;
public :
stack();
bool isStackEmpty();
void push(Game g);
void pop();
void display();
};
stack::stack()
{
top = NULL;
}
bool stack::isStackEmpty()
{
return top == NULL;
}
void stack::push( Game g)
{
Game * p;
p = new Game;
if( p == NULL)
{
cout << "Stack is full.\n";
return ;
}
strcpy(p->GameName, g.GameName);
p->NumberOfPlayers = g.NumberOfPlayers;
p->next = top;
top = p;
void stack::pop()
{
if( isStackEmpty())
{
cout <<"Stack is empty.\n";
return;
}
else
{
Game * p;
p = top;
top = p->next;
delete p;
void stack::display()
{
if(!isStackEmpty())
{
Game * p;
p = top;
cout<<"\nThe Stack :\n";
while( p != NULL)
{
cout<<"\t"<<p->GameName<<" : "<<p->NumberOfPlayers<<endl;
p = p->next;
}
}
else
cout << "Stack is empty.\n";
}
int main()
{
stack s;
Game g;
strcpy(g.GameName, "Cricket");
g.NumberOfPlayers = 11;
s.push(g);
strcpy(g.GameName, "Baskeball");
g.NumberOfPlayers = 5;
s.push(g);
strcpy(g.GameName, "Football");
g.NumberOfPlayers = 11;
s.push(g);
strcpy(g.GameName, "Baseball");
g.NumberOfPlayers = 9;
s.push(g);
strcpy(g.GameName, "Kabaddi");
g.NumberOfPlayers = 7;
s.push(g);
s.display();
s.pop();
s.pop();
s.display();
return 0;
Output : -
The Stack :
Kabaddi : 7
Baseball : 9
Football : 11
Baskeball : 5
Cricket : 11
The Stack :
Football : 11
Baskeball : 5
Cricket : 11
Programming Problem:
Write a complete program in C++ to implement a function SelectSort() to
sort the objects of the following structure in descending order of their points:
struct IPL
{
char teamname[20];
int points;
float net_rr;
};
///////////////Selection Sort////////////////////////////////////
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
struct IPL
{
char teamname[30];
int points;
float net_rr;
};
void swap( IPL &x,IPL &y)
{
IPL temp;
temp=x;
x=y;
y=temp;
}
}
int main ()
{
IPL s[MAX];
cout<<"How many teams are there? ";
int n;
cin>>n;
////////////sorting////////////////////////////////////
SelectSort(s , n);
///////displaying sorted array of objects/////////////
display(s,n);
return 0;
}
//////////////////////////////////////////////////////////////////////////////////
Output:
How many teams are there? 8
Now, enter the details of the teams :
Team Name : Kings XI Punjab
Points : 22
Net RR : 0.968
Team Name : Kolkata Knight Riders
Points : 18
Net RR : 0.418
Team Name : Chennai Super Kings
Points : 18
Net RR : 0.385
Team Name : Mumbai Indians
Points : 14
Net RR : 0.095
Team Name : Rajasthan Royals
Points : 14
Net RR : 0.060
Team Name : Sunrisers Hyderabad
Points : 12
Net RR : -0.399
Team Name : R C Bangalore
Points : 10
Net RR : -0.428
Team Name : Delhi Daredevils
Points : 4
Net RR : -1.182
Team Points Net RR
----------------------------------------------------------------
-
Kings XI Punjab 22 0.968
Kolkata Knight Riders 18 0.418
Chennai Super Kings 18 0.385
Mumbai Indians 14 0.095
Rajasthan Royals 14 0.06
Sunrisers Hyderabad 12 -0.399
R C Bangalore 10 -0.428
Delhi Daredevils 4 -1.182
SQL Using MariaDB
Create database db1;
Use db1;
describe product;
describe Client;
describe client;
alter table client
modify tel_no integer(15);
describe client;