Question

In C++ Create a vector of size 1000, fill it with random numbers, use algorithm/lambda to...

In C++

Create a vector of size 1000, fill it with random numbers, use algorithm/lambda to count how many odd numbers there are. Display only the odd numbers.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
#include <iostream>
#include <algorithm>
#include <functional>
#include <vector>
#include <cstdlib>
#include <ctime>

using namespace std;

int main() {
    srand(time(NULL));
    int size = 1000;
    vector<int> v;
    for (int i = 0; i < size; ++i) {
        v.push_back(rand() % 1000);
    }

    cout << "Odd numbers in vector are: ";
    for_each(v.begin(), v.end(), [](int n) {
        if (n % 2 == 1) {
            cout << n << " ";
        }
    });
    auto n = count_if(v.begin(), v.end(), bind(logical_not<bool>(), bind(modulus<int>(), placeholders::_1, 2)));
    cout << "\n\nNumber of odd numbers is " << n << endl;
    cout << endl;
    return 0;
}

Add a comment
Know the answer?
Add Answer to:
In C++ Create a vector of size 1000, fill it with random numbers, use algorithm/lambda to...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Use the rbinom() function to create a vector of 1000 random observations from the binomial distribution...

    Use the rbinom() function to create a vector of 1000 random observations from the binomial distribution with n=100 and probability of success is equal to 0.4. Calculate the mean and standard deviation statistics for this vector of random draws using the mean() and sd() commands. How do these numbers compare the mean and standard deviation of the binomial distribution when  and ? If they are different, why? Make a histogram of this vector using the hist() command.

  • C languge please ! 1. Using the random number program, fill up an array with 100...

    C languge please ! 1. Using the random number program, fill up an array with 100 random numbers between -30 and 30 and display it. Find out how many of those numbers in the array are positive, negative, even and odd. Display the results. Make sure you cover the special case. Your entire code should have only 1 FOR loop. You will not receive any credit if you use more than 1 loop. (50 points - 5pts for commenting and...

  • Create a heapsort function in C++ using priority_queue in a vector. The vector contains 20 numbers...

    Create a heapsort function in C++ using priority_queue in a vector. The vector contains 20 numbers with varying numbers between 0-20 with some duplicates. From what I understand this should only take around 6 lines with the brackets. The heapsort just needs to put the numbers in order from 0-20 using the priority queue. priority queue is implemented using #include <queue> and the function also includes <algorithm> . Use this as a base: template void heapsorter(std::vector& vec) { }

  • in c++ 3- Create a vector of integers and do the following: a- Create fillVector function...

    in c++ 3- Create a vector of integers and do the following: a- Create fillVector function that uses the push_back method provided by the vector class to fill the vector with random numbers (use rand). b- Now implement the void addNumbers(vector &data) method that adds 10 numbers to the data vector (use a for loop). c- Create a new function void print_even(const vector& data) that prints all of the elements in the data vector that are stored at an even...

  • Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 -...

    Task 1 Main Function: Declare and fill an array with 1000 random integers between 1 - 1000. You will pass the array into functions that will perform operations on the array. Function 1: Create a function that will output all the integers in the array. (Make sure output is clearly formatted. Function 2: Create a Function that will sum and output all the odd numbers in the array. Function 3: Create a Function that will sum and output all the...

  • Write a C++ program to fill a vector with 5000 random numbers. Then display: The smallest...

    Write a C++ program to fill a vector with 5000 random numbers. Then display: The smallest number The largest number The number of odd values The number of even values The total of the values The average of the values Ask the user for an integer and display the subscript of the cell where it was found or NOT FOUND! if it isn’t found. There is a file called "Activity 25 Sort functions.zip" under Materials in this week’s module. Download...

  • C++ 2) Create a program that does all of the following: Create 100 random int numbers...

    C++ 2) Create a program that does all of the following: Create 100 random int numbers from 48-84 Puts them in an array called height Counts how many of each there were Sorts the numbers from high to low If short 48-60, average- 61-70, tall- 71-74 and big is any them above 84, count how many is each group Print the results from the last part as a bar graph short average tall big

  • Generate an array of random numbers in C++ 1)The size of the array is defined by...

    Generate an array of random numbers in C++ 1)The size of the array is defined by the user 2) The application is to take every odd number and find the mean. Program has to use structured programing and pointers

  • Write a c++ code into the given code  to find composite numbers from the given random number...

    Write a c++ code into the given code  to find composite numbers from the given random number list. The composite numbers is only counted once if there is a repeated number. I need to use this code and add on a code to find if the numbers generated is a composite function. Please help #include <cmath> #include <cstdlib> #include <ctime> #include <iostream> #include <vector> using namespace std; int main() {    srand(time(NULL)); int size_of_list = 0; // the number of random...

  • Please use the algorithm to generate 1000 random numbers in JAVA. (Print every 50th) integer array...

    Please use the algorithm to generate 1000 random numbers in JAVA. (Print every 50th) integer array lDon real array (aiim eo ← any integer such that l < 10 < 231-1 for i = 1 to n do 4 ← remainder when /.til IS divided by 2. 31-1 231 end for

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT