Question

In: Computer Science

C++ Vectors. Create a program do the following in the program: 1. declare an vector without...

C++

Vectors.

Create a program do the following in the program:

1. declare an vector without specifying the size

2. use push_back to add random integers between 100 and 999 to the vector

3. write a function that returns the smallest, largest, and average of the numbers in the vector

display the smallest, largest, and average of the numbers in the vector

Solutions

Expert Solution

output: smallest, largest, average in respective order

#include <bits/stdc++.h>
#include <cstdlib>
#include <ctime>
using namespace std;
class values{
    public:
        int smallest;
        int largest;
        int average;
};
values myfunc(vector<int> &v){
    auto n = v.size();
    values stats;
    stats.smallest = *min_element (v.begin(), v.end()); //stl function to find minimum element
    stats.largest = *max_element (v.begin(), v.end()); //stl function to find maximum element
    //accumulate does the sum of elements in vector and then to get avg we will devide it with vector size
    stats.average = accumulate( v.begin(), v.end(), 0.0) / n;
    return stats;
}
int main() {
        vector<int> v;
        int num;
    int count = 1000000; // number of elements we want to push in vector
    
    // This program will create different sequence of  
    // random numbers on every program run  
  
    // Use current time as seed for random generator 
    srand(time(0));

    //rand() % x generates the number between 0 and x [excluding x]
    // so for number between 100 and 999 we will add 101 to 
    while(count--) {
        num =  101 + rand() % 898;
        v.push_back(num);
    }
    
    values v_stats;
    v_stats = myfunc(v);
    
        cout << v_stats.smallest << "   " << v_stats.largest << "   " << v_stats.average;
        return 0;
}

Related Solutions

Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t...
Write a  program in C++ using a vector to create the following output. Declare a vector named  numbers    -  Don’t specify a size and don’t initialize with values. Starting with 2, pushing these into the back of the vector:   2, 4, 6, 8, 10 vector capacity (array size) changes and is dependent on the compiler. The size of the list is now 5. The vector capacity (array size) is 6. The back element is: 10 The front element is: 2 Now deleting the value at...
Write a program in c++ to do the following : (1) Declare an array a of...
Write a program in c++ to do the following : (1) Declare an array a of size 10 and three pointer variables p, q, and v. (2) Write a loop to fill array a with values 10, 20, 30, 40, 50, 60, 70, 80, 90, 100 (3) write following statement: p= &a[2]; q = &a[5]; i = *q - *p; cout<<“The value of i is”<< i; i = *p - *q; cout<<“The value of i is %d”<< i; 4) assign...
C++ Suppose the vector v = [4 -6 7]. Create three vectors: 1. p, which is...
C++ Suppose the vector v = [4 -6 7]. Create three vectors: 1. p, which is twice as long as v and points in the same direction as v 2. q, which has the same length as v and points in the opposite direction of v 3. r, which is three quarters the length of v and points in the same direction as v Print out the results of each vector calculation.
Create a function that takes a vector of vectors as an argument. Each inner vector has...
Create a function that takes a vector of vectors as an argument. Each inner vector has 2 elements. The first element is the numerator and the second element is the denominator. Return the sum of the fractions rounded to the nearest whole number. Examples: sum_fractions({{18, 13}, {4, 5}}) ➞ 2 sum_fractions({{36, 4}, {22, 60}}) ➞ 9 sum_fractions({{11, 2}, {3, 4}, {5, 4}, {21, 11}, {12, 6}}) ➞ 11 Notes Your result should be a number not string. Code in C++...
Rewrite your program for part 1. Do not declare the array globally, declare it in the...
Rewrite your program for part 1. Do not declare the array globally, declare it in the loop function. This now requires that you add two parameters to your fill array and print array functions. You must now pass the array name and array size as arguments, when the program calls these functions. The program has the same behavior as problem 1, but illustrates the difference between globally and locally declared variables. The program code for part 1 was: int Array[15]...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an...
C++ Program 1. Declare an integer static array a[ ] with 100 elements. 2. Declare an integer pointer p. 3. Let p pointing to the array a[ ]. 4. Use p (you have to use p) to put 0 into the first element of this array, 2 into the second element, 4 into the 3rd element, 6 into the 4th element, ... 198 into the 100th element of this array. 5. Use a (you have to use a) to display...
It's java file 1. declare an vector without specifying the size 2. use push_back to add...
It's java file 1. declare an vector without specifying the size 2. use push_back to add random integers between 100 and 999 to the vector 3. write a function that returns the smallest, largest, and average of the numbers in the vector 3. display the smallest, largest, and average of the numbers in the vector
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask...
Write C++ program to do the following: 1. Create integer array size of 10 2. Ask user input the values of the array's element using for loop 3. pass the array to void function. in void function do the following: a. Find the maximum of the array. b. Compute the element average c. Find out how many numbers are above the average d. Find out and print how many numbers are below the average e. find out how many numbers...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q...
Write this program in C++ language. Use the concept of structures. DO NOT use vectors. Q (4) Create a structure called time. Its three members, all type int, should be called hours, minutes, and seconds. Write a program that prompts the user to enter a time value in hours, minutes, and seconds. This should be in 12:59:59 format. This entire input should be assigned first to a string variable. Then the string should be tokenized thereby assigning the 1st token...
Write a program in c++ to do the following: 2. Create an array to hold up...
Write a program in c++ to do the following: 2. Create an array to hold up to 20 integers. 3. Create a data file or download attached text file (twenty_numbers.txt) that contains UP TO 20 integers. 4. Request the input and output file names from the user. Open the files being sure to check the file state. 5. Request from the user HOW MANY numbers to read from the data file, up to twenty. Request the number until the user...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT