Question

In: Computer Science

Assume the following functions have already been defined, write a main() using c++ that uses them...

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

Solutions

Expert Solution

#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;
}


Related Solutions

In C++ Prototype your functions above "main" and define them below "main"; Write a program that...
In C++ Prototype your functions above "main" and define them below "main"; Write a program that uses two identical arrays of at least 20 integers. It should call a function that uses the bubble sort algorithm to sort one of the arrays in ascending order. The function should keep count of the number of exchanges it makes. The program then should call a function that uses the selection sort algorithm to sort the other arrays. It should also keep count...
C++ Need to add the following functions to my templatized class linked list. (main is already...
C++ Need to add the following functions to my templatized class linked list. (main is already set to accommodate the functions below) --void destroy_list () deletes each node in the list, and resets the header to nullptr --bool search_list (key value) searches the list for a node with the given key. Returns true if found, false if not. --bool delete_node (key value) deletes the node which contains the given key. If there is more than one node with the same...
Using the string functions below, write new functions to do the following, and test them in...
Using the string functions below, write new functions to do the following, and test them in your main() function: Determine whether the first or last characters in the string are any of the characters a, b, c, d, or e. Reverse a string Determine whether a string is a palindrome (spelled the same way forward or backward FUNCTIONS REFERENCE: string myString = "hello"; // say we have a string… // … we can call any of the following // string...
Assume we have already defined two classes, ClickableShape and Pentagon. Using only the extends functionality of...
Assume we have already defined two classes, ClickableShape and Pentagon. Using only the extends functionality of Java, can we create a new class ClickablePentagon?
This is for C++ Assuming the value of a is already defined elsewhere, convert the following...
This is for C++ Assuming the value of a is already defined elsewhere, convert the following code snippet from using a WHILE loop to using a DO-WHILE loop. Please paste in the modified code in the text area below. int k = 1; while( k <= 8 ) { a = a * k; k++; }
Write a program that uses the defined structure and all the above functions. Suppose that the...
Write a program that uses the defined structure and all the above functions. Suppose that the class has 20 students. Use an array of 20 components of type studentType. Other than declaring the variables and opening the input and output files, the function main should only be a collection of function calls. The program should output each student’s name followed by the test scores and the relevant grade. It should also find and print the highest test score and the...
Write a C++ program which consists of several functions besides the main() function. The main() function,...
Write a C++ program which consists of several functions besides the main() function. The main() function, which shall ask for input from the user (ProcessCommand() does this) to compute the following: SumProductDifference and Power. There should be a well designed user interface. A void function called SumProductDifference(int, int, int&, int&, int&), that computes the sum, product, and difference of it two input arguments, and passes the sum, product, and difference by-reference. A value-returning function called Power(int a, int b) that...
Please write the following swap functions and print code in main to show that the functions...
Please write the following swap functions and print code in main to show that the functions have adequately . Your code should, in main(), print the values prior to being sent to the swap function. In the swap function, the values should be swapped and then in main(), please print the newly swapped values. 1) swap integer values using reference parameters 2) swap integer values using pointer parameters 3) swap pointers to integers - you need to print the addresses,...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a...
*****For C++ Program***** Overview For this assignment, write a program that uses functions to simulate a game of Craps. Craps is a game of chance where a player (the shooter) will roll 2 six-sided dice. The sum of the dice will determine whether the player (and anyone that has placed a bet) wins immediately, loses immediately, or if the game continues. If the sum of the first roll of the dice (known as the come-out roll) is equal to 7...
Intro C++ Programming Chapter 6 Functions You have been tasked to write a new program for...
Intro C++ Programming Chapter 6 Functions You have been tasked to write a new program for the Research Center's shipping department. The shipping charges for the center are as follows: Weight of Package (in kilograms)                Rate per mile Shipped 2 kg or less                                                      $0.05 Over 2 kg but no more than 6 kg    $0.09 Over 6 kg but not more than 10 kg    $0.12 Over 10 kg    $0.20 Write a function in a program that asks for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT