In: Computer Science
Assume the following functions have already been defined, write a main() using c++ that uses them to fill a vector with random integers, remove the smallest and largest integers from the vector, save the result in a file called "trimmed.txt"
void fillRandom(vector & nums, int howMany); // fill with specified number of integers
int minValue(vector nums); // return smallest value in vector
int maxValue(vector <;int> nums); // return largest value in vector
void writeFile(string outFileName, vector nums ); // writes numbers into file
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <vector>
#include<time.h>
using namespace std;
void fillRandom(vector<int> & nums, int howMany)
{
srand(time(0));
for(int i=0;i<howMany;i++)
{
nums.push_back(rand()%1000);
}
}
int minValue(vector<int> nums)
{
int min=nums[1];
for(int i=2;i<=nums.size();i++)
{
if(nums[i]<min)
min=nums[i];
}
return min;
}
int maxValue(vector<int> nums)
{
int max=nums[1];
for(int i=2;i<=nums.size();i++)
{
if(nums[i]>max)
max=nums[i];
}
return max;
}
void writeFile(string outFileName, vector<int> nums
)
{
ofstream f1;
f1.open(outFileName);
for(int i=1;i<nums.size();i++)
{
f1<<nums[i]<<endl;
}
}
int main()
{
vector<int> nums;
fillRandom(nums,10);
cout<<"Minimum = "<<minValue(nums)<<endl;
cout<<"Maximum = "<<maxValue(nums)<<endl;
writeFile("sample.txt",nums);
return 0;
}