In this program create a one-dimensional array of n random integers from the range [a,b], where n, a,b are given by the user. compute and display how many positive numbers are in the array. list all the positive numbers from the array. some sample interaction with the program might look like this below.
Give the length of the array:10
give the range [a,b]
a=-3
b=15
Numbers from the range [-3,15]
-1 -2 2 -3 4 5 -3 6 7 -3
There are 5 positive numbers in this array:
2 4 5 6 7
#include <random>
#include <iostream>
#include <limits>
#include <cctype>
#include <string>
#include <vector>
int getInt(const std::string& prm)
{
int i {};
while ((std::cout << prm) && (!(std::cin >> i) || !std::isspace(std::cin.peek()))) {
std::cout << "Not an integer\n";
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return i;
}
int main()
{
constint numnos {getInt("How many integers: ")};
constint low {getInt("Low range number: ")};
constint high {getInt("High range number: ")};
std::cout << numnos << " Random numbers in the range [" << low << ", " << high << "]\n";
std::mt19937 rng(std::random_device {}());
std::uniform_int_distribution<int> distrib(low, high);
std::vector<int> vi(numnos);
int nopos {};
for (int& v : vi) {
nopos += ((v = distrib(rng)) > 0);
std::cout << v << ' ';
}
std::cout << "\nThere are " << nopos << " positive numbers in the array\n";
for (int v : vi)
if (v > 0)
std::cout << v << ' ';
std::cout << '\n';
}
How many integers: 20
Low range number: -10
High range number: 10
20 Random numbers in the range [-10, 10]
0 -4 5 0 7 -4 8 4 -8 -9 1 3 -6 4 4 -6 3 -1 10 7
There are 11 positive numbers in the array:
5 7 8 4 1 3 4 4 3 10 7