jeudi 4 avril 2019

Inquire about using a random integer as the size of a vector

so I have trouble remembering how to call upon random numbers in C++. I'm not sure how to, so if someone could remind me that would be fantastic. I'm trying to make a vector with a random size filled with random integers. Here is my following code:

#include <cstdlib>
#include <iostream>
#include <vector>
    int main()
{
  int randSize = rand();
  int sum = 0;
  int product = 1;
  std::vector<int> numbers(randSize);
  int output;
  for (int i = 0; i < numbers.size(); i++)
  {
    int randNum = rand();
    numbers[i] = randNum;
    output = numbers[i]&2;
    if (output == 0)
    {
      sum += numbers[i];
    }
    else
    {
  product = product * numbers[i];
    }




  }

  std::cout << "The sum of even numbers is " << sum << "\n";
  std::cout << "The product of off numbers is " << product << "\n";



}




Aucun commentaire:

Enregistrer un commentaire