#include <iostream>
using namespace std;
int main()
{
int arraySize;
cout<<"How many integers are you inputing? ";
cin>>arraySize;
int a,b;
cout<<"Input the range [a,b]? ";
cin>>a>>b;
cout<<"a= "<<a<<endl;
cout <<"b= "<<b<<endl;
if (arraySize>0)
{
if(a>b)
{
int temp1=a;
a=b;
b=temp1;
}
int *arrayName=new int [arraySize] {};
srand(time(nullptr));
for(int i=0; i<arraySize; ++i)
{
arrayName[i]=rand()%(b-a+1)+a;
}
cout<<"\nNumbers from the range"<<"["<<a<<","<<b<<"]\n";
for(int i=0; i<arraySize; ++i)
{
cout<<arrayName[i]<<"\t";
}
cout <<endl;
int counter =0;
int sum=0;
double average;
for(int i =0; i<arraySize; ++i)
{
if(arrayName[i]%2!=0)
{
++counter;
sum+=arrayName[i];
}
else
{
cout<<"The number is an even number"<<endl;
break;
}
}
average=sum/counter;
cout<< "The total of the odd is: " <<counter <<endl;
cout<< "The total sum of the odd is:" <<sum <<endl;
cout<< "The average numbers are:" <<average <<endl;
The only problem I see is that the use of time() on line 27 requires the <ctime> header.
PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post. http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
#include <iostream>
#include <utility>
#include <random>
usingnamespace std;
auto Rand {mt19937 {random_device {}()}};
int main() {
size_t arraySize {};
cout << "How many integers are you inputting? ";
cin >> arraySize;
if (arraySize > 0) {
int a {}, b {};
cout << "Input the range [a, b]? ";
cin >> a >> b;
if (a > b)
swap(a, b);
cout << "a = " << a << "\nb = " << b << '\n';
auto randNo {std::uniform_int_distribution<int> {a, b}};
size_t counter {};
double sum {};
for (size_t i = 0; i < arraySize; ++i) {
constauto n {randNo(Rand)};
cout << n << "\t";
if (n % 2) {
++counter;
sum += n;
}
}
constdouble average {sum / counter};
cout << "\nThe number of odd is: " << counter << endl;
cout << "The total sum of odd is: " << sum << endl;
cout << "The average of odd numbers is: " << average << endl;
}
}
How many integers are you inputting? 10
Input the range [a, b]? 5 25
a = 5
b = 25
14 23 21 5 17 18 16 16 16 9
The number of odd is: 5
The total sum of odd is: 75
The average of odd numbers is: 15