Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 88

C++ Program File

Class XII
Session 2019-20
Computer Science with C++
(Code – 283)

R P V V RAJ NIWAS MARG, DELHI -110054


Submitted by- Shubham Yadav
Class- XIIth E
Roll no.-26652382
Programming Environment
Operating System: Windows 7/8/8.1/10
C++ IDE: Code::Blocks version 16.01/ 17.12

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.

8. Write a complete program in C++ to illustrate


the use of write() to write a string to a text file.
S. No. Practical Page Sign. of Teacher
No.
9. Write a complete program in C++ to open a
file in binary mode and use read() function to
read its content.
10. Write a complete program in C++ to illustrate
the use of binary file, creating a binary file
using a structure student.
11. 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.
12.. Assume a text file “BOOK.txt” is already
created. Write a complete C++ program to
count the number of alphabets in the file.
13. Assume a text file “book.txt” is already
created. Write a complete C++ program to
count the number of words having first
character capital.
14. 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.
S. No. Practical Page Sign. of Teacher
No.
15. 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.

16. Assuming the class Student as declared below,


write a complete program in C++
(a) to write the objects of Student to a binary
file Student.dat
(b) to read the objects of Student from this file
and display those student details whose
percentage is more than 75.
class Student
{
char S_Admno[10];
char S_Name[30];
int Percentage;
public:
void enterdata();
void DisplayData();
int ReturnPercentage();
};
NOTE:-You have to implement member
functions accordingly.

17. Each node of a queue contains the following


information, in addition to a pointer field:
S. No. Practical Page Sign. of Teacher
No.
(i) EmpCode of employee
(ii) Ename of employee
Write a complete program in C++ to
implement this linked list version of queue and
also implement the following functions:
(a) add() - To add a node at the rear of queue
which is allocated dynamically
(b) del()- To remove a node from the front of
the queue and release the memory
(c) display() - To display the content of the
queue at any stage.

18. Write a complete C++ program to implement


class STACK and the following functions:
(a) push() - To push a node into the stack
(b) pop()- To remove a node from the stack
(c) display() - To display the content of the
stack at any stage.

19. Write a complete program in C++ to


implement class QUEUE and also implement
the following functions:
(a) add() - To add a node at rear of the queue
(b) del()- To remove a node from front of the
queue
(c) display() - To display the content of the
queue at any stage

20. Write a complete program in C++ that accepts


2 two-dimensional arrays (matrices) and
display the product of the two matrices if they
are compatible for multiplication otherwise
S. No. Practical Page Sign. of Teacher
No.
print the error message about incompatibility
for multiplication.
21. Write a complete program in C++ that accepts
2 two-dimensional arrays (matrices) and
display the sum and difference of the two
matrices.
22. 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.
23. Write a complete program in C++ to
implement selection sort algorithm. The
program should implement a function
SelectSort(int a[], int n) to sort the integer
array a[] of size n.
24. 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[].
S. No. Practical Page Sign. of Teacher
No.
25. 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'
S. No. Practical Page Sign. of Teacher
No.
26. Write a complete program in C++ to
implement a dynamically allocated
Stack containing the objects of the
following structure:
struct Game
{
char GameName[30];
int NumberOfPlayers;
Game * next;
};
You have to implement the member functions
push(), pop() and display() of class stack.
27. 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;
};

28. SQL Queries using MariaDB

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 default constructor, defined outside the class


person::person()
{
UID = 0;
strcpy(name, "Mr. X");
gender = 'M';
annual_income = 0.0;
}
// Definition of parameterized constructor, defined outside the class
person:: person(long uid, char n[], char gen, float a_i)
{
UID = uid;
strcpy(name, n);
gender = gen;
annual_income = a_i;
}
void person::get_data()
{
cout << "\n\nEnter the details of the person:" << endl;
cout << "Aadhar Number (UID) : "; cin >> UID;
cout << "Name : "; cin >> name;
fflush(stdin);
cout << "Gender : "; cin >> gender;
cout << "Annual income(in Rs.) : "; cin >> annual_income;

}
// 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();

person P2(12121212, "Chaman Lal", 'M', 60000); // implicit call to parameterized


// constructor
P2.display();

person P3 = person(13131313, "Madan Lal", 'M', 120000); // explicit call to


//parametrized constructor
P3.display();

person P4; // implicit call to default constructor


P4.get_data(); // calling the function to get data
P4.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

Enter the details of the person:


Aadhar Number (UID) : 14141515
Name : Ram Lal
Gender :M
Annual income(in Rs.) : 80000
Person's details:
Aadhar Number (UID) : 14141515
Name : Ram
Gender :M
Annual income(in Rs.) : 80000

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()
{

cout << "The details of the student:" << endl;


cout << "Roll No. : " << roll_no << endl;
cout << "Name : " << name << endl;
cout << "Percentage marks: " << perc_marks << endl;
}

// main function
int main()
{
student s1; // default constructor is called implicitly
s1.display();

student s2(2, "Rishabh Kumar", 89); // implicit call to parameterized constructor


s2.display();

student s3(s2); // copy constructor called implicitly


s3.display();

student s4 = s3; // copy constructor called to assign one object to another


s4.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;
}
};

// student is also a person


// student class derived from person class
class student : public person
{
protected:
int admn_no;
int Class;
char section;
public:
void in_data()
{
fflush(stdin);
get_data();
cout << "Admission No. : "; cin >> admn_no;
cout << "Class(1-12) : "; cin >> Class;
cout << "Section(A/B/C/D) : "; cin >> section;
}
void out_data()
{
show_data();
cout << "Admission No. : " << admn_no << endl;
cout << "Class(1-12) : " << Class << endl;
cout << "Section(A/B/C/D) : " << section << endl;
}
};

// main function
int main()
{
cout << "Base class:" << endl;
person P;
P.get_data();
P.show_data();

cout << "Derived class:" << endl;


student S;
S.in_data();
S.out_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();

// public member doj of date type calling member functions


cout << "Date of joining(dd mm yyyy): ";
s.doj.get_date();
s.doj.show_date();

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.

//Program to illustrate opening of a file using open()


#include<iostream>
#include<fstream>
using namespace std;

int main()
{
char fileName[80];

cout<<"Enter the name of the file : ";


cin>>fileName;

ofstream fout;

fout.open(fileName, ios::out); //opening the file for writing


fout<<"Sisters and Brothers of America,\n";
fout<<"\n";
fout<<"It fills my heart with joy unspeakable to rise in response\n";
fout<<"to the warm and cordial welcome which you have given us. I thank\n";
fout<<"you in the name of the most ancient order of monks in the world!\n";

fout.close(); //closing the file using close()

return 0;
}

Output:

Enter the name of the file : Discourse.txt

The content of the file:

Sisters and Brothers of America,

It fills my heart with joy unspeakable to rise in response


to the warm and cordial welcome which you have given us. I thank
you in the name of the most ancient order of monks in the world!

Programming Problem:
Write a complete program in C++ to open a text file
for writing purpose using constructor and insert text
in the file.

//Program to illustrate opening of a file using a constructor


#include<fstream>
using namespace std;

int main()
{
ofstream myFile("poem.txt");

myFile<<"Never let a thought shrivel and die\n";


myFile<<"For want of a way to say it\n";
myFile<<"For English is a wonderful game\n";
myFile<<"And all of you can play it\n";
myFile<<"All that you do is match the words\n";
myFile<<"To the brightest thoughts in your head\n";
myFile<<"So that they come out clear and true\n";
myFile<<"And handsomely groom and fed\n";

myFile.close();

return 0;

The content of the file poem.txt:

Never let a thought shrivel and die


For want of a way to say it
For English is a wonderful game
And all of you can play it
All that you do is match the words
To the brightest thoughts in your head
So that they come out clear and true
And handsomely groom and fed

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];

ifstream myFile("poem.txt"); //opening the file for reading

//reads a line into the array arr using getline()


myFile.getline(arr, size);

cout<<arr; //displays on the screen

myFile.close();

return 0;
}

Output:

Never let a thought shrivel and die

Programming Problem:
Write a complete program in C++ to illustrate the use
of write() to write a string to a text file.

//Program to illustrate the use of write() function


#include<fstream>
using namespace std;

int main()
{
char buffer[100] ="Where there is a will, there is a way";

ofstream aFile("saying.txt", ios::out);


aFile.write(buffer, 100);

aFile.close();

return 0;
}

The content of saying.txt:

Where there is a will, there is a way

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];

ifstream myFile("saying.txt", ios::in | ios::binary); //opening file for reading


in binary mode

myFile.read(buffer, 100);

cout<<buffer; //prints on the screen

myFile.close();

return 0;
}

Output:

Where there is a will, there is a way

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));

cout<<"\nMore records (y/n) : ";


cin>>ch;
} while((ch != 'n') &&(ch != 'N'));
fout.close();
cout<<"\n\n";

return 0;

}
Output:

Enter the name of the student : aman

Enter percentage : 99

More records (y/n) : y


Enter the name of the student : chaman

Enter percentage : 88

More records (y/n) : y


Enter the name of the student : raman

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.

//Program to illustrate the use of binary files


#include<iostream>
#include<fstream>
using namespace std;

//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:

The number of alphabets = 99

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:

Words starting with Capital letter = 4

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:

Number of lowercase characters = 95


Number of uppercase characters = 4

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.

//////////////Linked List version of Stack////////////////////////////////

#include <iostream>
#include <string.h>
using namespace std;

const int size = 10;


struct emp
{
int EmpCode;
char EName[30];
};

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;
}

void stack::push( emp ele)


{
node * p;

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:

Assuming the class Student as declared below; write a complete program in


C++
(a) to write the objects of Student to a binary file Student.dat
(b) to read the objects of Student from this file and display those student
details whose percentage is more than 75.
class Student
{
char S_Admno[10];
char S_Name[30];
int Percentage;
public:
void enterdata();
void DisplayData();
int ReturnPercentage();
};
NOTE:-You have to implement member functions accordingly.

///////////////////////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.open("Student.dat", ios::binary | ios::out);


if(!fout)
{
cout<<"Unable to open file!\n";
exit(-1);
}
Student St;
char ans = 'n';
do
{
St.enterdata();

fout.write((char *) &St, sizeof(St));


cout<<"More records?(y/n) : ";
cin>>ans;
}while(ans == 'y' || ans == 'Y');

fout.close();

ifstream fin;

fin.open("Student.dat", ios::binary | ios::in);


if(!fin)
{
cout<<"Unable to open file!\n";
exit(-1);
}

while(fin.read((char *) &St, sizeof(St)))


{
if(St.ReturnPercentage()> 75)
St.DisplayData();
}
fin.close();

return 0;
}

Output:-

Enter the admission number : 23456


Enter the name of the student : Ram Kumar
Enter the percentage marks : 89
More records?(y/n) : y
Enter the admission number : 23457
Enter the name of the student : Shyam Lal
Enter the percentage marks : 88
More records?(y/n) : n
23456 Ram Kumar 89
23457 Shyam Lal 88
Programming Problem:
Each node of a queue 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
queue and also implement the following functions:
(a) add() - To add a node at the rear of queue which is allocated dynamically
(b) del()- To remove a node from the front of the queue and release the
memory
(c) display() - To display the content of the queue at any stage.

////Linked List version of queue////////////////////////////////////////


#include<iostream>
#include<string.h>
using namespace std;

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;
}
}
}

void queue :: del()


{

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:

101 , Ram----->102 , Shyam----->103 , Shyam Lal----->


102 , Shyam----->103 , Shyam Lal----->
Programming Problem:
Write a complete C++ program to implement class STACK and the following
functions:
(a) push() - To push a node into the stack
(b) pop()- To remove a node from the stack
(c) display() - To display the content of the stack at any stage.

///////OOP Stack/////////////////////////////////////////////
//OPP implementation of stack
#include <iostream>
using namespace std;

const int size = 10;


class stack
{
private:
int a[size];
int top;
public:
stack();
void push(int x);
int pop();
void show();
~stack();
};

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:

Write a complete program in C++ to implement class QUEUE and also


implement the following functions:
(a) add() - To add a node at rear of the queue
(b) del()- To remove a node from front of the queue
(c) display() - To display the content of the queue at any stage

//////////////OOP Queue/////////////////////////////////////////////////////
//Array and OOP implementation of Queue

#include <iostream>
using namespace std;

const int size = 10;


class queue
{
private:
int a[size];
int front, rear;
public:
queue();
void insert(int x);
int deletion();
void display();
~queue();
};
queue::queue()
{
front = rear = -1;
}
void queue::insert(int x)
{
if(rear == size - 1)
{
cout << "Queue is full!\n";
return ;
}
if(front == -1)
{
front = rear = 0;
a[rear] = x;
}
else
{
rear++;
a[rear] = x;

}
}
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!

The contents of the queue :


11->22->33->44->55->

The contents of the queue :


33->44->55->

The contents of the queue :


44->55->
Queue is empty!
Queue is empty!
Programming Problem:

Write a complete program in C++ that accepts 2 two-dimensional arrays


(matrices) and display the product of the two matrices if they are compatible
for multiplication otherwise print the error message about incompatibility for
multiplication.

/////////////////////////////Matrix Multiplication////////////////////

#include<iostream>
#include<windows.h>
#include<stdlib.h>
using namespace std;

const int MAX = 10;


void gotoxy(short, short);

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 ";

for(i = 0; i <m; i++)


for(j = 0; j <n; j++)
{
gotoxy(5*(j+1), 14+m+p+i);
cout<<c[i][j]<<endl;
}

return 0;
}

