0% found this document useful (0 votes)
4 views

C++ PROGRAMS Converted

The document contains a series of C++ programs that demonstrate various data structure operations, including finding the frequency of an element in an array, inserting and deleting elements, sorting an array using insertion sort, and searching for an element using binary search. It also includes programs for calculating simple interest and determining the nature of roots of a quadratic equation based on the discriminant. Each program is structured with class definitions and member functions to encapsulate the functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

C++ PROGRAMS Converted

The document contains a series of C++ programs that demonstrate various data structure operations, including finding the frequency of an element in an array, inserting and deleting elements, sorting an array using insertion sort, and searching for an element using binary search. It also includes programs for calculating simple interest and determining the nature of roots of a quadratic equation based on the discriminant. Each program is structured with class definitions and member functions to encapsulate the functionality.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

C++ AND DATA STRUCTURE

/*1. Write a program to find the frequency of presence of an element in an array*/

#include<iostream.h>
#include<conio.h>
class frequency
{
int a[10],n,i,ele;
public: void getdata();
void putdata();
};
void frequency::getdata()
{
cout<<"Enter the size of an array";
cin>>n;
cout<<"Enter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
}
void frequency::putdata()
{
int count=0;
cout<<"Enter the element whose frequency is to be checked: ";
cin>>ele;
for(i=0;i<n;i++)
if(ele==a[i])
count++;
cout<<ele<<" has appeared "<<count<<" times";
}
void main()
{
frequency f;
clrscr();
f.getdata();
f.putdata();
getch();
}
/* 2. Write a program to insert an element into an array at a given position.*/

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,n,ele,p;
cout<<"Enter the size of an array: ";
cin>>n;
cout<<"Enter the elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the element to be inserted: ";
cin>>ele;
cout<<"Enter the position (from 0 to "<<n<<")";
cin>>p;
if(p>n)
cout<<"Invalid Position";
else
{
for(i=n-1;i>=p;i--)
a[i+1]=a[i];
a[p]=ele;
n=n+1;
}
cout<<"Elements after insertion are:\n";
for(i=0;i<n;i++)
cout<<a[i];
getch();
}
/* 3. Write a program to delete an element from an array from a given position.*/

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,n,ele,p;
clrscr();
cout<<"Enter the size of an array: ";
cin>>n;
cout<<"Enter the elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the position (from 0 to "<<n-1<<")";
cin>>p;
if(p>n-1)
cout<<"Invalid position";
else
{
ele=a[p];
for(i=p;i<n;i++)
a[i]=a[i+1];
n=n-1;
cout<<"Elements after deletion are:\n";
for(i=0;i<n;i++)
cout<<a[i];
}
getch();
}
/* 4. Write a program to sort the elements of an array in ascending order using an
insertion sort.*/

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,j,n,temp;
clrscr();
cout<<"Enter the size of an array: ";
cin>>n;
cout<<"Enter the elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
for(i=1;i<n;i++)
{
temp=a[i];
j=i-1;
while(temp<a[j]&&j>=0)
{
a[j+1]=a[j];
j=j-1;
}
a[j+1]=temp;
}
cout<<"Sorted elements are:\n";
for(i=0;i<n;i++)
cout<<a[i];
getch();
}
/* 5. Write a program to search for a given element in an array using Bianry search
method. */

#include<iostream.h>
#include<conio.h>
void main()
{
int a[10],i,n,ele,pos=-1,m,b,e;
clrscr();
cout<<"Enter the size of an array: ";
cin>>n;
cout<<"Enter the elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"Enter the search element: ";
cin>>ele;
b=0;
e=n-1;
while(b<=e)
{
m=(b+e)/2;
if(ele==a[m])
{
pos=m;
break;
}
else if(ele<a[m])
e=m-1;
else
b=m+1;
}
if(pos>=0)
cout<<"Location="<<pos;
else
cout<<"Search is unsuccessful";
getch();
}
6. Write a program to create a class with data member principal, time and rate. Create
member functions to accept data values to compute simple interest and to display the
result*/

#include<iostream.h>
#include<conio.h>
class SI
{
int time;
float principal,rate;
public: void getdata()
{
cout<<"Enter the principal, time and rate of interest: \n";
cin>>principal>>time>>rate;
}
float compute_si()
{
return(principal*time*rate/100.0);
}
void display()
{
cout<<"Simple Interest="<<compute_si();
}
};
void main()
{
SI s;
clrscr();
s.getdata();
s.display();
getch();
}
/* 7. 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.
- if discriminant=0, print the roots that are equal
- if the discriminant is>0, print the real roots
- if the discriminant is<0, print that the roots are imaginary*/

#include<iostream.h>
#include<conio.h>
#include<math.h>
class quad
{
float x1,x2,imaginarypart,realpart,discriminant,a,b,c;
public: void getdata()
{
cout<<"Enter the values of a, b, c: ";
cin>>a>>b>>c;
discriminant=b*b-4*a*c;
}
void putdata()
{
if(discriminant>0)
{
x1=(-b+sqrt(discriminant))/(2*a);
x2=(-b-sqrt(discriminant))/(2*a);
cout<<"Roots are real and different:\n";
cout<<"x1="<<x1<<"and x2="<<x2;
}
else if(discriminant==0)
{
x1=-b/(2*a);
cout<<"Roots are real and equal\n";
cout<<"x1=x2="<<x1;
}
else
{
realpart=-b/(2*a);
imaginarypart=sqrt(-discriminant)/(2*a);
cout<<"Roots are imaginary\n";
cout<<realpart<<"+"<<imaginarypart<<"i"<<endl;
cout<<realpart<<"-"<<imaginarypart<<"i";
}
}
};
void main()
{
quad q;
clrscr();
q.getdata();
q.putdata();
getch();
}

You might also like