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

write a program that reads a data file called RandNumLond.dat. This file contain

ID: 3631452 • Letter: W

Question

write a program that reads a data file called RandNumLond.dat. This file contains no more than 1,000 integers, with one integer value each line. The value of each integer ranges from 0 to 5000. Yours program should read these numbers into a one-dimensional array and read until it reaches the end of the file. use a bubble sort function, sort your array and then write the answer to the following questions to the screen.

How many numbers are in the data file?
What is the average value?
What is the largest(max) value in array?
what is the lowest (min) value in array?

search for some items as follows:

use binary search to search the algorithm (use the avg value as the search key)
if the item is found in the list,t hen print its postition.
What is the median value?

your program may read the data file in the main function and fill the array. The functions listed
int FindAve (int {}, int); return average
int BubbleSort (int {}, int) pass unsorted array and sort from low to high
int FindMedian (int {},int)
int BinarySearch(const int{},int,int)

Explanation / Answer

please rate - thanks

BubbleSort should be void, it doesn't return anything

average should return a double, but I'm leaving it as int

#include <iostream>
#include <fstream>
using namespace std;
int FindAve(int[],int);
void BubbleSort (int [], int);
int FindMedian (int [],int);
int BinarySearch(const int[],int,int);
int main()
{int a[1000],n=0;
int avg,loc;
ifstream input;
input.open("RandNumLond.dat");           //open file
   if(input.fail())             //is it ok?
       { cout<<"file did not open please check it ";
        system("pause");
        return 1;
        }
input>>a[n];
while(input&&n<1000)
   {n++;
    input>>a[n];
   }
BubbleSort(a,n);
avg=FindAve(a,n);
loc=BinarySearch(a,n,avg);
cout<<"The file has "<<n<<" numbers ";
cout<<"The average value is: "<<avg<<endl;
if(loc<=0)
    cout<<"It is not in the array ";
else
    cout<<"It is at location "<<loc<<" of the array ";
cout<<"The median number is: "<<FindMedian(a,n)<<endl;
cout<<"The largest number is: "<<a[n-1]<<endl;
cout<<"The smallest number is: "<<a[0]<<endl;
input.close();
system("pause");
return 0;
}
int FindAve(int a[],int n)
{int i,sum=0;
for(i=0;i<n;i++)
    sum+=a[i];
return sum/n;
}
int FindMedian (int a[], int n)
{int m;
if(n%2==0)
     return ((int)(a[n/2-1]+a[n/2])/2);
else
    return a[n/2];
}
int BinarySearch(const int a[],int max,int key)
{int mid,low=0;
max--;
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;
}
void BubbleSort(int a[],int n)
{int i,j,temp,flag=1,c=0,m=0;
for(i=1;i<=n&&flag==1;i++)
     {flag = 0;
     for(j=0;j<n-1;j++)
        {c++;
        if(a[j+1]<a[j])   
          {temp = a[j];           
           a[j] = a[j+1];
           a[j+1] = temp;
           flag = 1;
           m++;             
           }
        }
     }
}