CH Programming 3
S
C++ Programming
‘Write a program to find the frequency of presence of an elelment in an array.
Logic: ‘Counting the given element(EELE) for how many times it exists in an array
by traversing. Initially compare ELE with first clement in an array. If first element is
equal to ELE, increment the count by 1. Next compare ELE with second element, if
second clementis equal to ELE, again increment the count by 1. Similarly compare
ELE with all other elements until the last.clement. Whenever comparison gives true
result, increment the.countiby 1. Finally display the count.
Example: Finding the frequency of 25 from the following array:
~ |e [|e fs | 9 |
Af) AQ] A) AB) Ala
When ELE=All], Initially Count=0 ;
1=0, 25=25, (Tue) Count=Count+ 1. ‘Cout=0+1=1
1=1, 25=16, (False) No increment Count= 1
J=2, 25=67, (False) No increment ‘Count = 1
J=3, 25=25, (True) Count= Count +1. Count = 1+1=2
1=4, 25=19, (False) No increment ‘Count = 2
1=5 (Outofrange). Therefore frequency of 25 =?
Program:
#include
#include
#include
class Frequency
{
private:
int n, a[100], i, ELE, freq;
public:
void input() ;
void findfrequency()
void display() ;C++ Programming
void Frequency :: input( )
{
cin >>
nter the array elements” << endl ;
isn; i++)
cin >> ali] ;
cout << “Enter the search element :” ;
cin >> ELE ;
void Frequency :: findfrequency( )
{
freq
for(i=0; i 0) ;
cout << “Frequency of” << ELE << “is” << freq <<” times” ;
else
cout << ELE << “ does not exist” ;
}
void main()
Frequency F ;
clrscr()
F.input( ) 5
Ffindfrequency( ) ;
F.display() ;
getch() ;
}C++ Programming
Output 1:
Enter the array size: §
Enter the array elements: 25 16 67
Enter the search element: 25
Frequency of 25 is 2 times
Output
Enter the array siz
24
Enter the array elements: 55 26 71
Enter the search element: 56
Frequency of 56 is 0 times
25
a1Write a program to insert an element into an array at position.
Logic: Create the space for a new element(ELE) at the appropriate position by
shifting the elements in an array. Then insert ELE to that position. It increases the
array size by one position.
Example: An array contains following elements.
20 30 | so | 60 | 7
Afo]) Afi] A[2] A[3] A[4]
Insert an element 40 at position to. i.e. ELE = 40 and POS = 2.
A[5] =AI4], 20 30 | 50 60 at 70
Ato] AU] AR} ABI AL] ALS)
A[4] =A[3], 20 30 50 60 70
Ato] Aft] A] AB) AL4] ALS]
A(3] =A(2], 20 30 50 60 70
Ao] Ati Al2] AB) Al4] ALS]
Resultant array[ 20 | 30 | 40 | so | 60 | 70
Ao) At] AR] AB] Al4] ALS]
, Program:
#include
#include
#include
ah tnclude & std -h>class Insert
f
private:
int n, af 100], i, BI
public:
void input() ;
void insertion( )
void display( ) ;.
IE, pos. 5
Le
void Insert :: input()
{
cout << “Enter the array size:” ;
cin>>n;
cout << “Enter the array elements” << end! ;
for(i=0; i> ali} ;
cout << “Enter an element to:insert:” ;
cin >> ELE ;
cout << “Enter the position:” ;
cin >> pos ;.
} :
void Insert :: insertion( )
{
if(pos > n)
{
cout << pos << “is an invalid position.” ;
exit(O) ;
}
else
{ ‘
for(i=n-1; i>=pos; i-)
afi+ 1] = ali] s
a[pos] = ELE ;
n=nt1;C+ Programming
}
}
void Insert :: display()
{
cout << “Resultant
for(i=0;i
#include
BinclndeSiomaniDI 5 aprncbuucte < Atdbtb: h>
{
private:
int n, a[100], i, ELE, pos ;
public:
void input() ;
void deletion( ) ;
void display() ;
Bs
void Delete :: input()
t
cout << “Enter the array size :” ;
cin>>n;
cout << “Enter the array elements” << endl ;
for(i=0; i> afi] 5
cout << “Enter the position (0 to” <> pos 5}
void Delete :: deletion( )
{
if(pos > n-1)
{
cout << pos << “is an invalid position.” << endl ;
exit(0) ;
}
else
{
ELE = a{pos] ;
for(i= pos ; i
#include
#include
class Sort
{
private:
int n, a[100] ;
public:
void input) ;CH Programming 7
void sorting( ) ;
void display( ) ;
hs
void Sort :: input( )
{
cout << “Enter the array size :” ;
cin>>n;
cout << “Enter the array elements” << endl ;
for(int i=0; i> ai] ;
}
void Sort :: sorting( )
{
int t, j,i;
for(i=1; i= 1)
{
~if(alj] < alj-1])
yo
}
}
}
}
void Sort :: display()
{
cout << endl << “An array after sortingz” << endl ;
for(inti=0; i
#include
class Search
{
private:
int a[100], n, ELE, loc, i;
public:
void input() ;
void bin_search( ) ;
void display() ;
hs
void Search :: input()
{
cout << “Enter the size array:” ;
cin>>n;
cout << “Enter the array elements:” << endl ;
for(i=0; i> afi] ;
cout << “Enter search element:” << endl ;
cin >> ELE ;
}
void Search :: bin_search( )
{
int beg = 0, end=n-1, mid;
loc =-1;
while(beg <= end)mid = (beg + end) / 2;
iN(EL a[mid])
{
loc = mid ;
break ;
}
else if(ELE < a[mid])
end = mid - 1;
else
beg = mid + 1;
1
J
void Search :: display( )
{
if(loc >= 0)
cout << ELE << “ exists in position” << loc << endl ;
else
}
void main( )
cout << ELE << “Element does not exists” ;
Search B ;
elrscr( ) ;
B.input( ) ;
B.bin_search( ) ;
B.display() ; 5 quero?
}
Output 1:
Enter the array size: 5
Enter the array elements: 12 26 38
Enter the search element: 26
26 exists in position 1
Output 2:
Enter the array size: 4
26 38 = 42
Enter the array elements:
42 59
59Write a program to create a class with data members principle amount,
time and rate of i t. Create a member fuctions to accept data values to
compute simple interest and to display the result.
Logic: The formula to find the simple interest is
gi PXTXR
100
Where P is principal amount, T is time and R is rate of interest.
Example: P= 1000 T=2 R=1.5%
gy = 1000 x 2 x 1.5
100 Simple interest = 30.00
Program:
#include
#include
#include
class Interest
{
private:
double P, T, R, SI;
public:
void input( ) ;
void compute( ) ;
void display( ) ;
33
void Interest :: input()
{
cout << “Enter principal amount, time and rate of interest:” << endl ;
cin>>P>>T>>R;
}
void Interest :: compute( )
{
SI=(P*T*R)/100;,
Void Interest :: display( )
{
cout << “Simple interest =” << SI ;
}
void main( )
{
Interest I ;
elrscr( ) ;
Linput() ;
T.compute( ) ;
Ldisplay( ) ;
getch( ) ;
}
Output:
Enter the principal amount, time and rate of interest : 1000
Simple interest = 30.00
2
1Saz.) Write a program to create a class with data members a, b, c and member
functions to input data, compute the discriminant based on the following
conditions and print the roots.
1) If discriminant = 0, print the roots that are equal.
2) If the discriminant is > 0, print the real and distinct roots.
3) If the discriminant < 0, print that the roots are imaginary.
Logic: The quadratic equation is in the form ax? + bx + c= 0. Where a, b, are
constants and x is the variable. If a = 0, then it becomes linear equation.
The formula to solve the quadratic equation is,
—b+Vb? —4ac
2a
Where b? — 4ac is called discriminant (D) of the quadratic equation.
If D =0, roots are equal,
D> 0, roots are real and distinct,
x=CH Programming
7
D <0, roots are imaginary,
Example:
x+3x-4=0
a=1,b=3,c=-4
_ 3 ea 3*- ACA)
2(1)
ec3EN 25 _ 345
2 [72
x=-4 x=1
Program:
#include
#include
#include Htaclude 2 ardkele “h>
class Quadratic
{
private:
double a, b, c, rl, 12, d;
public:
void getdata( ) ;
void compute( ) ;
void display( ) ;
35
void Quadratic :: getdata()
{
cout << “Enter the co-efficients:” ;
cin>>a>>b>>c;
}
void Quadratic :: compute( )
{
d=b*b-4*a*c;
if(d = 0)
{
cout << “Roots are equal” << endl ;
(-b - sqrt(d)) / (2 * a) ;
=r;}
else
if(d > 0)
{
cout << “Roots are different” << endl ;
rl =(-b+ sqr(d))/(2* a);
12=(-b- sqrt(d)) /(2* a);
}
else
{
cout << “Roots are imaginary” ;
exit(0) ;
}
i
void Quadratic :: display()
{
cout << “First root =” << rl << endl ;
cout << “Second root =” << 12 5
}
void main( )
{
Quadratic Q ;
clrser( ) ;
Qgetdata() ;
Q.compute( ) ;
Q.display( ) ;
getch() ;
}
Output 1:
Enter the co-efficients: 2. 4 2
Roots are equal
First root = -1
Second root =-1Output 2:
Enter the co-efficients : 1 3 4
Roots are distinct
First root = 1
Second root = -4
Output 3:
Enter the co-efficients: 3. 2 5
Roots are imaginary.Write a program to find the cube of a number using inline function.
Logic: Calculating the cube of a number (N = 5) using the formula,
Cube of N =NxXNXN
=5x5x5
=125Program:
#include
#include
Hinclude
class FindCube
{
public:
inline int CUBE(int x)
{
retumn(x * x * x);
}
33
void main( )
{
FindCube F ; i
intns CL SEED) ‘
cout << “Enter a number: “ ;
cin >> n;
cout << “Cube of * << n << "=“ << FCUBE(n);
y POO?
Output: Enter a number : 5
Cube of 5 = 125“
1 1v6ramming
Create a base class containing the data members roll number and name. Also
create a member function to read and display the data using the concept of
Single level inheritance. Create a derived class that contains marks of tw
Subjects and total marks as the data members. :
Logie: An inheritance is the process of generating a new class by extracting the
Properties from the pre-existing class. In single level inheritance a new class is
derived from the one base class,
Program:
#include
#include
class Student
{
private:
int regno ;
char name[20] ;
public:
void base_getdata( )
{
cout << “Enter the student name :” << endl ;
cin.getline(name, 20) ;
cout << “Enter the register number :” << endl ;
cin >> regno ;
}
void base_display( )
{
cout << “Name =” << name << endl ;
cout << “Student details are” << endl ;
cout << “Register number =” << regno << endl ;
}
5
class marks : public fludent
{
private:
int mark, mark2, total ;C+ Programming
25
public:
void derived_getdata( )
{
cout << “Enter two subject marks:” ;
cin >> markl >> mark2 ;
total = mark1 + mark2 ;
}
void derived_display( )
‘
5
cout << “Subject! marks =” << mark1 << endl ;
cout << “Subject2 marks =” << mark2 << endl ;
cout << “Total marks =” << total ;
33
void main( )
{
marks ob ;
clrser() ;
ob.base_getdata() ;
ob.derived_getdata() ;
ob.base_display() ;
ob.derived_display() ;
getch() ;
}
Output:
Enter the student name : Sunil Kumar
Enter the register number : $562145
Enter two subject marks: 86 91
The student details are:
Student Name = Sunil Kumar
Register Number = $562145
Subject] marks = 86
Subject2 marks = 91
Total marks = 17726
(ext
CH Programming
a members register number, name
Cc ae ‘ing dat:
reate a class containing the following da’ d display the data using
and fees. Also create a member function to read an'
the concept of pointers to objects.
Logie: Pointer is a variable that stores the memory address O}
Example: int *ptr ;
Where ptr is a pointer can hold the address of integer variable.
0; — //initializing the integer variable
f another variable.
ay // assigning an address of ‘a’ to pointer variable ‘ptr’
int *ptr // declaration of pointer variable ptr. *ptr points to value of ‘a’.
The value of normal variable ‘a’ and pointer variable ‘ptr’ is shown below.
a ptr
10 | [12562
12562 — 16346
Program:
#include
#include
class Student
{
private:
int regno ;
char name[20] ;
float fee ;
public:
void getdata( ) ;
void display( ) ;
bs
void Student :: getdata()
: cout << “Enter the register number :” << endl ;
cin >> regno ;
cout << “Enter the student name :” << endl ;
cin >> name ;
cout << “Enter the student fees :” << endl ;
cin >> fee;C+ Programming 21
void Student :: display( )
{
cout << endl << “Student details are...” << endl ;
cout << “Register number =” << regno << endl ;
cout << “Name =” << name << endl ;
cout << “Fees =” << fee ;
}
void main( )
t
Ss
student S, *ptr ;
clrser() ;
ptr=&S;
ptr > getdata() ;
ptr > display() ;
getch() ;
}
Output:
Enter the register number : 20146789
Enter the student name : Soujanya
Enter the student fees : 5000.00
The student details are...
Register number = $20146789
Name = Soujanya
Fees = 5000.00