#include <iostream>
#include <ctime>
#include <fstream>
usingnamespace 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];
constint 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:
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <ctime>
usingnamespace 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];
constauto 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';
}
}
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);