void gotoxy(short col, short row)


{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = {col, row};
SetConsoleCursorPosition(h, position);
}
Output:
Enter the number of rows of Matrix A : 3
Enter the number of columns of Matrix A: 3
Enter the number of rows of Matrix B : 3
Enter the number of columns of Matrix B : 4
Enter the elements of first matrix A :

3 4 5
4 2 1
4 5 0

Enter the elements of second matrix B :

4 3 7 8
6 7 8 2
1 0 5 -3

Product of the matrices :

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;

const int MAX = 10;


void gotoxy(short, short);

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;

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), 5+i);
//cout<<"a["<<i<<"]["<<j<<"] = ";
cin>>a[i][j];
}
gotoxy(1,12);
cout<<"Enter the elements of second matrix B :\n ";
for(i = 0; i <m; i++)
for(j = 0; j <n; j++)
{
gotoxy(5*(j+1), 14+i);
cin>>b[i][j];
}

int c[MAX][MAX];

for(i = 0; i <m; i++)


for(j = 0; j <n; j++)
{
c[i][j] = a[i][j] + b[i][j];
}
gotoxy(1,20);
cout<<"Sum of the matrices :\n ";
for(i = 0; i <m; i++)
for(j = 0; j <n; j++)
{
gotoxy(5*(j+1), 22+i);
cout<<c[i][j]<<endl;
}

int d[MAX][MAX];

for(i = 0; i <m; i++)


for(j = 0; j <n; j++)
{
d[i][j] = a[i][j] - b[i][j];
}
gotoxy(1,27);
cout<<"Diffrence of the matrices :\n ";
for(i = 0; i <m; i++)
for(j = 0; j <n; j++)
{
gotoxy(5*(j+1), 29+i);
cout<<d[i][j]<<endl;
}

return 0;
}

void gotoxy(short col, short row)


{
HANDLE h = GetStdHandle(STD_OUTPUT_HANDLE);
COORD position = {col, row};
SetConsoleCursorPosition(h, position);
}
Output:

Enter the number of rows : 3


Enter the number of columns : 2
Enter the elements of first matrix A :

3 4
5 5
6 4

Enter the elements of second matrix B :

3 6
4 8
11 5

Sum of the matrices :

6 10
9 13
17 9

Diffrence of the matrices :

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;

const int MAX = 20;


void display(int s[], int n)
{

for( int j = 0 ; j <n ; j++)


cout<<s[j]<<" ";
cout<<endl;
system ("pause");
}
void InsertSort(int s[], int n)
{
int i, j, k;
for ( i = 1; i <n; i++)
{
int temp = s[i];

for( j = 0 ; j<i && temp > s[j]; j++) ; //null statement


for( k = i-1; k >= j; k--)
s[k+1] = s[k];
s[j] = temp;

cout<<" Iteration : "<<i<<endl;


display(s,n);
}
}

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:

How many elements are there in the array ? 10


Now, enter the elements of the array :
45 3 45 2 11 67 89 99 4 33
Iteration : 1
3 45 45 2 11 67 89 99 4 33
Press any key to continue . . .
Iteration : 2
3 45 45 2 11 67 89 99 4 33
Press any key to continue . . .
Iteration : 3
2 3 45 45 11 67 89 99 4 33
Press any key to continue . . .
Iteration : 4
2 3 11 45 45 67 89 99 4 33
Press any key to continue . . .
Iteration : 5
2 3 11 45 45 67 89 99 4 33
Press any key to continue . . .
Iteration : 6
2 3 11 45 45 67 89 99 4 33
Press any key to continue . . .
Iteration : 7
2 3 11 45 45 67 89 99 4 33
Press any key to continue . . .
Iteration : 8
2 3 4 11 45 45 67 89 99 33
Press any key to continue . . .
Iteration : 9
2 3 4 11 33 45 45 67 89 99
Press any key to continue . . .
Programming Problem:
Write a complete program in C++ to implement selection sort algorithm. The
program should implement a function SelectSort(int a[], int n) to sort the
integer array a[] of size n.
///////////////selection sort////////////////////////////

#include<iostream>
#include<stdlib.h>
using namespace std;

const int MAX = 20;

void swap( int &x,int &y)


{
int temp;
temp=x;

x=y;
y=temp;

void display(int s[], int n)


{

for( int j = 0 ; j <n ; j++)


cout<<s[j]<<" ";
cout<<endl;
system ("pause");
}
void SelectSort(int s[], int n)
{
int i ,j,k;
for ( i = 0; i <n; i++)
{
int min = s[i];
k = i;
for ( j = i+1 ; j < n; j++)
if (min > s[j])
{
min = s[j];
k = j;
}
swap(s[k], s[i]);

cout<<" Iteration : "<<i+1<<endl;


display(s,n);
}
}

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];

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;

const int MAX = 20;

void swap( int &x,int &y)


{
int temp;
temp=x;

x=y;
y=temp;

}
void display(int s[], int n)
{

for( int j = 0 ; j <n ; j++)


cout<<s[j]<<" ";
cout<<endl;
system ("pause");
}

void SelectSort(int s[], int n)


{
int i ,j,k;
for ( i = 0; i <n; i++)
{
int min = s[i];
k = i;
for ( j = i+1 ; j < n; j++)
if (min > s[j])
{
min = s[j];
k = j;
}
swap(s[k], s[i]);
}
}

void BinarySearch(int s[], int n, int ele)


{
int first, last, mid;
first = 0;
last = n -1;
char found = 'n';
while( first <= last)
{
mid = (first + last) / 2;

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')

cout<<"Sorry! your integer is not in the list.\n";

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];
SelectSort(s,n);
cout<<"The array in sorted form :\n";
display(s,n);

cout<<"\n\nEnter the element you want to find : ";


int ele;
cin>>ele;
BinarySearch(s, n, ele); //binary search function call

return 0;
}
Output:

How many elements are there in the array ? 10


Now, enter the elements of the array :
34 3 45 22 36 78 90 10 5 67
The array in sorted form :
3 5 10 22 34 36 45 67 78 90
Press any key to continue . . .

Enter the element you want to find : 78


Eureka! your element is found at the location 9


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;

class student{int rollno;


char name[40];
char Class;
char grade;
float marks;
public:
void getdata();
void display();
int getrno();
};

void student:: getdata()


