Saturday, June 26, 2021

Write a function template selection sort. Write a program that inputs, sorts and outputs an integer array and a float array.

 

Problem Statement:

            Implement a function template selection sort. Write a program that inputs, sort & output on integer array and a float array.

 

Objectives:

       To learn the concept of template.

 

Algorithm:

    1)     Start
    2)    Declare the template parameter T.
    3)    Define template function for selection sort.
    4)    In main() Define two arrays, one for integer & another for float and take input for both the arrays            all sorting function template to sort the number.
    5)    Stop.


Program:

#include<iostream>

using namespace std;

int n;

#define size 10

template<class T>

void sel(T A[size])

{

int i,j,min;

T temp;

for(i=0;i<n-1;i++)

{

min=i;

for(j=i+1;j<n;j++)

{

if(A[j]<A[min])

min=j;

}

temp=A[i];

A[i]=A[min];

A[min]=temp;

}

cout<<"\nSorted array:";

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

{

cout<<" "<<A[i];

}

}

int main()

{

int A[size];

float B[size];

int i;

int ch;

do

{

cout<<"\n* * * * * SELECTION SORT SYSTEM * * * * *";

cout<<"\n--------------------MENU-----------------------";

cout<<"\n1. Integer Values";

cout<<"\n2. Float Values";

cout<<"\n3. Exit";

cout<<"\n\nEnter your choice : ";

cin>>ch;

switch(ch)

{

case 1:

cout<<"\nEnter total no of int elements:";

cin>>n;

cout<<"\nEnter int elements:";

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

{

cin>>A[i];

}

sel(A);

break;

case 2:

cout<<"\nEnter total no of float elements:";

cin>>n;

cout<<"\nEnter float elements:";

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

{

cin>>B[i];

}

sel(B);

break;

case 3:

exit(0);

}

}while(ch!=3);

return 0;

}

 

Output:

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

 

Enter your choice : 1

 

Enter total no of int elements:5

 

Enter int elements:45

99

79

05

11

 

Sorted array: 5 11 45 79 99

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

 

Enter your choice : 2

 

Enter total no of float elements:5

 

Enter float elements:25.3

55.9

11.8

78.4

68.0

 

Sorted array: 11.8 25.3 55.9 68 78.4

* * * * * SELECTION SORT SYSTEM * * * * *

--------------------MENU-----------------------

1. Integer Values

2. Float Values

3. Exit

 

Enter your choice : 3

 

--------------------------------

Process exited after 184.3 seconds with return value 0

Press any key to continue . . .








Tuesday, June 15, 2021

Creating a class which uses the concept of inheritance, displays data and data members and uses the concept of exception handling using the C++ language.

 Problem statement:

        Imagine a publishing company which does marketing for book and audio Cassette Versions. Create a class publication that stores the title (a string) and price (type float) of publications from this class derive two classes: book which adds. a page count (type int) and tape which adds a playing time in minutes (type float). Write a Program that instantiates the book and tape class, allows user to enter data and displays the data members. If an exception is caught, replace all the data member values, with Zero Values.

Objectives:

       To learn the concept of inheritance and exception handling.

Algorithm:

  1. Start
  2. Create classes Publication book and tape.
  3. Publication class having data member title, price.
  4. Class Book having data members pages and member functions getData() and putData().
  5. Class Tape having data members minutes and member functions getData() and putData().
  6. Create an object book class book and object of classtape.
  7. Stop.

Program:

# include<iostream>

# include<string.h>

using namespace std;

class publication // declaring class Publication

{

private:

            string title;

            float price;

public:

            void add()

            {

                        cout << "\nEnter the Publication information : " << endl;

                        cout << "Enter Title of the Publication : ";

                        cin.ignore();

                        getline(cin, title);

                        cout << "Enter Price of Publication : ";

                        cin >> price;

            }

            void display()

            {

                        cout << "\n--------------------------------------------------";

                        cout << "\nTitle of Publication : " << title;

                        cout << "\nPublication Price : " << price;

            }

};

class book : public publication // declaring class book which inherits class publication in public mode.

{

private:

            int page_count;

public:

            void add_book()

            {

            try

            {

                        add();

                        cout << "Enter Page Count of Book : ";

                        cin >> page_count;

                        if (page_count <= 0)

                        {

                                    throw page_count;

                        }

            }

            catch(...)

            {

                        cout << "\nInvalid Page Count!!!";

                        page_count = 0;

            }

            }

            void display_book()

            {

                        display();

                        cout << "\nPage Count : " <<page_count;

                        cout << "\n--------------------------------------------------\n";

            }

};

class tape : public publication // declaring class tape which inherits class publication in public mode

{

private:

            float play_time;

public:

            void add_tape()

{

try

{

            add();

            cout << "Enter Play Duration of the Tape in minutes:";

            cin >> play_time;

            if (play_time <= 0)

            throw play_time;

}

catch(...)

{

            cout << "\nInvalid Play Time!!!";

            play_time = 0;

}

}

void display_tape()

{

            display();

            cout << "\nPlay Time : " <<play_time << " min";

            cout << "\n--------------------------------------------------\n";

}

};

int main()

{

            book b1[10]; // object of class book

            tape t1[10]; // object of class tape

            int ch, b_count = 0, t_count = 0;

            do

            {

                        cout << "\n* * * * * PUBLICATION DATABASE SYSTEM * * * * *";

                        cout << "\n--------------------MENU-----------------------";

                        cout << "\n1. Add Information to Books";

                        cout << "\n2. Add Information to Tapes";

                        cout << "\n3. Display Books Information";

                        cout << "\n4. Display Tapes Information";

                        cout << "\n5. Exit";

                        cout << "\n\nEnter your choice : ";

                        cin >> ch;

                        switch(ch)

                        {

                        case 1:

                                    b1[b_count].add_book();

                                    b_count ++;

                                    break;

                        case 2:

                                    t1[t_count].add_tape();

                                    t_count ++;

                                    break;

                        case 3:

                                    cout << "\n* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *";

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

                                    {

                                                b1[j].display_book();

                                    }

                                    break;

                        case 4:

                                    cout << "\n* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *";

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

                                    {

                                                t1[j].display_tape();

                                    }

                                    break;

                        case 5:

                                    exit(0);

                        }

            }while (ch != 5);

return 0;

}


Output:

* * * * * PUBLICATION DATABASE SYSTEM * * * * *

--------------------MENU-----------------------

1. Add Information to Books

2. Add Information to Tapes

3. Display Books Information

4. Display Tapes Information

5. Exit

 

Enter your choice : 1

 

Enter the Publication information :

Enter Title of the Publication : Object Oriented Programing

Enter Price of Publication : 360

Enter Page Count of Book : 410

 

* * * * * PUBLICATION DATABASE SYSTEM * * * * *

--------------------MENU-----------------------

1. Add Information to Books

2. Add Information to Tapes

3. Display Books Information

4. Display Tapes Information

5. Exit

 

Enter your choice : 2

 

Enter the Publication information :

Enter Title of the Publication : Storytel

Enter Price of Publication : 250

Enter Play Duration of the Tape in minutes:280

 

* * * * * PUBLICATION DATABASE SYSTEM * * * * *

--------------------MENU-----------------------

1. Add Information to Books

2. Add Information to Tapes

3. Display Books Information

4. Display Tapes Information

5. Exit

 

Enter your choice : 3

 

* * * * BOOK PUBLICATION DATABASE SYSTEM * * * *

--------------------------------------------------

Title of Publication : Object Oriented Programing

Publication Price : 360

Page Count : 410

--------------------------------------------------

 

* * * * * PUBLICATION DATABASE SYSTEM * * * * *

--------------------MENU-----------------------

1. Add Information to Books

2. Add Information to Tapes

3. Display Books Information

4. Display Tapes Information

5. Exit

 

Enter your choice : 4

 

* * * * TAPE PUBLICATION DATABASE SYSTEM * * * *

--------------------------------------------------

Title of Publication : Storytel

Publication Price : 250

Play Time : 280 min

--------------------------------------------------

 

* * * * * PUBLICATION DATABASE SYSTEM * * * * *

--------------------MENU-----------------------

1. Add Information to Books

2. Add Information to Tapes

3. Display Books Information

4. Display Tapes Information

5. Exit

 

Enter your choice : 5

 

--------------------------------

Process exited after 130.7 seconds with return value 0

Press any key to continue . . .







Conclusion:

Hence, we have successfully understand the Concept of Inheritance and exception handling.


Monday, June 14, 2021

Personal information system using constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation.

Experiment:

Develop a program in C++ to create a database of student’s information system containing the following information: Name, Roll number, Class, Division, Date of Birth, Blood group, Contact address, Telephone number, Driving license no. and other. Construct the database with suitable member functions. Make use of constructor, default constructor, copy constructor, destructor, static member functions, friend class, this pointer, inline code and dynamic memory allocation operators-new and delete as well as exception handling.

Problem Statement:

            Develop an object-oriented programming in C++ to create a database of the Personal information System Containing the following information:

            Name, Date of Birth, Blood Group, Height, Weight, Insurance policy Number, Contact address, Telephone number, driving license no. etc.

            Construct the Database with suitable member functions for initializing and destroying the data via constructer, default constructer, copy constructer, destructor, static member function, friend class, this pointer, inline code, and dynamic memory allocation operators- new and delete as well as exception handling.

Objectives:

            To learn the concept of Constructor, default Constructor, copy constructor, destructor, static member functions, friend class, this pointer, inline code, and dynamic memory allocation operators- new and delete.   

Algorithm:

1.     Start

2.     Read Personal information such as Name, Date of Birth, Blood group, Height, Weight, Insurance policy, Contact address, Telephone number driving license no.

3.     Print all information from database

4.     Stop

Program:

#include<iostream>

#include<string.h>

using namespace std;

class Data

{

string address;

long int *mobile_no;

long int *dl_no;

public:

Data(); // default constructor of class Data

~Data(); // Destructor of class Data

void input_student_data();

void display_student_data();

friend class Student;

};

class Student

{

string Name;

int Roll;

string Class;

char *Div;

string dob;

char *bg;

static int count;

public:

Student();

~Student();

static int get_count()

{

return count;

}

void input_data(Data *);

void display_data(Data *);

};

Data::Data()

{

address = " ";

mobile_no = new long;

dl_no = new long;

}

Data::~Data()

{

delete mobile_no;

delete dl_no;

}

void Data::input_student_data()

{

cout<<"Contact Address : ";

cin.ignore();

getline(cin,address);

cout<<"Mobile Number : ";

cin>>*mobile_no;

cout<<"Driving License Number : ";

cin>>*dl_no;

}

void Data::display_student_data()

{

cout<<"Contact Address : "<<address<<endl;

cout<<"Mobile Number : "<<*mobile_no<<endl;

cout<<"Driving License Number : "<<*dl_no<<endl;

}

Student::Student()

{

Name = " ";

Roll = 0;

Class = " ";

Div = new char;

dob = "dd/mm/yyyy";

bg = new char[5];

}

Student::~Student()

{

delete Div;

delete[] bg;

}

inline void Student::input_data(Data *stud1)

{

cout<<"Name : ";

cin.ignore();

getline(cin,Name);

cout<<"Roll Number : ";

cin>>Roll;

cout<<"Class : ";

cin.ignore();

getline(cin,Class);

cout<<"Division : ";

cin>>Div;

cout<<"Date of Birth : ";

cin.ignore();

getline(cin,dob);

cout<<"Blood Group : ";

cin>>bg;

stud1->input_student_data();

count++;

}

inline void Student::display_data(Data *stud2)

{

cout<<"Name : "<<Name<<endl;

cout<<"Roll Number : "<<Roll<<endl;

cout<<"Class : "<<Class<<endl;

cout<<"Division : "<<Div<<endl;

cout<<"Date of Birth : "<<dob<<endl;

cout<<"Blood Group : "<<bg<<endl;

stud2->display_student_data();

cout<<"----------------------------------------------------------------------\n";

}

int Student::count;

int main()

{

Student *st1[100];

Data *st2[100];

int a;

int s=0;

char ch;

for(int i=0;i<2;i++)

{

cout<<"--------------------MENU--------------------\n";

cout<<"1. New Student\n";

cout<<"2. View Database\n";

cout<<"Enter your choice : "<<endl;

cin>>a;

switch (a)

{

case 1 :

{

do

{

st1[s] = new Student;

st2[s] = new Data;

cout<<"Enter the details of student "<<s+1<<" : "<<endl;

st1[s]->input_data(st2[s]);

s++;

cout<<"Do you want to create a new student (y/n): ";

cin>>ch;

}while(ch=='y' || ch=='y');

}

break;

case 2:

{

cout<<"**************************StudentDatabase****************************"<<endl;

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

{

st1[j]->display_data(st2[j]);

}

}

break;

default:

cout<<"WRONG CHOICE EXECUTE AGAIN\n";

}

}

cout<<"Total Number of Students : "<<Student::get_count()<<endl;

return 0;

}         


Output:

--------------------MENU--------------------

1. New Student

2. View Database

Enter your choice :

1

Enter the details of student 1 :

Name : kdjkfjkj

Roll Number : 12

Class : 6

Division : A

Date of Birth : 24/6/2000

Blood Group : A+

Contact Address : jhkjyug

Telephone Number : 892563215

Driving License Number : 52258656

Do you want to create a new student (y/n): y

Enter the details of student 2 :

Name : hdjhdjh

Roll Number : 21

Class : A

Division : B

Date of Birth : 27/8/2003

Blood Group : A+

Contact Address : 656853235

Telephone Number : 256656555

Driving License Number : 243546

Do you want to create a new student (y/n): n

--------------------MENU--------------------

1. New Student

2. View Database

Enter your choice :

2

**************************StudentDatabase****************************

Name : kdjkfjkj

Roll Number : 12

Class : 6

Division : A

Date of Birth : 24/6/2000

Blood Group : A+

Contact Address : jhkjyug

Telephone Number : 892563215

Driving License Number : 52258656

----------------------------------------------------------------------

Name : hdjhdjh

Roll Number : 21

Class : A

Division : B

Date of Birth : 27/8/2003

Blood Group : A+

Contact Address : 656853235

Telephone Number : 256656555

Driving License Number : 243546

----------------------------------------------------------------------

Total Number of Students : 2

 

--------------------------------

Process exited after 374.7 seconds with return value 0

Press any key to continue . .