help me with the modification of the code

#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;


return 0;
}
}
Last edited on
Please format your post for readability.
https://www.cplusplus.com/articles/jEywvCM9/
What modification do you need help with?

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.
the modification of the code


You don't say what modification - but consider these modifications:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <utility>
#include <random>
using namespace 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) {
			const auto n {randNo(Rand)};

			cout << n << "\t";

			if (n % 2) {
				++counter;
				sum += n;
			}
		}

		const double 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


Topic archived. No new replies allowed.