Question

In: Computer Science

C++ For this question alone, put in everything (headers and so on) you need to make...

C++

For this question alone, put in everything (headers and so on) you need to make it compile. Assume the following functions have already been defined, write a main() 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

Solutions

Expert Solution

Below is the complete C++ solution. If you face any difficulty while understanding the solution, Please let me know in the comments.

Code:

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <ostream>
#include <bits/stdc++.h>
#include <vector>
#include <string>

using namespace std;

// fill with specified number of random integers in vector
void fillRandom(std::vector<int> &nums, int howMan) {
for(int i=0;i<howMan;i++) {
int num= rand();
nums.push_back(num);
}
}

// return smallest value in vector
int minValue(std::vector<int> nums) {
  
// find the min value in the vector
int minVal = *min_element(nums.begin(), nums.end());
return minVal;
}


// return largest value in vector
int maxValue(std::vector<int> nums) {
  
// find the max value in the vector
int maxVal = *max_element(nums.begin(), nums.end());
return maxVal;
}

// Method to write the vector elements in text outFileName
// after removing the minimum and maximum value
void writeFile(string outFileName, std::vector<int> nums) {
std::ofstream outFile(outFileName);
  
// get the min and max value of vector
int min = minValue(nums);
int max = maxValue(nums);
  
// traverse the vector and put the elements excluding
// min and max in trimmed.txt file
for (const auto &num : nums)
if(num != min && num != max)
outFile << num << "\n";
}

int main() {
  
// declare vector
std::vector<int> nums ;
int howMany = 10;
fillRandom(nums,howMany);
  
string outFileName = "trimmed.txt";
writeFile(outFileName , nums);

return 0;
}

Output:


Related Solutions

C++ For a job, you need to schedule everything in life. There is personal life, hobbies...
C++ For a job, you need to schedule everything in life. There is personal life, hobbies and lots of other things, and work. You have to manage your time well. For your job, you have to work on projects early and probably every day. How much time would you need to spend to keep up with your work when you have other things in your life? To solve this problem, write a C++ program to help you figure out how...
When you are using Tally, everything you need is right there when you need it. You...
When you are using Tally, everything you need is right there when you need it. You can edit vouchers from within the Balance Sheet, change the Stock groups from within Stock Summary and so much more. It just operates the way you expect it to; this is such a delight to use, that you would almost feel like it’s made for you. Analyse the statement from your own experiences with the Tally software. (200 Words)
I need a short and clear opinion so I can understand the question and make my...
I need a short and clear opinion so I can understand the question and make my own opinion Writing in the 19th century, the British economist Thomas Malthus argued that the law of diminishing marginal returns implied eventual misery for humanity because, in the case of agriculture, there is a fixed amount of available land and additional labor would only yield ever smaller increases in food production. The inevitable result, as Malthus saw it, was that population growth would eventually...
You need to accumulate $10,000. To do so, you plan to make deposits of $1,250 per...
You need to accumulate $10,000. To do so, you plan to make deposits of $1,250 per year - with the first payment being made a year from today - into a bank account that pays 15% annual interest. Your last deposit will be less than $1,250 if less is needed to round out to $10,000. How many years will it take you to reach your $10,000 goal? Do not round intermediate calculations. Round your answer up to the nearest whole...
a) You went on spring vacation and missed school so you need to make up your...
a) You went on spring vacation and missed school so you need to make up your homework. You are missing 4 math assignments and 2 science assignments and 3 social studies assignments. What is the probability that the assignments from each class will be completed together (i.e. all of one class, then all of another class, then all of another class)? Round to 4 decimals. b) A family is planning on having 5 children. A baby food company told the...
You need to accumulate $10,000. To do so, you plan to make deposits of $1,250 peryear—...
You need to accumulate $10,000. To do so, you plan to make deposits of $1,250 peryear— with the first payment being made a year from today —into a bank account that pays 12% annual interest .Your last deposit will be less than $1,250 if less is needed to round out to $10,000. How many years will it take you to reach your $10,000 goal ,and how large will the last deposit be?
You need to accumulate $10,000. To do so, you plan to make deposits of $1,950 per...
You need to accumulate $10,000. To do so, you plan to make deposits of $1,950 per year - with the first payment being made a year from today - into a bank account that pays 6.5% annual interest. Your last deposit will be less than $1,950 if less is needed to round out to $10,000. How many years will it take you to reach your $10,000 goal? Do not round intermediate calculations. Round your answer up to the nearest whole...
You need to accumulate $10,000. To do so, you plan to make deposits of $1,200 per...
You need to accumulate $10,000. To do so, you plan to make deposits of $1,200 per year - with the first payment being made a year from today - into a bank account that pays 7.6% annual interest. Your last deposit will be less than $1,200 if less is needed to round out to $10,000. How many years will it take you to reach your $10,000 goal? Round your answer up to the nearest whole number. How large will the...
Lets make a Deal c# simulation? The steps that you will need to complete this homework...
Lets make a Deal c# simulation? The steps that you will need to complete this homework is: //display and output to the screen telling the player that they can win a car with exactly $1000 //simulate the rolling of the die for $100, $200 or $300 //show the player the amount they have just rolled //show the player their total amount //check if total amount has not exceeded $1000. If exceeded, game over (tell player they lost) //if total is...
You now know everything you need to know to be an informed critic of the Affordable...
You now know everything you need to know to be an informed critic of the Affordable Care Act. Based on all that you have learned, how does the Act measure up to our three goals of 1) increasing quality of care, 2) increasing access to care and 3) keeping costs in check? In your answer, try not to veer into the political, but stay objective and non- partisan. Be specific about the provisions you’re referencing and the economic principle that’s...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT