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...
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.
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]...
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++...
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 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...
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...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) =...
In C++ Use vectors instead of linked lists Create a Hash table program using H(key) = key%tablesize with Chaining and Linear probing for a text file that has a list of 50 numbers Ask the user to enter the file name, what the table size is, and which of the two options they want to use between chaining and linear probing
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2....
C++ Code Write a program that performs the following: a) Declare long variables value1 and value2. The variable value1 has been initialized to 200000. Declare the variable longPtr to be a pointer to an object of type long. b) Assign the address of variable value1 to pointer variable longPtr. c) Display the value of the object pointed to by longPtr. d) Assign the value of the object pointed to by longPtr to variable value2. e) Display the value of value2....
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT