0% found this document useful (0 votes)
207 views79 pages

Akshay Prog.

The document contains code for 4 questions related to C++ classes. Question 1 defines a Student class with name, marks, and functions to assign values, calculate total/average marks, and display data. Question 2 defines a BigCity class with name, address, pincode, phone number and functions to accept and print data. Question 3 defines nested classes Car and Record to store car details. Question 4 creates a binary file containing Laugh objects and modifies a record.

Uploaded by

Sangeeta Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
207 views79 pages

Akshay Prog.

The document contains code for 4 questions related to C++ classes. Question 1 defines a Student class with name, marks, and functions to assign values, calculate total/average marks, and display data. Question 2 defines a BigCity class with name, address, pincode, phone number and functions to accept and print data. Question 3 defines nested classes Car and Record to store car details. Question 4 creates a binary file containing Laugh objects and modifies a record.

Uploaded by

Sangeeta Yadav
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 79

/**************************** QUESTION 1 ********************************

Write a program to implement a class named STUDENT having the following


members :
1. Data members :
Name of student.
Marks in five subject.
Average marks. Mk.
2. Member functions :
To assign initial values.
To compute total and average marks.
To display the data.
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class STUDENT
{
private : char name[20];
float marks[5];
float avg;
public : void init();
void compute();
void disp();
};
char sub[][10]={"Physics","Maths","Chemistry","English","C++"};
float total=0;
void STUDENT::init()
{
cout<<"\n\n Enter student name : ";
gets(name);
for(int i=0;i<5;i++)
{
cout<<"\n\n Enter students marks in "<<sub[i]<<" : ";
cin>>marks[i];
}
compute();
}
void STUDENT::compute()
{
for(int i=0;i<5;i++)
{
total+=marks[i];
}
avg=total/5;
}
void STUDENT::disp()
{
cout<<"\n\n\n\n\n Name : "<<name
<<"\n\n\n Marks : "
<<"\n\n\t Physics : "<<marks[0]
<<"\n\n\t Maths : "<<marks[1]

<<"\n\n\t
<<"\n\n\t
<<"\n\n\t
<<"\n\n\n
<<"\n\n\n

Chemistry : "<<marks[2]
English : "<<marks[3]
C++ : "<<marks[4]
Total marks : "<<total
Average marks : "<<avg;

}
void main()
{
clrscr();
STUDENT s;
s.init();
s.disp();
getch();
}
/****************** OUTPUT

1*******************

Enter sudent name : Akshay


Enter students marks in Physics : 100
Enter students marks in Maths : 100
Enter students marks in Chemistry : 100
Enter students marks in English : 100
Enter students marks in C++ : 100
Name : Akshay
Marks :
Physics : 100
Maths : 100
Chemistry : 100
English : 100
C++ : 100
Total marks : 500
Average marks : 100
************************************************************************/

/******************************** QUESTION 2*****************************


Create a class of big cities BIG CITY of india,the data membesr of the class are name
of the city,std code,etc.Write a member function that interactively ask the name and
address,local phone number of residents and print in the following format :
1. Name : S.P.Sharma
2. Address : AA,Pitam Pura
3. Pin code : 110035
4. Phone No. : (011)-7311786
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class BIGCITY
{
private : char name[20];
char add[50];
int stdcode;
long int pincode;
long int no;
public : void accept();
void print();
};
void BIGCITY::accept()
{
pincode=0;
stdcode=0;
no=0;
cout<<"\n\n Enter your name : ";
gets(name);
cout<<"\n\n Enter your address : ";
gets(add);
cout<<"\n\n Enter your pin code : ";
cin>>pincode;
cout<<"\n\n Enter your STD code : ";
cin>>stdcode;
cout<<"\n\n Enter your landline number : ";
cin>>no;
}
void BIGCITY::print()
{
cout<<1. Name : "<<name;
cout<<\n2. Address : "<<add;
cout<<"\n3.Pin code : "<<pincode;
cout.write"\n4. Phone No. : "<<stdcode<<no;
}
void main()
{
clrscr();
BIGCITY b;
b.accept();
b.print();
getch();
}

/******************************* OUTPUT 2*******************************


ON SCREEN
Enter your name : S.P.Sharma
Enter your address : AA,Pitam Pura
Enter your pin code : 110035
Enter your STD code : 011
Enter your landline number : 7311786
ON PRINTER
1.
2.
3.
4.

Name : S.P.Sharma
Address : AA,Pitam Pura
Pin code : 110035
Phone No. : (011)-7311786

************************************************************************/

/******************************** QUESTION 3*****************************


Ques -Write a program To enter n number of records of various cars and display it
using classes.
#include<iostream.h>
#include<conio.h>
class first
{private:
char name[15];
char model[10];
public:
void getdata();
void putdata();
};
class second
{
private :
char carno[12];
int year;
public :
void getdata();
void putdata();
};
class third
{
private :
first obj1;
second
obj2;
public :
void getdata();
void putdata();
};
void first :: getdata()
{
cout<<"Name of the car : ";
cin>>name;
cout<<"Model of the car : ";
cin>>model;
}
void first :: putdata()
{
cout<<"Name of the car : "<<name<<endl;
cout<<"Model of the car : "<<model<<endl;
}
void second :: getdata()
{
cout<<"Enter car number : ";
cin>>carno;
cout<<"Year of manufacturer : ";
cin>>year;
}
void second :: putdata()
{
cout<<"Car number : "<<carno<<endl;
cout<<"Year : "<<year<<endl;
}
void third :: getdata()
{
obj1.getdata();

obj2.getdata();
}
void third :: putdata()
{
obj1.putdata();
obj2.putdata();
}
void main()
{
class third obj[10];
int n, i;
clrscr();
cout<<"How many records to enter : ";
cin>>n;
cout<<"***Enter the data***";
for(i=0; i<=n-1; i++);
{
cout<<"Record No. "<<i+1<<endl;
obj[i].getdata();
}
cout<<endl<<"***The entered data is ***";
for(i=0; i<=n-1; i++);
{
cout<<"Record No. "<<i+1<<endl;
obj[i].putdata();
}
getch();
}

OUTPUT
How many records to enter :1
***Enter the data***
Record No. 1
Name of the car :creta
Model of the car :n077
Enter car number :6674
Year of manufacturer :2015
***The entered data is***
Record no. 1
Name of the car :creta

Model of the car :n077


Car number :6674
Year :2015

/**************************** QUESTION 4 ********************************


Ques -Define the following class :
Class laughter
{ int idno;
char type[5];
char desc[255];
public :
laughter( );
void newentry( );
void showentry( );
~laughter( );
};
Write a program to create binary file LAUGHTER.DAT that contains objects of the given
class and modifies a particular record for a given idno.
(NOTE : Add any member function(s) in the class as required in the program.)
#include<fstream.h>
#include<conio.h>
# include <stdio.h>
# include <string.h>
class laughter
{
int idno;
char type[5];
char desc[255];
public :
laughter()
{
idno = 0;
strcpy(type,"ABC");
strcpy(desc,"TEMP");
}
void newentry()
{
cout<<"Enter the id number : ";
cin>>idno;
cout<<"Enter the type : ";
gets(type);
cout<<"Enter the description : ";
cin>>desc;
}
void showentry()
{
cout<<"\n ID NO : "<<idno;
cout<<" TYPE : "<<type;
cout<<" DESCRIPTION : "<<desc;
}
void modientry()
{
cout<<"Enter the type : ";
gets(type);
cout<<"Enter the description : ";
cin>>desc;
}
int retidno()
{
return idno;
}
~laughter()
{
}
};
void create()
{
laughter l;
char ans;
ofstream fout;

fout.open("LAUGHTER.DAT");
do
{
l.newentry();
fout.write((char *) &l, sizeof(l));
cout<<"Do you want to continue ?(Y/N) : ";
cin>>ans;
} while ((ans == 'y')||(ans == 'Y'));
fout.close();
}
void display()
{
laughter l;
ifstream fin;
fin.open("LAUGHTER.DAT");
cout<<"The details are :-\n";
while (!(fin.eof()))
{
fin.read((char *)&l, sizeof(l));
l.showentry();
}
fin.close();
}
void modify()
{
ifstream fin;
fin.open("LAUGHTER.DAT");
ofstream fout;
fout.open("TEMP.DAT");
laughter l;
int x, t;
cout<<"Enter the id no to be changed : ";
cin>>x;
while (!(fin.eof()))
{
fin.read((char *) &l, sizeof(l));
t = l.retidno();
if (t == x)
{
l.showentry();
l.modientry();
}
fout.write((char *)&l, sizeof(l));
}
fin.close();
fout.close();
remove("LAUGHTER.DAT");
rename("TEMP.DAT", "LAUGHTER.DAT");
}
void main()
{
clrscr();
create();
display();
cout<<"\nMODIFICATION \n";
modify();
cout<<"The new file is :\n";
display();
getch();
}

Output
Enter the id number : 1
Enter the type : 1
Enter the description :
Do you want to continue
Enter the id number : 2
Enter the type : 2
Enter the description :
Do you want to continue
The details are :ID NO : 1 TYPE
ID NO : 2 TYPE
ID NO : 2 TYPE
MODIFICATION
Enter the id no

1
?(Y/N) : y
2
?(Y/N) : n

: 1 DESCRIPTION : 1
: 2 DESCRIPTION : 2
: 2 DESCRIPTION : 2
to be changed : 1

ID NO : 1 TYPE : 1 DESCRIPTION : 1Enter the type : 11


Enter the description : 11
The new file is :
The details are :ID
ID
ID
ID

NO
NO
NO
NO

:
:
:
:

1
2
2
2

TYPE
TYPE
TYPE
TYPE

:
:
:
:

11 DESCRIPTION : 11
2 DESCRIPTION : 2
2 DESCRIPTION : 2
2 DESCRIPTION : 2

/**************************** QUESTION 8 ********************************

Ques-Define the following class :


Class TOUR
{ int tcode;
int noofpassengers;
int fare;
public :
TOUR( );
void input( );
void output( );
~TOUR( );
};
Write a program to create binary file TOUR.DAT that contains objects of the given
class and deletes a particular record for a given tcode.
(NOTE : Add any member function(s) in the class as required in the program.)

# include <fstream.h>
# include <conio.h>
# include <stdio.h>
class tour
{
int tcode;
int noofpassengers;
int fare;
public :
tour()
{
tcode = 0;
noofpassengers = -1;
fare = -1;
}
void input()
{
cout<<"\nEnter the details :-\n";
cout<<"TCODE = ";
cin>>tcode;
cout<<"NO OF PASSENGERS = ";
cin>>noofpassengers;
cout<<"FARE = ";
cin>>fare;
}
void output()
{
cout<<"\n TCODE = "<<tcode;
cout<<" NO OF PASSENGERS = "<<noofpassengers;
cout<<" FARE = "<<fare<<endl;
}
int retcode()
{
return tcode;
}
~tour()
{
}
};
void create()
{
ofstream fout;
fout.open("TOUR.DAT");
tour t1;

char ans;
do
{
t1.input();
fout.write((char *)&t1, sizeof(t1));
cout<<"Do you want to continue ?(Y/N) : ";
cin>>ans;
} while ((ans == 'y') || (ans == 'Y'));
fout.close();
}
void delete1()
{
ifstream fin;
fin.open("TOUR.DAT");
ofstream fout;
fout.open("TEMP.DAT");
tour t1;
int code, c;
cout<<"\nEnter the code to be deleted :\n";
cin>>code;
while (!(fin.eof()))
{
fin.read((char *)&t1, sizeof(t1));
c = t1.retcode();
if (code != c)
fout.write((char *)&t1, sizeof(t1));
}
fin.close();
fout.close();
remove("TOUR.DAT");
rename("TEMP.DAT", "TOUR.DAT");
}
void display()
{
ifstream fin;
fin.open("TOUR.DAT");
tour t1;
cout<<"The file is :-\n";
while (!(fin.eof()))
{
fin.read((char *)&t1, sizeof(t1));
t1.output();
}
fin.close();
}
void main()
{
clrscr();
create();
display();
cout<<"\nDELETING\n";
delete1();
display(); getch();}

Output 8Enter the details :TCODE = 1


NO OF PASSENGERS = 1
FARE = 1
Do you want to continue ?(Y/N) : y
Enter the details :TCODE = 2
NO OF PASSENGERS = 2
FARE = 2
Do you want to continue ?(Y/N) : y
Enter the details :TCODE = 3
NO OF PASSENGERS = 3
FARE = 3
Do you want to continue ?(Y/N) : n
The file is :TCODE = 1 NO OF PASSENGERS = 1 FARE = 1
TCODE = 2 NO OF PASSENGERS = 2 FARE = 2
TCODE = 3 NO OF PASSENGERS = 3 FARE = 3
TCODE = 3 NO OF PASSENGERS = 3 FARE = 3
DELETING
Enter the code to be deleted :
2
The file is :TCODE = 1 NO OF PASSENGERS = 1 FARE = 1
TCODE = 3 NO OF PASSENGERS = 3 FARE = 3
TCODE = 3 NO OF PASSENGERS = 3 FARE = 3
TCODE = 3 NO OF PASSENGERS = 3 FARE = 3

/************************** QUESTION 9 **********************************


Write a program which uses overloaded prototypes of the function
large. Write 2 versions of it :
To find the Largest of the three integers.
To find the Largest of the two strings.
***********************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>

void large(int a,int b,int c);


void large(char a[],char b[]);
void main()
{
clrscr();
int a,b,c;
char s1[50],s2[60];
cout<<"\n\n Enter first number : ";
cin>>a;
cout<<"\n\n Enter second number : ";
cin>>b;
cout<<"\n\n Enter third number : ";
cin>>c;
cout<<"\n\n Enter first string : ";
gets(s1);
cout<<"\n\n Enter second string : ";
gets(s2);
large(a,b,c);
large(s1,s2);
getch();
}
void large(int a,int b,int c)
{
if(a>b&&a>c)
cout<<"\n\n The largest integer is : "<<a;
else
{
if(b>a&&b>c)
cout<<"\n\n The largest number is : "<<b;
else
cout<<"\n\n The largest number is : "<<c;
}
}
void large(char s1[],char s2[])
{
int a,b;
a=strlen(s1);
b=strlen(s2);

cout<<"\n\n The largest string is : ";


if(a>b)
puts(s1);
else
puts(s2);
}

/***************************** OUTPUT 9***************************


Enter first number : 25
Enter second number : 26
Enter third number : 36
Enter first string : computer
Enter second string: programme
The largest number is : 36
The largest string is : programme
************************************************************************/

/**************************** QUESTION 5 ********************************


Ques-Declare a class student with the following specifications:
Class name
student
Data members (private)
int rollno
- roll number
char name[20]
- name
float marks[5]
- marks in 5 subjects
float Total
- total marks
float Grade
- grade according to the average marks
Member functions (public)
void input()
- to input the data
void Calcgrade()
- to calculate total, average and
grade
void Dispdata()
- to display the data.
Grades are calculated according to the following rates:
Average
Grade
>=75
A
60-74
B
40-59
C
<40
fail
Take an array of objects of size 10 of the given class and input, calculate and
display the values.
# include <iostream.h>
# include <conio.h>
# include <stdio.h>
class student
{
int rollno;
char name[20];
float marks[5];
float total;
char grade;
public :
void input();
void calcgrade();
void display();
};
void student::input()
{
cout<<"Enter the details of the student:-\n";
cout<<"Roll No : ";
cin>>rollno;
cout<<"Name : ";
gets(name);
cout<<"Marks in 5 subjects:";
for (int i=0 ; i<5 ; i++)
cin>>marks[i];
}
void student::calcgrade()
{
int sum = 0;
for (int i=0 ; i<5 ; i++)
sum = sum + marks[i];
total = sum / 5.0;
if (total >= 75)
grade = 'A';
else if (total >= 60)
grade = 'B';
else if (total >= 40)

grade = 'C';
else
grade = 'f';
}
void student::display()
{
cout<<"\n The details are :-\n";
cout<<"\nRoll No : "<<rollno;
cout<<"\nName : "<<name;
cout<<"\nTotal Marks : "<<total;
cout<<"\nGrade : "<<grade;
}
void main()
{
student s[10];
int n;
clrscr();
cout<<"\n Enter the number of stduents : ";
cin>>n;
for (int i=0 ; i<n ; i++)
{
s[i].input();
s[i].calcgrade();
s[i].display();
}
getch();
}
OutputEnter the number of stduents : 2
Enter the details of the student:Roll No : 12
Name : Rajni
Marks in 5 subjects:34 56 78 21 98
The details are :Roll No : 12
Name : Rajni
Total Marks : 57.400002
Grade : CEnter the details of the student:Roll No : 16
Name : Rahul
Marks in 5 subjects:76
12 98 56 34
The details are :Roll No : 16
Name : Rahul
Total Marks : 55.200001
Grade : C

/**************************** QUESTION 6 ********************************


Ques - Define a class BOOK containing following details :
Private : access no, author name, title, publishers name, price, no of copies
Public member functions :
(i)
To read the details
(ii) To display the details
(iii) A constructor and a destructor
WAP to store details of all books in a library and display only those books whose
price is greater any given value.
(NOTE : Add any member function(s) in the class as required in the program.)
// Program for books thru class

#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
#include<string.h>
class book
{int acc_no;
//access number of the book
char auth_name[50];
//name of the auther of the book
char title[50];
char pub_name[50];
//name of the publisher of the book
float price;
int no_copy;
//no of copies of the book in the library
public:
void read()
//to read the details of the book
{cout<<"\n enter the access no of the book:\n";
cin>>acc_no;
cout<<"\n enter auther name of the book:\n";
gets(auth_name);
cout<<"\n enter the title of the book:\n";
gets(title);
cout<<"\n enter publisher's name:\n";
gets(pub_name);
cout<<"\n price of the book:\n";
cin>>price;
cout<<"\n enter no of copies of book:\n";
cin>>no_copy;
};
void display()
//to display the details of the book
{cout<<"\n access no of the book:\n";
cout<<acc_no;
cout<<"\n name of th eauther:\n";
puts(auth_name);
cout<<"\n title of the book:\n";
puts(title);
cout<<"\n name of the publisher:\n";
puts(pub_name);
cout<<"\n price of the book:\n";
cout<<price;
cout<<"\n no of copies of the book:\n";

cout<<no_copy;
};
int ret_price()
//to return the price of the book
{ return price;};
book()
{acc_no=0;
strcpy(auth_name,"a");
strcpy(title,"b");
strcpy(pub_name,"c");
price=330;
no_copy=12;
};
~book()
{cout<<"destroying";
};
};
void main()
{clrscr();
book b[100];int i,n,p;
cout<<"\n enter the no of books present in the library:\n";
cin>>n;
for(i=0;i<n;i++)
b[i].read();
cout<<"\n enter the price limit:\n";
cin>>p;
for(i=0;i<n;i++)
if(b[i].ret_price()>=p)
b[i].display();
getch();
};

OUTPUT
Enter the number of books in the library : 3
Enter the details of the book
Access Number : 1
Author Name : a
Publisher : a
Title : a
Price : 12
Copies : 3
Enter the details of the book
Access Number : 2
Author Name : b
Publisher : b
Title : b
Price : 45
Copies : 5
Enter the details of the book
Access Number : 3
Author Name : c
Publisher : c
Title : c
Price : 10
Copies : 2
Enter the price limit : 15
The details of the book are
Access Number : 2
Author Name : b
Publisher : b
Title : b
Price : 45
Copies : 5

/******************************** QUESTION 7*****************************


Ques- Write a Simple Class Example Program For Find Prime Number In C++
#include<iostream.h>
#include<conio.h>
// Class Declaration
class prime
{
//Member Varibale Declaration
int a,k,i;
public:
prime(int x)
{
a=x;
}
// Object Creation For Class
void calculate()
{
k=1;
{
for(i=2;i<=a/2;i++)
if(a%i==0)
{
k=0;
break;
}
else
{
k=1;
}
}
}
void show()
{
if(k==1)
cout<<"\n"<<a<<" is Prime Number.";
else
cout<<"\n"<<a<<" is Not Prime Numbers.";
}
};
//Main Function
int main()
{
int a;
cout<<"Enter the Number:";
cin>>a;
// Object Creation For Class
prime obj(a);
// Call Member Functions
obj.calculate();
obj.show();
getch(); return 0;}

Output:

Enter the Number:10


10 is Not Prime Numbers.
Enter the Number:7
7 is Prime Number.

/******************************** QUESTION 17 *****************************


Ques- Simple Example Program For Constructor In C++
#include<iostream>
#include<conio.h>
using namespace std;
class Example
{
// Variable Declaration
int a,b;
public:
//Constructor
Example()
{
// Assign Values In Constructor
a=10;
b=20;
cout<<"Im Constructor\n";
}
void Display()
{
cout<<"Values :"<<a<<"\t"<<b;
}
};
int main()
{
Example Object;
// Constructor invoked.
Object.Display();
// Wait For Output Screen
getch();
return 0;
}

Sample Output
Im Constructor
Values :10

20

/******************************** QUESTION 10*****************************


Write a program to accept personal details,sports details and educational details of
the candidate and find out,if he qualifies for the selection or not.
The program should have following specifications :
First class named personal should have roll no.,name and age as data members
and two functions to accept and display data.
Second class named sports should have sports name and grade as data members and
two functions to accept and display data.The function that accepts data should return
grade.This class should inherit from personal.
Third class named education should have subject and marks as data
members and two functions to accept and display data.The function that accepts
data should return marks.This class should inherit from sports.
Fourth class should inherit education and should retutn either 's' (for
selected candidates) or 'n' dependin on the following criteria :
1. If grade=a and marks>60 return s.
2. If grade=b and marks>75 return s.
3. If grade=c and marks>90 return s.
4. In other conditions return n.
main() program should make use of above mentioned classes and display the data
and final result.
***********************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
class personal
{
private : int rno;
char name[20];
int age;
public : void paccept();
void pdisplay();
};
void personal::paccept()
{
cout<<"\n\n Enter Roll No. : ";
cin>>rno;
cout<<"\n\n Enter Name : ";
gets(name);
cout<<"\n\n Enter Age : ";
cin>>age;
}
void personal::pdisplay()
{
cout<<"\n\n Roll No. : "<<rno
<<"\n\n Name : "<<name
<<"\n\n Age : "<<age;
}
class sports:public personal

{
private : char sname[20];
char grade;
public : char saccept();
void sdisplay();
};
char sports::saccept()
{
cout<<"\n\n Enter Sports Name : ";
gets(sname);
cout<<"\n\n Enter Grade : ";
cin>>grade;
return grade;
}
void sports::sdisplay()
{
cout<<"\n\n Sports Name : "<<sname
<<"\n\n Grade : "<<grade;
}
class education:public sports
{
private : char sub[30];
float marks;
public : float eaccept();
void edisplay();
};
float education::eaccept()
{
cout<<"\n\n Enter Subject : ";
gets(sub);
cout<<"\n\n Enter Marks : ";
cin>>marks;
return marks;
}
void education::edisplay()
{
cout<<"\n\n Subject : "<<sub
<<"\n\n Marks : "<<marks;
}
class result:public education
{
private : char grade;
float marks;
public

: result();
char calculate();
void rdisplay();

};
result::result()

{
personal::paccept();
grade=sports::saccept();
marks=education::eaccept();
}

char result::calculate()
{
if(grade=='a'&&marks>60)
return 's';
else
if(grade=='b'&&marks>75)
return 's';
else
if(grade=='c'&&marks>90)
return 's';
else
return 'n';
}
void result::rdisplay()
{
char r;
r=calculate();
personal::pdisplay();
sports::sdisplay();
education::edisplay();
if(r=='s')
cout<<"\n\n Status : Selected";
else
cout<<"\n\n Status : Rejected";
}
void main()
{
clrscr();
result r;
cout<<"\n\n\n\t\t\t\t RESULT";
r.rdisplay();
getch();
}

/***************************** OUTPUT 10 ****************************


Enter Roll No. : 5
Enter
Enter
Enter
Enter
Enter
Enter

Name : akshay
Age : 25
Sports Name : Basket Ball
Grade : b
Subject : Physical Education
Marks : 80
RESULT
Roll No. : 5
Name : akshay
Age : 25
Sports Name : Basket Ball
Grade : b
Subject : Physical Education
Marks : 80
Status : Selected
************************************************************************/

/******************************** QUESTION 11*****************************


Ques -Program to define the classes PERSON, GAME and STUDENT & to access the
essential data using multiple inheritance.
/***********************************************************************
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
class person
{ char name[21];
int age;
public:
void indata()
{cout<<"\n\nEnter the name of Student: " ;
gets(name);
cout<<"\n\nEnter the age : ";
cin>>age;
}
void outdata();
};
void person::outdata()
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
}
class game {
char game_name[20];
public:
void input()
{
cout<<"\n\nEnter the game name : ";
gets(game_name);
}
void output()
{
cout<<"\n\nGame opted by the student is : "<<game_name;
}
};
class student: public person, public game
{ float Tmarks;
int rollno;
public:
char calgrade()
{if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)

return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
else
return 'E';
}
void enter()
{
indata();
cout<<"\n\nEnter the roll number: "; cin>>rollno;
input();
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{
outdata();
cout<<"\n\nRoll number : "<<rollno;
output();
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{ clrscr();
student A;
A.enter();
A.display();
getch();
}

OUTPUT
Enter
Enter
Enter
Enter
Enter

the name of Student: Akshay


the age : 17
the roll number: 34
the game name : Tennis
total marks (out of 100) : 95

Name of the student is: Akshay


Age of the student is : 17
Roll number : 34
Game opted by the student is : Tennis
Total marks are : 95
Grade = A

/*******************************QUESTION 12 *****************************
Ques-Display the information of a student and calculate his grade using the class
inheritance
***********************************************************************/
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
Class person{ char name[21]; int age;
public:
void indata()
{
cout<<"\n\nEnter the name of Student: " ;
gets(name);
cout<<"\n\nEnter the age : ";
cin>>age;
}
void outdata()
{
cout<<"\n\n";
for(int i=0; i<79; i++)
cout<<"-";
cout<<"\n\nName of the student is: "<<name;
cout<<"\n\nAge of the student is : "<<age;
}
};
class student: public person
{ float Tmarks;
int rollno;
public:
char calgrade()
{if(Tmarks>90)
return 'A';
else if(Tmarks>80&&Tmarks<=90)
return 'B';
else if(Tmarks>70&&Tmarks<=80)
return 'C';
else if(Tmarks>60&&Tmarks<=70)
return 'D';
else
return 'E';
}
void enter()
{
cout<<"\n\nEnter the roll number: "; cin>>rollno;
cout<<"\n\nEnter total marks (out of 100) : ";
cin>>Tmarks;
}
void display()
{ cout<<"\n\nRoll number : "<<rollno;
cout<<"\n\nTotal marks are : "<<Tmarks;
cout<<"\n\nGrade = "<<calgrade();
}
};
void main()
{ clrscr();
student A;
A.indata(); A.enter(); A.outdata(); A.display();getch();}
OUTPUT

Enter the name of Student: Akshay


Enter the age : 16
Enter the roll number: 34
Enter total marks (out of 100) : 92
------------------------------------------------------------------------------Name of the student is: Akshay
Age of the student is : 16
Roll number : 34
Total marks are : 92
Grade = A

/************************ QUESTION 13 ***********************************


Write a program to read an already existing text file and print the following
information :
a)Number of words
b)Number of three letter words
************************************************************************/
#include<fstream.h>
#include<conio.h>
#include<process.h>
#include<stdio.h>
#include<string.h>
void main()
{
clrscr();
ifstream fin("AAA.TXT");
char ch[20];
int n=0,l3=0,l;
if(!fin)
{
cout<<"\n\n File doesn't exists ";
exit(1);
}
while(!fin.eof())
{
fin>>ch;
n+=1;
l=strlen(ch);
if(l==3)
l3+=1;
}
cout<<"\n\n Total number of words : "<<n;
cout<<"\n\n Total number of 3 letter words : "<<l3;
getch();
}
/************************OUTPUT 13 *************************************
Total number of words : 29001
Total number of 3 letter words : 782
**********************************************************************/

/************************** QUESTION 14**********************************


Write a program to accept a string and write into text file in toggle case i.e. lower
case characters to upper case and vice versa ?
***********************************************************************/
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<ctype.h>
void main()
{
clrscr();
char str[100],ch;
ofstream fout;
fout.open("toggle.txt",ios::app|ios::out);
cout<<"\n\n Enter the string to be entered in file : ";
gets(str);
for(int i=0;str[i]!='\0';i++)
{
if(isupper(str[i]))
{
str[i]=tolower(str[i]);
fout<<str[i];
}
else
{
if(islower(str[i]))
{
str[i]=toupper(str[i]);
fout<<str[i];
}
else
continue;
}
}
fout.close();
ifstream fin("toggle.txt",ios::in);
cout<<"\n\n\n The data stored in file is : ";
while(fin.get(ch))
{
cout<<ch;
}
fin.close();
getch();
}
/********************************* OUTPUT 14*****************************
Enter the string to be entered in file : I study in M.A.M.S
The data stored in file is : i STUDY IN m.a.m.s
***********************************************************************/

/******************************* QUESTION 15*****************************


Write a program to read an already existing text file and print the
following information :
a. Number of words.
b. Number of characters.
c. Number of lines.
************************************************************************/
#include<fstream.h>
#include<conio.h>
#include<process.h>
void main()
{
clrscr();
ifstream fin("AAA.TXT");
char ch[100];
double l=0,w=0,c=0;
while(!fin.eof())
{
fin.getline(ch,100);
l++;
for(int i=0;ch[i]!='\n';i++)
{
c++;
if(ch[i]==' ')
w++;
}
}
w++;
cout<<"\n\n Total number of words : "<<w;
cout<<"\n\n Total number of characters : "<<c;
cout<<"\n\n Total number of lines : "<<l;
getch();
}
/*************************** OUTPUT 15*************************
Total number of words : 290
Total number of characters : 5000
Total number of lines : 51
***********************************************************************/

/******************************** QUESTION 36****************************


Write an interactive C++ program to read a text file and display the
following :
Frequency table of all the alphabetic characters.
Number of numeric characters present in the file.
************************************************************************/
#include<fstream.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
char
uc[26]={'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','
T','U','V','W','X','Y','Z'};
char
lc[26]={'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','
t','u','v','w','x','y','z'};
int n=0,u[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
int l[26]={0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void upper(char ch)
{
for(int i=0;i<26;i++)
{
if(ch==uc[i])
{
u[i]++;
break;
}
}
}
void lower(char ch)
{
for(int i=0;i<26;i++)
{
if(ch==lc[i])
{
l[i]++;
break;
}
}
}
void display()
{
cout<<"\n\n Total number of numeric data is : "<<n;
getch();
for(int i=0;i<26;i++)
{
cout<<"\n\n No. of character "<<uc[i]<<" : "<<u[i];
getch();
}
for(i=0;i<26;i++)
{
cout<<"\n\n No. of character "<<lc[i]<<" : "<<l[i];
getch();
} }
void main()
{
clrscr();
ifstream fin("ABC.CPP");
char ch;
while(!fin.eof())
{
fin>>ch;
if(isdigit(ch))
n++;

else
if(isupper(ch))
upper(ch);
else
lower(ch);
display();

}
/******************** OUTPUT 36(Sample for file ABC.CPP)*****************
Total number of numeric data is : 37
No. of character A : 2
No. of character B : 0
No. of character C : 0
No. of character D : 1
No. of character E : 2
No. of character F : 1
No. of character G : 0
No. of character H : 0
No. of character I : 1
No. of character J : 0
No. of character K : 0
No. of character L : 1
No. of character M : 1
No. of character N : 1
No. of character O : 0
No. of character P : 0
No. of character Q : 0
No. of character R : 1
No. of character S : 0
No. of character T : 0
No. of character U : 0
No. of character V : 0
No. of character W : 0
No. of character X : 1
No. of character Y : 0
No. of character Z : 0
No. of character a : 17
No. of character b : 11
No. of character c : 12
No. of character d : 10
No. of character e : 27
No. of character f : 32
No. of character g : 3
No. of character h : 8
No. of character i : 60
No. of character j : 4
No. of character k : 7
No. of character l : 26
No. of character m : 4
No. of character n : 27
No. of character o : 21
No. of character p : 14
No. of character q : 0
No. of character r : 15
No. of character s : 21
No. of character t : 30
No. of character u : 8
No. of character v : 5
No. of character w : 7
No. of character x : 1 No. of character y : 4 No. of character z : 4

/**************************** QUESTION 18********************************


Create a menu driven program to implement the following operations in a binary file
for the class student with the field names roll number,name,marks in five subjects
and average marks.The class also have related functions to display data,accept data
and calculate average.The operation to implement are :
Addition of records.
Insertion in a sorted file.
Deletion in a sortd file.
Modification of records.
Searching records based on a field.
View all records.
***********************************************************************/
#include<fstream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<process.h>
char sub[][10]={"Physics","Chemistry","Maths","English","C++"};
class student
{
private : int rno;
char name[20];
float marks[5];
float avg;
public : void accept();
void display();
void modify();
int retrno()
{
return rno;
}
};
void student::accept()
{
cout<<"\n\n Enter Roll No. : ";
cin>>rno;
cout<<"\n\n Enter Name : ";
gets(name);
for(int i=0;i<5;i++)
{
cout<<"\n\n Enter marks in "<<sub[i]<<" : ";
cin>>marks[i];
}
}
void student::display()
{
float t=0;
cout<<"\n\n\t Roll No. : "<<rno;
cout<<"\n\n\t Name : "<<name;
cout<<"\n\n\t Marks : ";
for(int i=0;i<5;i++)
{
cout<<"\n\t\t "<<sub[i]<<" : "<<marks[i];
t+=marks[i];
}
avg=t/5;
cout<<"\n\n\t Average marks : "<<avg;
}
void disp()
{
ifstream fin("add.dat",ios::binary);
student s;

int a=1;
if(fin.eof()==1)
{
cout<<"\n\n No Data in the file";
return;
}
while(fin.read((char *)&s,sizeof(student)))
{
cout<<"\n\n Record : "<<a;
s.display();
cout<<"\n\n\n";
++a;
getch();
}
}
void student::modify()
{
fstream f("add.dat",ios::binary|ios::in|ios::out);
int r,n;
char s[20];
float m;
long int pos=0;
cout<<"\n\n Enter the Roll No. whose name is to be modified : ";
cin>>r;
while(!f.eof())
{
pos=f.tellg();
f.read((char *)this,sizeof(student));
if(r==rno)
{
cout<<"\n\n Enter the subject in which marks are to be changed : ";
gets(s);
for(int i=0;i<5;i++)
{
if(strcmpi(s,sub[i])==0)
{
n=i;
break;
}
}
cout<<"\n\n Enter the new Marks : ";
cin>>m;
marks[n]=m;
f.seekp(pos);
f.write((char *)this,sizeof(student));
break;
}
else
{
cout<<"\n\n The record is not found ";
}
}
disp();
}
void add()
{
ofstream fout("add.dat",ios::binary|ios::out|ios::app);
student s;
char ch='y';
while(ch=='y'||ch=='Y')
{
s.accept();
fout.write((char *)&s,sizeof(student));

cout<<"\n\n Do you want to add more data (y or n) : ";


cin>>ch;
}
disp();
}
void insert()
{
ifstream fin("add.dat",ios::binary|ios::in);
ofstream fout("t.dat",ios::binary|ios::out|ios::app);
student s1,s2;
char ch='n';
cout<<"\n\n Enter the record to be added\n\n";
s2.accept();
while(!fin.eof())
{
fin.read((char *)&s1,sizeof(student));
if(s1.retrno()>=s2.retrno())
{
fout.write((char *)&s2,sizeof(student));
ch='y';
break;
}
else
fout.write((char *)&s1,sizeof(student));
}
if(ch=='n')
fout.write((char *)&s2,sizeof(student));
else
if(!fin.eof())
{
while(fin.read((char *)&s1,sizeof(student)))
{
fout.write((char *)&s1,sizeof(student));
}
}
fin.close();
fout.close();
remove("add.dat");
rename("t.dat","add.dat");
disp();
}
void search()
{
ifstream fin("add.dat",ios::binary);
student s;
int no,i=1;
cout<<"\n\n Enter the Roll No. to be searched : ";
cin>>no;
while(fin.read((char *)&s,sizeof(student)))
{
if(s.retrno()==no)
{
cout<<"\n\n The Record found at position : "<<i;
break;
}
i++;
}
disp();
}

void deleter()
{
ifstream fin("add.dat",ios::binary);
ofstream fout("temp.dat",ios::binary|ios::out|ios::app);
student s;
int no;
cout<<"\n\n Enter the record's Roll No. which is to be deleted : ";
cin>>no;
while(fin.read((char *)&s,sizeof(student)))
{
if(no==s.retrno())
continue;
else
fout.write((char *)&s,sizeof(student));
}
remove("add.dat");
rename("temp.dat","add.dat");
disp();
}
void main()
{
student s;
char ch='y';
int p;
do
{
clrscr();
cout<<"\n\n 1. Add record"
<<"\n\n 2. Insert record"
<<"\n\n 3. Delete record"
<<"\n\n 4. Modify record"
<<"\n\n 5. Search record"
<<"\n\n 6. View records"
<<"\n\n 7. Exit(0)";
cout<<"\n\n Enter your choice (1 - 7) : ";
cin>>p;
clrscr();
switch(p)
{
case 1 : add();
break;
case 2 : insert();
break;
case 3 : deleter();
break;
case 4 : s.modify();
break;
case 5 : search();
break;
case 6 : disp();
break;
case 7 : exit(0);
}
cout<<"\n\n Do you want to continue : ";
cin>>ch;
}while(ch=='y'||ch=='Y');
getch();
}

/********************************* OUTPUT 18*************************


1. Add record
2. Insert record
3. Delete record
4. Modify record
5. Search record
6. View records
7. Exit(0)
Enter your choice (1 - 7) : 6
Roll No. : 1
Name : AMIT
Marks :
Physics
78
Chemistry 70
Maths
90
English
80
C++
95
Average marks : 82.5
***********************************************************************/

/*************************** QUESTION 19 ********************************


Ques-Write a program to create the given menu for the following operations on a text
file STORY.TXT.
1. Display the contents of text file on monitor
2. To count no of this word.
3. To count no of words starting with R
Exit

# include <fstream.h>
# include <stdio.h>
# include <conio.h>
# include <string.h>
void create()
{
char x;
ofstream fout;
fout.open("story.txt");
cout<<"Enter the contents of text file. Press * to end.\n";
while (x != '*')
{
x = getche();
fout<<x;
}
fout.close();
}
void display()
{
char x;
ifstream fin;
fin.open("story.txt");
cout<<"The file is as follows :-\n";
while (!(fin.eof()))
{
fin.get(x);
cout<<x;
}
fin.close();
}
void count_this()
{
char x[25];
int count = 0;
ifstream fin;
fin.open("story.txt");
while (!(fin.eof()))
{
fin>>x;
if (strcmp(x,"this") == 0)
count++;
}
cout<<"The total no of this are : "<<count;
fin.close();
}
void count_R()
{
char x[25];
int count = 0;
ifstream fin;
fin.open("story.txt");
while (!(fin.eof()))
{
fin>>x;
if (x[0] == 'R')
count++;

}
cout<<"The total no of words of R are : "<<count;
fin.close();
}
void main()
{
int choice;
clrscr();
create();
do
{
cout<<"\n\n\nMENU OPTION FOR TEXT FILE\n";
cout<<"1. Display the text file STORY.TXT\n";
cout<<"2. To count no. of this.\n";
cout<<"3. To count no. of words of R.\n";
cout<<"4. Exit.\n";
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1 : display();
break;
case 2 : count_this();
break;
case 3 : count_R();
break;
case 4 : cout<<"End of program.\n";
break;
default : cout<<"Wrong choice!!!\n";
}
}while (choice != 4);
getch();
}

Output
Enter the contents of text file. Press * to end.
MENU OPTION FOR TEXT FILE
1. Display the text file STORY.TXT
2. To count no. of this.
3. To count no. of words of R.
4. Exit.
Enter your choice : 1
MENU OPTION FOR TEXT FILE
1. Display the text file STORY.TXT
2. To count no. of this.
3. To count no. of words of R.
4. Exit.
Enter your choice : 2
The total no of this are : 0

/*********************************QUESTION 20 **************************
C++ Program To Add Two Matrix Using Arrays

#include <iostream.h>
int main(){
int r,c,a[100][100],b[100][100],sum[100][100],i,j;
cout << "Enter number of rows (between 1 and 100): ";
cin >> r;
cout << "Enter number of columns (between 1 and 100): ";
cin >> c;
cout << endl << "Enter elements of 1st matrix: " << endl;
/* Storing elements of first matrix entered by user. */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element a" << i+1 << j+1 << " : ";
cin >> a[i][j];
}
/* Storing elements of second matrix entered by user. */
cout << endl << "Enter elements of 2nd matrix: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << "Enter element b" << i+1 << j+1 << " : ";
cin >> b[i][j];
}
/*Adding Two matrices */
for(i=0;i<r;++i)
for(j=0;j<c;++j)
sum[i][j]=a[i][j]+b[i][j];
/* Displaying the resultant sum matrix. */
cout << endl << "Sum of two matrix is: " << endl;
for(i=0;i<r;++i)
for(j=0;j<c;++j)
{
cout << sum[i][j] << " ";
if(j==c-1)
cout << endl;
}
return 0;
}
Output
Enter number of rows (between 1 and 100): 2
Enter number of columns (between 1 and 100): 2

Enter
Enter
Enter
Enter
Enter

elements of 1st matrix:


element a11: -4
element a12: 5
element a21: 6
element a22: 8

Enter
Enter
Enter
Enter
Enter

elements of 2nd matrix:


element b11: 3
element b12: -9
element b21: 7
element b22: 2

Sum of two matrix is:


-1
-4
13
6

/*************************** QUESTION 35 ********************************


Ques -Write a menu driven program for the following :
1. To search a given element in a 1-D array of integers using binary search.
2. To insert a given element in a 1-D array at a given position.
3. Exit
# include <iostream.h>
# include <conio.h>
# include <fstream.h>
int a[100], n;
void search(int x)
{
int i, found = 0, pos;
i = 0;
while ((i < n)&&(found ==0))
{
if(a[i] == x)
{
found = 1;
pos = i;
}
i++;
}
if (found == 1)
cout<<"The number "<<x<<" is present at "<<pos;
else
cout<<"The number "<<x<<" is not present in the array.";
}
void dele_x(int x)
{
int pos = -1;
for (int i = 0;i < n;i++)
if (a[i] == x)
pos = i;
if (pos != -1)
{
i = pos;
while (i < n)
{
a[i] = a[i+1];
i++;
}
n--;
}
else
cout<<"Number "<<x<<" is not present.";
}
void display()
{
int i;
cout<<"\nThe array elements are :\n";
for (i = 0; i < n;i++)
cout<<a[i]<<"\n";
}
void main()
{
clrscr();
int choice, x;
cout<<"ENter the total number of elements : ";
cin>>n;
cout<<"Enter the elements of the array :";
for (int i = 0; i < n; i++)
cin>>a[i];
do
{
cout<<"\n\nThe menu is :";
cout<<"\n1. To search a given number.";
cout<<"\n2. To delete a given number.";

cout<<"\n3. Display the array.";


cout<<"\n4. Exit,";
cout<<"\n\nEnter your choice : ";
cin>>choice;
switch(choice)
{
case 1 : cout<<"\nEnter the number to be searched : ";
cin>>x;
search(x);
break;
case 2 : cout<<"\nEnter the number to be deleted : ";
cin>>x;
dele_x(x);
break;
case 3 : display();
break;
case 4 : cout<<"\nEnd of program.";
break;
default : cout<<"\nWrong choice.";
}
}while (choice != 4);
getch();
}

Enter the total number of elements : 5


Enter the elements of the array :
1
2
3
4
5
The menu is :
1. To search a given number.
2. To delete a given number.
3. Display the array.
4. Exit,

Enter your choice : 1


Enter the number to be searched : 2
The number 2 is present at 1
The menu is :
1. To search a given number.
2. To delete a given number.
3. Display the array.
4. Exit,
4
5
The menu is :
1. To search a given number.
2. To delete a given number.
3. Display the array.
4. Exit,
Enter your choice : 3
The array elements are :
2
3
4
5
The menu is :
1. To search a given number.
2. To delete a given number.
3. Display the array.
4. Exit,
Enter your choice : 4

/****************************Question 21 **************************
Create a program using function to accept an integer array and its size as argument
and replace elements having even values with its half and odd Values with twice its
values ?
************************************************************************/
#include<iostream.h>
#include<conio.h>
void accept(int a[],int n);
void convert(int a[],int n);
void display(int a[],int n);
void main()
{
clrscr();
int a[50],n;
cout<<"\n\n Enter the elements you want to enter : ";
cin>>n;
accept(a,n);
convert(a,n);
cout<<"\n\nThe new array is : \n\n";
display(a,n);
getch();
}
void accept(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\n\n Enter the element "<<i+1<<" : ";
cin>>a[i];
}
}
void convert(int a[],int n)
{
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
a[i]=a[i]/2;
else
a[i]=a[i]*2;
}
}
void display(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<" "<<a[i];
}
/************ OUTPUT 21 (Taking a sample array of 3) ********************
Enter the number of elements you want to enter : 3
Enter element 1 : 11
Enter element 2 : 24
Enter element 3 : 4
The new array is :
22 12 2

/**************************** QUESTION 22 *******************************


Write a program using functions to accept an integer array ant its size as arguments
and count the number of elements divisible by 3 or 5 ?
************************************************************************/
#include<iostream.h>
#include<conio.h>
void count(int a[],int n);
void main()
{
clrscr();
int *a=NULL,n;
cout<<"\n\n Enter the number of elements to be entered in array : ";
cin>>n;
a=new int[n];
for(int i=0;i<n;i++)
{
cout<<"\n\n Enter element "<<i+1<<" : ";
cin>>a[i];
}
count(a,n);
delete a;
getch();
}
void count(int a[],int n)
{
int t=0;
for(i=0;i<n;i++)
{
if(a[i]%3==0||a[i]%5==0)
{
t++;
}
}
cout<<"\n\n The total number of elements divisible by 3 or 5 are : "<<t;
}
/****************************** OUTPUT 22*******************************
Enter the number of elements to be entered in array : 5
Enter element 1 : 1
Enter element 2 : 2
Enter element 3 : 3
Enter element 4 : 4
Enter element 5 : 6
The total number of elements divisible by 3 or 5 are : 2
************************************************************************/

/*************************** QUESTION 23********************************


Write a program to implement bubble sort to an integer array input in a program.
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void bubble(int ar[],int size)
{
int i,j,t=0;
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(ar[j]>ar[j+1])
{
t=ar[j];
ar[j]=ar[j+1];
ar[j+1]=t;
}
}
}
}
void display(int a[],int size)
{
for(int i=0;i<size;i++)
{
cout<<a[i]<<" ";
} }
void main()
{
clrscr();
int a[50],size;
cout<<"\n\n Enter the number of elements to be entered in array : ";
cin>>size;
cout<<"\n\n\n Enter the elements of the array : \n";
for(int k=0;k<size;k++)
{
cout<<"\n Enter the element "<<k+1<<" : ";
cin>>a[k];
}
cout<<"\n\n\n Array before being sorted : \n\n";
display(a,size);
bubble(a,size);
cout<<"\n\n Array after sorting : \n\n";
display(a,size);
getch();
}
/****************************** OUTPUT 23*******************************
Enter the number of elements to be entered in array : 5
Enter the elements of array :
Enter the element 1 : 1
Enter the element 2 : 45
Enter the element 3 : 2
Enter the element 4 : 645
Enter the element 5 : 123
Array before being sorted :
1 45 2 645 123
Array after sorting :
1 2 45 123 645
***********************************************************************/

/************************** QUESTION 24*********************************


Write a program to implement selection sort to an integer array input in a program ?
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<iostream.h>
#include<conio.h>
void selsort(int x[],int size)
{
int index_of_min;
for(int i=0; i<size; i++)
{
index_of_min = i;
for(int y=i; y<size; y++)
{
if(x[i]>x[y])
{
index_of_min = y;
}
}
int temp = x[i];
x[i] = x[index_of_min];
x[index_of_min] = temp;
}
}
void display(int a[],int size)
{
for(int i=0;i<size;i++)
{
cout<<" "<<a[i];
} }
void main()
{
clrscr();
int size;
int *a=NULL;
cout<<"\n Enter the number of elements to be entered in array : ";
cin>>size;
a=new int[size];
cout<<"\n\n\n Enter the elements of the array : ";
for(int k=0;k<size;k++)
{
cout<<"\n\n Enter element "<<k+1<<" : ";
cin>>a[k];
}

cout<<"\n\n\n Array before sorting : \n";


display(a,size);
select(a,size);
cout<<"\n\n Array after sorting : \n";
display(a,size);
delete a;
getch();
}

/************************** OUTPUT 24 **********************************


Enter the number of elements to be entered in array : 4
Enter the elements of array :
Enter element 1 : 55
Enter element 2 : 45
Enter element 3 : 3
Enter element 4 : 452
Array before being sorted :
55 45 3 452
Arraye after sorting :
3 45 55 452
*************************************************************/

/************************ QUESTION 25 *********************************


Write a program to implement insertion sort to an integer array input in a program.
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void insertion(int a[],int size)
{
int i,j,t=0;
for(i=1;i<size;i++)
{
t=a[i];
j=i-1;
while(t<a[j]&&j>=0)
{
a[j+1]=a[j];
j--;
}
a[j+1]=t;
}
}
void display(int a[],int size)
{
for(int i=0;i<size;i++)
{
cout<<" "<<a[i];
}
}
void main()
{
clrscr();
int *a=NULL,size;
cout<<"\n Enter the number of elements to be entered in array : ";
cin>>size;
a= new int[size];
cout<<"\n\n\n Enter the elements of the array : \n";
for(int k=0;k<size;k++)
{
cout<<"\n Element "<<k+1<<" : ";
cin>>a[k];
}
cout<<"\n\n Arraye before sorting : \n";
display(a,size);
insertion(a,size);
cout<<"\n\n Array after sorting : \n";
display(a,size);
delete a;
getch();
}
/************************** OUTPUT 25************************************
Enter the number of elements to be entered in array : 4
Enter the elements of the array :
Element 1 : 54
Element 2 : 32
Element 3 : 56
Element 4 : 5
Array before sorting :
54 32 56 5
Array after sorting :
5 32 54 56

**********************************************************************/
/************************* QUESTION 26 **********************************
Write a program to search an element in a 1D array input by the user and passed as an
argument to the function using the concept of binary search
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int bsearch(int a[],int size,int val)
{
int beg,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{
mid=(beg+last)/2;
if(val==a[mid])
return mid;
else
if(val>a[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;
}
void accept(int a[],int size)
{
cout<<"\n\n Enter the elements of the array (In ascending order) : ";
for(int i=0;i<size;i++)
{
cout<<"\n Element"<<i+1<<" : ";
cin>>a[i];
}
}
void display(int a[],int size)
{
for(int i=0;i<size;i++)
{
cout<<"\n"<<a[i];
} }
void main()
{
clrscr();
int a[50],size,ele,pos;
cout<<"\n\n Enter the size of the array : ";
cin>>size;
accept(a,size);
display(a,size);
cout<<"\n\n Enter the element you want to search : ";
cin>>ele;
pos=bsearch(a,size,ele);
if(pos==-1)
cout<<"\n\n Element not found";
else
cout<<"\n\n Element found at "<<pos+1<<" position";
getch();
}

/***************************** OUTPUT 26********************************


Enter the size of array : 4
Enter the elements of array (In ascending order) :
Element
Element
Element
Element

1
2
3
4

:
:
:
:

1
2
3
4

1
2
3
4
Enter the element to be searched : 2
The element found at 2 position
**********************************************************************/

/************************** QUESTION 27 ***************************


Write a program to search an element in a 1D array input by user and passed as an
argument to the function using the concept of linear
search ?
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int lsearch(int a[],int size,int val)
{
for(int i=0;i<size;i++)
{
if(a[i]==val)
return i;
}
return -1; }
void accept(int a[],int size)
{
cout<<"\n\n Enter the elements of the array : \n";
for(int i=0;i<size;i++)
{
cout<<"\n Element "<<i+1<<" : ";
cin>>a[i];
} }
void display(int a[],int size)
{
cout<<"\n\n The elements of the array are : ";
for(int i=0;i<size;i++)
{
cout<<"\n"<<a[i];
} }
void main()
{
clrscr();
int a[50],size,ele,pos;
cout<<"\n Enter the size of the array : ";
cin>>size;
accept(a,size);
display(a,size);
cout<<"\n\n Enter the element you want to search : ";
cin>>ele;
pos=lsearch(a,size,ele);
if(pos==-1)
cout<<"\n\n Element not found";
else
cout<<"\n\n Element found at "<<pos+1<<" position";
getch();
}
/***************************** OUTPUT 27 ********************************
Enter the size of array : 4
Enter the elements of array :
Element 1 : 1
Element 2 : 2
Element 3 : 3
Element 4 : 4
1
2
3
4
Enter the element to be searched : 2
The element found at 2 position
************************************************************************/

/************************ QUESTION 28 ******************************


Write a program to merge two 1D arrays into one array according to the following
information :
Given :
a. Array A in ascending order
b. Array B in descending order
c. Create an array C in ascending order that would contain all the
elements of A and B in the sorted order.
************************************************************************/
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
void accept(int a[],int size)
{
for(int i=0;i<size;i++)
{
cout<<"\n\n Element "<<i+1<<" : ";
cin>>a[i];
}
}
void display(int a[],int size)
{
cout<<"\n\n The elements of the array are : \n";
for(int i=0;i<size;i++)
{
cout<<"\n"<<a[i];
}
}
void mergsort(int a[],int b[],int c[],int m,int n)
{
int actr=0,bctr=n-1,cctr=0;
while(actr<m && bctr>=0)
{
if(a[actr]<=b[bctr])
c[cctr++]=a[actr++];
else
c[cctr++]=b[bctr--];
}
if(actr<m)
{
while(actr<m)
{
c[cctr++]=a[actr++];
}
}
else
if(bctr>=0)
{
while(bctr>=0)
{
c[cctr++]=b[bctr--];
}
}
}

void main()
{
clrscr();
int a[50],b[50],c[100],m,n;
cout<<"\n\n Enter the size of array A : ";
cin>>m;
cout<<"\n\n Enter the elements of array A in ascending order : ";
accept(a,m);
display(a,m);
getch();
clrscr();
cout<<"\n\n Enter the size of array B : ";
cin>>n;
cout<<"\n\n Enter the elements of array B in descending order : ";
accept(b,n);
display(b,n);
getch();
clrscr();
mergsort(a,b,c,m,n);
cout<<"\n\n The array after merging is : \n";
int p=m+n;
display(c,p);
getch();
}

/***************************** OUTPUT 28 *******************************


Enter the size of array A : 4
Enter the elements of array in ascending order :
Element 1 : 1
Element 2 : 2
Element 3 : 3
Element 4 : 4
1
2
3
4
// NEXT SCREEN
Enter the size of array B : 4
Enter the elements of array in descending order :
Element 1 : 4
Element 2 : 3
Element 3 : 2
Element 4 : 1
4
3
2
1
// NEXT SCREEN
The array after merging is :
1
1
2
2
3
3
4
4
*****************************************************************************/

/************************ QUESTION 29 ******************************


MERGING OF TWO ARRAYS
************************************************************************/
# include <iostream.h>
# include <conio.h>
# include <stdio.h>
void read(int a[10], int &n)
{
int i;
cout<<"Enter the length : ";
cin>>n;
cout<<"Enter the elements :-\n";
for (i = 0; i < n; i++)
cin>>a[i];
}
void display(int a[10], int n)
{
int i;
for (i = 0; i < n;i++)
cout<<a[i]<<"
";
cout<<endl;
}
void merge(int a[10], int b[10], int c[10], int n, int m)
{
int i;
int k = 0;
for (i = 0; i < n; i++)
if ((a[i] % 2) == 1)
c[k++] = a[i];
for (i = 0;i < m;i++)
if ((b[i] % 2) == 1)
c[k++] = b[i];
for (i = n-1; i >= 0; i--)
if ((a[i] % 2) == 0)
c[k++] = a[i];
for (i = m-1;i >= 0;i--)
if ((b[i] % 2) == 0)
c[k++] = b[i];
}
void main()
{
int a[10], b[10], c[10];
int i,j,m,n;
clrscr();
cout<<"\nEnter lhe first array:-\n";
read(a,n);
cout<<"Enter the second array:-\n";
read(b,m);
merge(a,b,c,m,n);
cout<<"\nThe first array is :-\n";
display(a,n);
cout<<"\n the second array is :-\n";
display(b,m);
cout<<"The merged array is :-\n";
display(c,m+n);
getch();
}

OUTPUT
Enter the
Enter the
Enter the
1
2
3
4
5
Enter the
Enter the
Enter the
6
7
8
9
10

first array:length : 5
elements :-

second array:length : 5
elements :-

The first array is :1


2
3
4
5
The
6
The
1

second array is :7
8
9
10
merged array is :3
5
7
9
4

10

/************************ QUESTION 30 ******************************


Replacing elements having even values with its half and odd Values with twice its
values
************************************************************************/
#include<iostream.h>
#include<conio.h>
void accept(int a[],int n);
void convert(int a[],int n);
void display(int a[],int n);
void main()
{
clrscr();
int a[50],n;
cout<<"\n\n Enter the elements you want to enter : ";
cin>>n;
accept(a,n);
convert(a,n);
cout<<"\n\nThe new array is : \n\n";
display(a,n);
getch();
}
void accept(int a[],int n)
{
for(int i=0;i<n;i++)
{
cout<<"\n\n Enter the element "<<i+1<<" : ";
cin>>a[i];
}
}
void convert(int a[],int n)
{
for(int i=0;i<n;i++)
{
if(a[i]%2==0)
a[i]=a[i]/2;
else
a[i]=a[i]*2;
}
}
void display(int a[],int n)
{
for(int i=0;i<n;i++)
cout<<" "<<a[i];
}

OUTPUT
Enter the number of elements you want to enter : 3
Enter element 1 : 11
Enter element 2 : 24
Enter element 3 : 4
The new array is :
22 12 2

/************************ QUESTION 31 ******************************


DISPLAY ALTERNATE POSITIONS OF AN ARRAY
************************************************************************/
#include<iostream.h>
#include<conio.h>
void main()
{
int a[3][3],i,j;
cout<<"enter array :";
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
cin>>a[i][j];}}
for(i=1;i<=3;i++)
{
for(j=1;j<=3;j++)
{
if((i+j)%2==0)
cout<<"alternate positions are :"<<a[i][j]<<"\n";
}}
getch();
}

OUTPUT
enter array : 1 3 6
3 4 6
3 6 8
alternate positions are : 16438

/************************ QUESTION 32 ******************************


Ques -write a program to find sum of each column of nxm matrix
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a[2][3],i,j,sum=0,k;
cout<<enter string :";
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
cin>>a[i][j];}}
for(i=1;i<=2;i++)
{
for(j=1;j<=3;j++)
{
sum=sum+a[k][j];}}
cout<<"sum of column is "<<sum;
getch();
}

OUTPUT
enter string :
2 3 4
2 3 4
sum of column is 18

/************************ QUESTION 33 ******************************


C++ Program to store 5 numbers entered by user in an array and display first and last
number only.
#include <iostream>
int main() {
int n[5];
cout<<"Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {
cin>>n[i];
}
cout<<"First number: "<<n[0]<<endl;
cout<<"Last number: "<<n[4];
}

Output
Enter 5 numbers: 4
-3
5
2
0
First number: 4
Last number: 0

return 0;

/************************ QUESTION 34 ******************************


Ques-Write a program that asks the user to type 10 integers of an array. The program
must compute and write how many integers are greater than or equal to 10.
#include <iostream.h>
#include<conio.h>
void main()
{
int a[10],i,b=0;
cin>>int N;
cout<<"enter the array\n";
for(i=0;i<N;i++)
{
cin>>a[i];
if (a[i]>=10)
b++;
}
cout<<"the number of integers greater or equal to 10 is: "<<b;
getch();
}

Output3
enter the array
1 22 34
the number of integers greater or equal to 10 is:2

/************************ QUESTION 37 ******************************


Ques- Write a C++ Program to Generate the Transpose of a Given Matrix of Order 3 x 3

#include<iostream.h>
int main()
{
int mat[3][3], trans_mat[3][3];
/* Initializing Mat1 and Mat2 */
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cin >> mat[i][j];
}
}
/* Transposing elements of the matrix */
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
trans_mat[j][i] = mat[i][j];
}
}
cout << "Transpose of the Given 3x3 Matrix : " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << trans_mat[i][j] << "\t";
}
cout << endl;
}
}
Output
Enter elements of a 3x3 Matrix :
2
3
4
3
4
5
4
5
6
Transpose of the Given 3x3 Matrix :
2
3
4
3
4
5
4
5
6

/************************ QUESTION 38 ******************************


Ques-C++ Program to Perform Matrix Multiplication

#include<conio.h>
#include<iostream.h>
int main()
{
int a[10][10], b[10][10], c[10][10];
int x, y, i, j, m, n;
cout << "\nEnter the number of rows and columns for Matrix A:::\n\n";
cin >> x >> y;
// x denotes number rows in matrix A
// y denotes number columns in matrix A
cout << "\n\nEnter elements for Matrix A :::\n\n";
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
cin >> a[i][j];
}
cout << "\n";
}
cout << "\n\nMatrix A :\n\n";
for (i = 0; i < x; i++)
{
for (j = 0; j < y; j++)
{
cout << "\t" << a[i][j];
}
cout << "\n\n";
}
cout << "\n-----------------------------------------------------------\n";
cout << "\nEnter the number of rows and columns for Matrix B:::\n\n";
cin >> m >> n;
// m denotes number rows in matrix B
// n denotes number columns in matrix B
cout << "\n\nEnter elements for Matrix B :::\n\n";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cin >> b[i][j];
}
cout << "\n";
}
cout << "\n\nMatrix B :\n\n";
for (i = 0; i < m; i++)
{
for (j = 0; j < n; j++)
{
cout << "\t" << b[i][j];
}
cout << "\n\n";
}
if (y == m)
{
for (i = 0; i < x; i++)
{
for (j = 0; j < n; j++)

{
c[i][j] = 0;
for (int k = 0; k < m; k++)
{
c[i][j] = c[i][j] + a[i][k] * b[k][j];
}
}
}
cout
<< "\n-----------------------------------------------------------\n";
cout << "\n\nMultiplication of Matrix A and Matrix B :\n\n";
for (i = 0; i < x; i++)
{
for (j = 0; j < n; j++)
{
cout << "\t" << c[i][j];
}
cout << "\n\n";
}
}
else
{
cout << "\n\nMultiplication is not possible";
}
getch();
return 0;
}
Output
Enter the number of rows and columns for Matrix A:::
2 2
Enter elements for Matrix A :::
1 2
3 4
Matrix A :
1
2
3
4
----------------------------------------------------------Enter the number of rows and columns for Matrix B:::
2 2
Enter elements for Matrix B :::
4 5
6 7
Matrix B :
4
5
6
7
----------------------------------------------------------Multiplication of Matrix A and Matrix B :
16
19
36
4
/************************ QUESTION 39 ******************************

Ques -Write a user-defined function named Lower_half() which takes 2D array A, with
size N rows and N columns as argument and prints the lower half of the array.
#include<conio.h>
#include<iostream.h>
int main()
{
void lower_half(int a[10][10],int n)
{
int i,j;
for(i=0;i<n;i++)
{ cout<<endl;
for(j=0;j<n;j++)
if(i>=j)
cout<<a[i][j]<<" ";
}
}return 0;
}

Output 2
7
2
0
3

3
1
5
1
4

1
5
7
5
9

5
3
8
0
1

0
1
1
1
5

2
7
2
0
3

1
5 7
1 5 0
4 9 1 5

/************************ QUESTION 40 ******************************

Ques- C++ Program to Calculate Average of Numbers Using Arrays


#include <iostream.h>
int main(){
int n, i;
float num[100], sum=0.0, average;
cout << "Enter the numbers of data: ";
cin >> n;
while (n>100 || n<=0)
{
cout << "Error! number should in range of (1 to 100)." << endl;
cout << "Enter the number again: ";
cin >> n;
}
for(i=0; i<n; ++i)
{
cout << i+1 << ". Enter number: ";
cin >> num[i];
sum+=num[i];
}
average=sum/n;
cout << "Average = " << average;
return 0;
}
OutputEnter the numbers of data: 6
1. Enter number: 45.3
2. Enter number: 67.5
3. Enter number: -45.6
4. Enter number: 20.34
5. Enter number: 33
6. Enter number: 45.6
Average = 27.69

CLASSES

INHERITANCE

FILE
HANDLING

ARRAYS

You might also like