#include using namespace std; //function to sort elements of array void sort(int a[], int n) { int i,j,temp; for(i=1;ia[j+1]) { temp=a[j]; a[j]=a[j+1]; a[j+1]=temp; } } } } //function to search location of item using binary search int binary_search(int a[],int n,int item) { int beg,end,mid; beg=0; end=n-1; mid=(beg+end)/2; while((beg<=end)&&(a[mid]!=item)) { if(item>n; cout<<"Enter "<>a[i]; } cout<<"\nThe size of the array entered by user is: "<>item; cout<<"\nThe integer being searched is: "<
#include<iostream>
using namespace std;
//function to sort elements of array
void sort(int a[], int n)
{
int i,j,temp;
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
}
//function to search location of item using binary search
int binary_search(int a[],int n,int item)
{
int beg,end,mid;
beg=0;
end=n-1;
mid=(beg+end)/2;
while((beg<=end)&&(a[mid]!=item))
{
if(item<a[mid])
{
end=mid-1;
}
else
{
beg=mid+1;
}
mid=(beg+end)/2;
}
if(item==a[mid])
{
return mid;
}
else
{
return -1;
}
}
//function to calculate mean
float mean(int a[],int n)
{
int i;
float sum=0;
for(i=0;i<n;i++)
{
sum=sum+a[i];
}
sum=sum/n;
return sum;
}
//main function declaration
int main()
{
int a[50],n,i,item,loc;
cout<<"Enter no. of elements you wants: ";
cin>>n;
cout<<"Enter "<<n<<"array elements:"<<endl;
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\nThe size of the array entered by user is: "<<n<<endl;
cout<<"\nArray elements entered by user is:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
sort(a,n);
cout<<endl<<"\nAfter sorting array elements are:"<<endl;
for(i=0;i<n;i++)
{
cout<<a[i]<<"\t";
}
cout<<endl<<"\nEnter item to search: ";
cin>>item;
cout<<"\nThe integer being searched is: "<<item<<endl;
loc=binary_search(a,n,item);
if(loc==-1)
{
cout<<"\nSearch unsuccessfull";
}
else
{
cout<<"\nThe location of that integer in the sorted array is: "<<loc<<endl;
}
cout<<"\nThe mean of the data set is: "<<mean(a,n);
return 0;
}
for all the functions that are used write prototypes of them and they should be written after main. like the example below:
Below is an example of a program where the function is written using prototypes:
void foo();
int main() {
foo();
}
void foo() {
cout <<This is an example where I have a prototype above main and am implemented after main" << endl;
}
![](/static/compass_v2/shared-icons/check-mark.png)
1. Start
2. Declare an array a[] to store integers.
3. Read the number of elements, n, from the user.
4. Read n integers and store them in the array a[].
5. Initialize variables i and j.
6. Use nested loops for sorting:
a. Outer loop (i-loop) runs from 1 to n-1.
b. Inner loop (j-loop) runs from 0 to n-i.
c. Compare adjacent elements a[j] and a[j+1].
d. If a[j] > a[j+1], swap them.
7. Continue the sorting process until the entire array is sorted.
8. Display the sorted array a[].
Algorithm for Binary Search:
1. Start
2. Declare an array a[] (which is assumed to be sorted).
3. Read the item to search, item, from the user.
4. Initialize variables beg, end, and mid.
5. Set beg to 0 (beginning of the array) and end to n-1 (end of the array), where n is the size of the array.
6. Use a while loop to perform binary search:
a. Calculate mid as (beg + end) / 2.
b. Compare a[mid] with the item:
- If a[mid] is equal to the item, return mid (item found).
- If item < a[mid], set end to mid - 1 (search the left half).
- If item > a[mid], set beg to mid + 1 (search the right half).
c. Repeat steps a and b until beg is less than or equal to end.
7. If the item is not found after the loop, return -1 (item not in the array).
8. Display the search result.
Algorithm for Calculating Mean:
1. Start
2. Declare an array a[] to store integers.
3. Read the number of elements, n, from the user.
4. Read n integers and store them in the array a[].
5. Initialize variables i and sum to 0.
6. Use a loop to calculate the sum of all elements in the array:
a. Iterate from i = 0 to i = n-1.
b. Add a[i] to the sum.
7. Calculate the mean as sum / n.
8. Display the calculated mean.
Algorithm for the Main Function:
1. Start
2. Declare an integer array a[] and variables n, i, item, and loc.
3. Read the number of elements, n, from the user.
4. Read n integers and store them in the array a[].
5. Sort the array a[] using the sorting algorithm.
6. Display the sorted array.
7. Read the item to search, item, from the user.
8. Perform a binary search on the sorted array a[] to find the location of the item.
9. Display the result of the binary search.
10. Calculate the mean of the data set using the mean algorithm.
11. Display the calculated mean.
12. End.
Step by step
Solved in 4 steps with 2 images
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)