MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Examples/NoModules/Chapter 13/Ex13_06/Ex13_06.cpp:18–61  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

16}
17
18int main()
19{
20 const double limit {99}; // Upper limit on Box dimensions
21 auto random { createUniformPseudoRandomNumberGenerator(limit) };
22
23 const size_t boxCount {20}; // Number of Box object to be created
24 std::vector<Box> boxes; // Vector of Box objects
25
26 // Create 20 Box objects
27 for (size_t i {}; i < boxCount; ++i)
28 boxes.push_back(Box{ random(), random(), random() });
29
30 size_t first {}; // Index of first Box object of pair
31 size_t second {1}; // Index of second Box object of pair
32 double minVolume {(boxes[first] + boxes[second]).volume()};
33
34 for (size_t i {}; i < boxCount - 1; ++i)
35 {
36 for (size_t j {i + 1}; j < boxCount; j++)
37 {
38 if (boxes[i] + boxes[j] < minVolume)
39 {
40 first = i;
41 second = j;
42 minVolume = (boxes[i] + boxes[j]).volume();
43 }
44 }
45 }
46
47 std::cout << "The two boxes that sum to the smallest volume are "
48 << boxes[first] << " and " << boxes[second] << '\n';
49 std::cout << std::format("The volume of the first box is {:.1f}\n",
50 boxes[first].volume());
51 std::cout << std::format("The volume of the second box is {:.1f}\n",
52 boxes[second].volume());
53 std::cout << "The sum of these boxes is " << (boxes[first] + boxes[second]) << '\n';
54 std::cout << std::format("The volume of the sum is {:.1f}", minVolume) << std::endl;
55
56 Box sum{ 0, 0, 0 }; // Start from an empty Box
57 for (const auto& box : boxes) // And then add all randomly generated Box objects
58 sum += box;
59
60 std::cout << "The sum of " << boxCount << " random boxes is " << sum << std::endl;
61}

Callers

nothing calls this directly

Calls 3

push_backMethod · 0.45
volumeMethod · 0.45

Tested by

no test coverage detected