.

...
Last edited on
Formatted and slightly changed:

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
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <ctime>
#include <fstream>

using namespace std;

int main() {
	int applesPicked[50]{};
	int counter{};
	int counter2{};
	int sum{};

	srand(static_cast<unsigned>(time(nullptr)));

	ofstream oData("apple_trip.txt");

	for (int i = 0; i < 50; ++i)
		oData << rand() % 104 + 2 << '\n';

	oData.close();

	ifstream iData("apple_trip.txt");

	for (int i = 0; i < 50; ++i)
		iData >> applesPicked[i];

	for (int i = 0; i < 50; ++i)
		if (applesPicked[i] < 25)
			++counter;

	cout << "The amount of people that picked less than 25 apples is: " << counter << '\n';

	for (int i = 0; i < 50; ++i)
		if (applesPicked[i] > 79)
			++counter2;

	cout << "The amount of numbers between 80 and 100 is: " << counter2 << '\n';

	for (int i = 0; i < 50; ++i)
		sum += applesPicked[i];

	const int average{ sum / 50 };

	cout << "The average numbers of apples picked is: " << average << "\n\n";

	if (average > 25)
		for (int i = 0; i < 50; ++i)
			if (applesPicked[i] < 10)
				applesPicked[i] = 0;

	for (int i = 0; i < 50; ++i) {
		cout << applesPicked[i] << ' ';

		if ((i + 1) % 10 == 0)
			cout << '\n';
	}
}


Each part can be extracted into a function. Eg as an example using a couple of functions:

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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>

using namespace std;

constexpr size_t Sze{ 50 };

void init(size_t sze) {
	ofstream oData("apple_trip.txt");

	for (size_t i = 0; i < sze; ++i)
		oData << rand() % 104 + 2 << '\n';
}

void read(unsigned applesPicked[], size_t sze) {
	ifstream iData("apple_trip.txt");

	for (size_t i = 0; i < sze; ++i)
		iData >> applesPicked[i];
}

int main() {
	unsigned applesPicked[Sze]{};
	size_t counter{};
	size_t counter2{};
	size_t sum{};

	srand(static_cast<unsigned>(time(nullptr)));

	init(Sze);
	read(applesPicked, Sze);

	for (size_t i = 0; i < Sze; ++i)
		if (applesPicked[i] < 25)
			++counter;

	cout << "The amount of people that picked less than 25 apples is: " << counter << '\n';

	for (size_t i = 0; i < Sze; ++i)
		if (applesPicked[i] > 79)
			++counter2;

	cout << "The amount of numbers between 80 and 100 is: " << counter2 << '\n';

	for (size_t i = 0; i < Sze; ++i)
		sum += applesPicked[i];

	const auto average{ (sum + 0.0) / Sze };

	cout << "The average numbers of apples picked is: " << average << "\n\n";

	if (average > 25)
		for (size_t i = 0; i < Sze; ++i)
			if (applesPicked[i] < 10)
				applesPicked[i] = 0;

	for (size_t i = 0; i < Sze; ++i) {
		cout << applesPicked[i] << ' ';

		if ((i + 1) % 10 == 0)
			cout << '\n';
	}
}

Last edited on
values between 2 and 105

Is that "between"
inclusive [2,105] as in 2<=x<=105, std::uniform_int_distribution<int> distribution(2,105); or
exclusive (2,105) as in 2<x<105, std::uniform_int_distribution<int> distribution(3,104);?

Either way, it is not rand() % 100,
as in std::uniform_int_distribution<int> distribution(0,99);
Well spotted! I didn't read the description. Code changed above for inclusive.
Last edited on
Topic archived. No new replies allowed.