/* Program of array using class*/
#include<iostream>
using namespace std;
class array
{
int arr[100],size;
public:
array()
{
for(int i=0;i<size;i++)
arr[i]=0;
}
void insert();
void show();
int max();
int min();
int search(int);
void sort();
void reverse();
int sum_element();
int frequency(int);
};
void array::insert()
{
cout<<"\nEnter number of terms=";
cin>>size;
cout<<"\nEnter the numbers=";
for(int i=0;i<size;i++)
cin>>arr[i];
}
void array::show()
{
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
}
int array:: max()
{
int m=arr[0];
for(int i=1;i<size;i++)
{
if(m<arr[i])
m=arr[i];
}
return m;
}
int array:: min()
{
int m=arr[0];
for(int i=1;i<size;i++)
{
if(m>arr[i])
m=arr[i];
}
return m;
}
int array:: search(int ele)
{
int loc=-1;
for(int i=0;i<size;i++)
if(ele==arr[i])
{
loc=i;
break;
}
return loc;
}
void array:: sort()
{
int temp;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void array:: reverse()
{
int temp;
for(int i=0;i<=size/2;i++)
{
temp=arr[i];
arr[i]=arr[size-i-1];
arr[size-i-1]=temp;
}
}
int array:: sum_element()
{
int sum=0;
for(int i=0;i<size;i++)
{
sum+=arr[i];
}
return sum;
}
int array::frequency(int ele)
{
int c=0;
for(int i=0;i<size;i++)
{
if(ele==arr[i])
{
c++;
}
}
return c;
}
main()
{
array a1;
int x;
a1.insert();
a1.show();
cout<<"\nThe maximum element="<<a1.max();
cout<<"\nThe mimimum element="<<a1.min();
cout<<"\nThe sum of the elements="<<a1.sum_element();
cout<<"\nEnter an element to search=";
cin>>x;
if(a1.search(x)==-1)
cout<<"\nElement not found\n";
else
cout<<"\nElement found in the position "<<a1.search(x)<<endl;
cout<<"\nEnter a number=";
cin>>x;
cout<<"Frequency of "<<x<<" in the array="<<a1.frequency(x);
cout<<"\nThe reversed array=";
a1.reverse();
a1.show();
cout<<"\nThe sorted array=";
a1.sort();
a1.show();
return 0;
}
#include<iostream>
using namespace std;
class array
{
int arr[100],size;
public:
array()
{
for(int i=0;i<size;i++)
arr[i]=0;
}
void insert();
void show();
int max();
int min();
int search(int);
void sort();
void reverse();
int sum_element();
int frequency(int);
};
void array::insert()
{
cout<<"\nEnter number of terms=";
cin>>size;
cout<<"\nEnter the numbers=";
for(int i=0;i<size;i++)
cin>>arr[i];
}
void array::show()
{
for(int i=0;i<size;i++)
cout<<arr[i]<<" ";
}
int array:: max()
{
int m=arr[0];
for(int i=1;i<size;i++)
{
if(m<arr[i])
m=arr[i];
}
return m;
}
int array:: min()
{
int m=arr[0];
for(int i=1;i<size;i++)
{
if(m>arr[i])
m=arr[i];
}
return m;
}
int array:: search(int ele)
{
int loc=-1;
for(int i=0;i<size;i++)
if(ele==arr[i])
{
loc=i;
break;
}
return loc;
}
void array:: sort()
{
int temp;
for(int i=0;i<size;i++)
{
for(int j=i+1;j<size;j++)
{
if(arr[i]>arr[j])
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
}
void array:: reverse()
{
int temp;
for(int i=0;i<=size/2;i++)
{
temp=arr[i];
arr[i]=arr[size-i-1];
arr[size-i-1]=temp;
}
}
int array:: sum_element()
{
int sum=0;
for(int i=0;i<size;i++)
{
sum+=arr[i];
}
return sum;
}
int array::frequency(int ele)
{
int c=0;
for(int i=0;i<size;i++)
{
if(ele==arr[i])
{
c++;
}
}
return c;
}
main()
{
array a1;
int x;
a1.insert();
a1.show();
cout<<"\nThe maximum element="<<a1.max();
cout<<"\nThe mimimum element="<<a1.min();
cout<<"\nThe sum of the elements="<<a1.sum_element();
cout<<"\nEnter an element to search=";
cin>>x;
if(a1.search(x)==-1)
cout<<"\nElement not found\n";
else
cout<<"\nElement found in the position "<<a1.search(x)<<endl;
cout<<"\nEnter a number=";
cin>>x;
cout<<"Frequency of "<<x<<" in the array="<<a1.frequency(x);
cout<<"\nThe reversed array=";
a1.reverse();
a1.show();
cout<<"\nThe sorted array=";
a1.sort();
a1.show();
return 0;
}
No comments:
Post a Comment