{
char ch;
fflush(stdin);
cout<<"Enter roll no."; cin>>rollno;
fflush(stdin);
cout<<"Enter name:"; cin.getline(name,40);
cout<<"Enter class:"; cin>>Class;
fflush(stdin);
cout<<"Enter marks:"; cin>>marks;
cout<<"\n";
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 student::display(void)
{
cout<<name<<", Roll no. "<<rollno<<" has "
<<" marks "<<marks<<" and grade "<<grade<<"\n";
}
int student:: getrno()
{
return rollno;

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;

fin.open("student.dat", ios::in| ios::binary);

while( fin.read((char*)&s, sizeof(s)))


{

s.display();
}
fin.close();
return 0;
}

Output:
Enter data for students :
Enter roll no.1
Enter name:Ram
Enter class:12
Enter marks:89

Record written to file.


More records?(y/n) y
Enter roll no.2
Enter name:Sohan
Enter class:12
Enter marks:56

Record written to file.


More records?(y/n) n
Ram, Roll no. 1 has marks 89 and grade A
Sohan, Roll no. 2 has marks 56 and grade C
Programming Problem:
Write a complete program in C++ to implement a dynamically allocated
Stack containing the objects of the following structure:
struct Game
{
char GameName[30];
int NumberOfPlayers;
Game * next;
};
You have to implement the member functions push(), pop() and display() of
class stack.

//////////////dynamically allocated stack/////////////////////////////////


#include <iostream>
#include <string.h>
using namespace std;

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;

const int MAX = 20;

struct IPL
{
char teamname[30];
int points;
float net_rr;
};
void swap( IPL &x,IPL &y)
{
IPL temp;
temp=x;

x=y;
y=temp;

void display(IPL s[], int n)


{
cout<<"Team\t\t\t\t\t\tPoints\tNet RR\n";
cout <<"--------------------------------------------------------------\n";
for( int j = 0 ; j <n ; j++)
cout<<s[j].teamname<<"\t\t\t\t\t"<<s[j].points<<"\t"<<s[j].net_rr<<endl;;
cout<<endl;
system ("pause");
}
void SelectSort(IPL s[], int n)
{
int i ,j,k;
for ( i = 0; i <n; i++)
{
int maxpoints = s[i].points;
k = i;
for ( j = i+1 ; j < n; j++)
if (maxpoints < s[j].points)
{
maxpoints = s[j].points;
k = j;
}
swap(s[k], s[i]);

}
}

int main ()
{
IPL s[MAX];
cout<<"How many teams are there? ";
int n;
cin>>n;

cout<<"Now, enter the details of the teams :"<<endl;


int i;
for(i = 0; i<n; i++)
{
fflush(stdin);
cout<<"Team Name : ";
cin.getline(s[i].teamname, 30);
cout <<"Points : ";
cin>>s[i].points;
cout<<"Net RR : ";
cin >> s[i].net_rr;
}

////////////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;

Create table Product


(P_ID CHAR(4) NOT NULL PRIMARY KEY,
ProductName CHAR(20) NOT NULL,
Manufacturer CHAR(3) NOT NULL,
Price INTEGER);
Create table Client
(C_ID INTEGER NOT NULL PRIMARY KEY,
ClientName CHAR(20) NOT NULL,
City CHAR(15),
P_ID CHAR(4),
Foreign Key(P_ID) references Product(P_ID));
Show tables;

describe product;
describe Client;

insert into product values ("TP01", "TALCOM POWDER", "LAK",40);

insert into product values ("FW05", "FACE WASH", "ABC",45);

insert into product values ("BS01", "BATH SOAP", "ABC",55);

insert into product values ("SH06", "SHAMPOO", "XYZ",120);

insert into product values ("FW12", "FACE WASH", "XYZ",95);


select * from Product;

insert into client values (01, "COSMETIC SHOP", "DELHI", "FW05");

insert into client values (06, "TOTAL HEALTH", "MUMBAI", "BS01");

insert into client values (12, "LIVE LIFE", "DELHI", "SH06");

insert into client values (15, "PRETTY WOMAN", "DELHI", "FW12");

insert into client values (16, "DREAMS", "BANGLORE", "TP01");


select * from Client;
select * from client where city = "Delhi";

select * from product where Price between 50 and 100;


select ClientName, City, ProductName, Price
from client, product
where client.p_id = product.p_id;

select distinct city from Client;


select Manufacturer, Max(Price), Min(Price), Count(*)
from product
group by Manufacturer;

select Manufacturer, Max(Price), Min(Price), Count(*)


from product
group by Manufacturer having Count(*) > 1;
select ClientName, Manufacturer
from client, product
where client.p_id = product.p_id;

select ProductName, Price*4


from product;
select * from product order by price asc;

select * from product where ProductName = "shampoo" or ProductName =


"face wash";
select ClientName from client where city = "Delhi" and p_id = "SH06";

select * from product


where ProductName like "fa%";

select * from product


where ProductName NOT like "fa%";
select ClientName from client
where city in ("Delhi", "Banglore");

Update product set price = price + 10;

select * from product;


Create View DelhiClient
as select *
from client
where city = "Delhi";

select * from DelhiClient;


delete from client where city = "Banglore";

select * from client;

alter table client


add (tel_no integer );

describe client;
alter table client
modify tel_no integer(15);

describe client;

alter table client


drop tel_no;
describe Client;

You might also like