Question

In: Computer Science

Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing...

Write a function named “compileStats” that has 4 parameters: a vector of integers, an integer representing the smallest value contained in the vector, an integer representing the largest value contained in the vector, and a double that represents the average of the values in the vector. The compileStats function will not “return” a value, it will set the Pass By Reference parameters to the correct values (smallest, largest, and average).

Use the following main function in driver1b.cpp as a starting point for testing your function.

int main() {

int min = 0;

int max = 0;

double avg = 0.0;

vector<int> test1{ 12, 15, 543, 1024 };

compileStats(test1, min, max, avg);

cout << "For test 1: " << min << " " << max << " " << avg << endl;

vector<int> test2{ -8 };

compileStats(test2, min, max, avg);

cout << "For test 2: " << min << " " << max << " " << avg << endl;

vector<int> test3{ -40, 39, -39, 40, 38, -38, 0, 0 };

compileStats(test3, min, max, avg);

cout << "For test 3: " << min << " " << max << " " << avg << endl;

}

// You may not use any “built-in” C++ classes other than iostream and vector.

Solutions

Expert Solution


/*
 *  C++ Program to find out min max average
 */

#include <iostream>
#include <vector>
using namespace std;

void compileStats(vector<int> vec, int &min, int &max, double &avg);

int main()
{  
  int min = 0;
  int max = 0;
  double avg = 0.0;
  
  vector<int> test1{12, 15, 543, 1024};
  compileStats(test1, min, max, avg);
  cout << "For test 1: " << min << " " << max << " " << avg << endl;
  
  vector<int> test2{-8};
  compileStats(test2, min, max, avg);
  cout << "For test 2: " << min << " " << max << " " << avg << endl;
  
  vector<int> test3{-40, 39, -39, 40, 38, -38, 0, 0};
  compileStats(test3, min, max, avg);
  cout << "For test 3: " << min << " " << max << " " << avg << endl;
  
  return 0;
}

void compileStats(vector<int> vec, int &min, int &max, double &avg)
{
  int sum = 0;
  min = vec[0];
  max = vec[0];

  for (int i = 0; i < vec.size(); i++)
  {
    if (min > vec[i])
      min = vec[i];
    
    if (max < vec[i])
      max = vec[i];
    
    sum += vec[i];
  }

  avg = (double)sum/vec.size();
}

/*  Program ends here */

Related Solutions

Write a function in R named counts. This function should take as parameters a numeric vector...
Write a function in R named counts. This function should take as parameters a numeric vector x and also a number indicating a number of bins n. The function will consider the range [min(x),max(x)], and then consider a parti- tion of this interval into n non-overlapping equally sized half open intervals: I1 = [min(x),b1),I2 = [b1,b − 2),...,In = (bn−1,max(x)]. Note that since the intervals are equally sized, the value of bi is constrained. The function will then return a...
Write a recursive function named multiply that takes two positive integers as parameters and returns the...
Write a recursive function named multiply that takes two positive integers as parameters and returns the product of those two numbers (the result from multiplying them together). Your program should not use multiplication - it should find the result by using only addition. To get your thinking on the right track: 7 * 4 = 7 + (7 * 3) 7 * 3 = 7 + (7 * 2) 7 * 2 = 7 + (7 * 1) 7 *...
JAVA PROGRAMMING RecallNoIF! Write a program named RecallNoIF that reads an integer representing a year. (In...
JAVA PROGRAMMING RecallNoIF! Write a program named RecallNoIF that reads an integer representing a year. (In this case there is no prompt from the program.) The program prints out RECALL if the year was 2004, 2010 or 2015 and NO RECALL otherwise. CONSTRAINT: Nowhere in the program is an if statement used. REMINDER: the program's output is shown here in bold; the user's data entry is shown here in italics. Sample Interactive Run 1: 2010 RECALL Sample Interactive Run 2:...
write a function named as cubeCalculator that takes an integer pointer as function and return its...
write a function named as cubeCalculator that takes an integer pointer as function and return its cube value , you are required to compute the cube of a number using pointer notation , return the result as an integer value , use c++
Write a function called draw_card. It takes no arguments and returns an integer representing the value...
Write a function called draw_card. It takes no arguments and returns an integer representing the value of a blackjack card drawn from a deck. Get a random integer in the range 1 to 13, inclusive. If the integer is a 1, print "Ace is drawn" and return 1. If the integer is between 2 and 10, call it x, print "<x> is drawn" and return x (print the number, not the string literal "<x>"). If the number is 11, 12,...
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++, using pass by reference
Write a function named findIndex that takes an array of integers, the number of elements in...
Write a function named findIndex that takes an array of integers, the number of elements in the array, and two variables, such that it changes the value of the first to be the index of the smallest element in the array, and changes the value of the second to be the index of the largest element in the array. Please complete this in C++
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and...
"sum_between" function Write a function named "sum_between" that receives 2 parameters - "start" (an int) and "end" (an int). It should return the sum (total) of all of the integers between (and including) "start" and "end". If "end" is less than "start", the function should return -1 instead. e.g. if you give the function a start of 10 and an end of 15, it should return 75 (i.e. 10+11+12+13+14+15)
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should...
Write a function, named isMultipleOfFive that accepts integer argument. When the function is called, it should display if the argument "is a multiple of 5" or "is not a multiple of 5".
Write a program that contains the following Write a function copies with two int parameters, named...
Write a program that contains the following Write a function copies with two int parameters, named n and x. It should dynamically allocate a new array of size n.  The function should then copy the value in x to every position in the array. The function should return a pointer to the new array. In the main function, call the copies function from part 1. with size 5 and value -1.  Output the resulting array to the screen, and deallocate the array....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT