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

Function main

Examples/NoModules/Chapter 17/Ex17_02A/Ex17_02A.cpp:10–50  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

8#include <typeinfo> // For use of typeid()
9
10int main()
11{
12 try
13 {
14 try
15 {
16 const size_t size {21}; // Number of array elements
17 const int start {-10}; // Index for first element
18 const int end {start + static_cast<int>(size) - 1}; // Index for last element
19
20 Array<double, start> values {size}; // Define array of double values
21
22 for (int i {start}; i <= end; ++i) // Initialize the elements
23 values[i] = i - start + 1;
24
25 std::cout << "Sums of pairs of elements: ";
26 size_t lines {};
27 for (int i {end}; i >= start; --i)
28 {
29 std::cout << (lines++ % 5 == 0 ? "\n" : "")
30 << std::format("{:5g}", values[i] + values[i-1]);
31 }
32 }
33 catch (const std::out_of_range& ex)
34 {
35 std::cerr << "\nout_of_range exception object caught! " << ex.what() << std::endl;
36 }
37
38 // Create array of Box objects
39 const int numBoxes {9};
40 Array<Box, -numBoxes / 2> boxes { static_cast<size_t>(numBoxes) };
41
42 for (int i { -numBoxes / 2 }; i <= numBoxes/2 + numBoxes%2; ++i)
43 std::cout << std::format("Volume of Box[{}] is {}\n", i, boxes[i].volume());
44 }
45 catch (const std::exception& ex)
46 {
47 std::cerr << typeid(ex).name() << " exception caught in main()! "
48 << ex.what() << std::endl;
49 }
50}

Callers

nothing calls this directly

Calls 2

whatMethod · 0.45
volumeMethod · 0.45

Tested by

no test coverage detected