Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program that prompts the user to enter the number of elements and the nu

ID: 3629101 • Letter: W

Question


Write a program that prompts the user to enter the number of elements and the
numbers themselves to be placed in an integer array that holds a maximum of 50
elements. The program should then prompt the user for an integer which will be
searched for in the array using a binary search. Make sure to include the following
steps along the way:

3) Add a value returning function that computes the mean of your data set.
Recall that the mean is the sum of the data values divided by the number
of pieces of data. Your program should output the size of the array
entered, the array as entered by the user, the sorted array, the integer
being searched for, the location of that integer in the sorted array (or an
appropriate message if it is not in the array), and the mean of the data set.

Explanation / Answer

please rate - thanks

#include <iostream>
using namespace std;
void sort(int[],int);
int binarysearch(int[],int,int);
double mean(int[],int);
void print(int[],int);
int main()
{int numbers[50],n,i,key,val;
cout<<"How many numbers do you have? (max 50): ";
cin>>n;
for(i=0;i<n;i++)
    {cout<<"Enter number "<<i+1<<": ";
    cin>>numbers[i];
    }
cout<<"Enter the number to search for: ";
cin>>key;  
cout<<"The unsorted "<<n<<" numbers ";
print(numbers,n);   
sort(numbers,n);
cout<<" The unsort "<<n<<" numbers ";
print(numbers,n);


val=binarysearch(numbers,n,key);
if(val<0)
    cout<<key <<" was not found ";
else
    cout<<key<<" was found at index "<<val<<endl;
cout<<"The average of the numbers is "<<mean(numbers,n)<<endl;
system("pause");
return 0;
}
void print(int a[],int n)
{int i;
for(i=0;i<n;i++)
    {cout<<a[i]<<" ";
    if((i+1)%10==0)
        cout<<endl;
    }
cout<<endl;
}
double mean(int a[],int n)
{int i,sum=0;
for(i=0;i<n;i++)
     sum+=a[i];
return (double)sum/n;
}
void sort(int a[],int n)
{int i,j,t;
for(i=0;i<n-1;i++)
     for(j=i;j<n;j++)
        if(a[i]>a[j])
            {t=a[i];
            a[i]=a[j];
            a[j]=t;
            }
}
int binarysearch(int a[],int n,int key)
{int low=0,max=n,mid;
while(low<=max )
    {mid=(low+max)/2;
    if(a[mid]<key)
       low = mid + 1;
    else
        {if(a[mid]>key )
            max=mid-1;                   
        else
           {return mid;
           }
        }
     }
return -1;
}