Write a program that reads a data file called RandNumLond.dat This file contain
ID: 3631470 • Letter: W
Question
Write a program that reads a data file called RandNumLond.dat This file contain to more than 1,000 integers, with one integer value each line. The value of each integer ranges from 0 to 5000. Your program should read these numbers into a one-demession array and read until it reaches the end of the file. Using bubble sort function, sort your array and then write the answer to the following question to the screen.How many numbers are in the data file?
What is the average value? (Display the answer to two decimal place of accuracy)
What is the largest (maximum) value in the array?
What is the smallest (minimum) value in the array?
Search for some items as follows:
Use binary search to search the algorithm(use the average value as the search key)
If the item is found in the list, then print its position.
What is the median value? That is, what number is in it the middle of the list so that there are an equal number of value above it below it?
Your program may read the data file in the main function and fill the array. The function listed the following table are called from the main.
int FindAve(int [], int); Pass the array and total in the array, return the average value.
int BubbleSort(int [], int); Pass unsorted array and it sort it for you from low to high
int FindMedian(int [], int); Pass the sorted array and total then dtermines (and return) the median value.
int BinarySearch(const int [], int, int); Pass the sorted array and total then determines ( and return) the position where the key is found.
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++;
}
}
}
}