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

Function main

Examples/NoModules/Chapter 17/Ex17_02/Ex17_02.cpp:8–48  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 2

whatMethod · 0.45
volumeMethod · 0.45

Tested by

no test coverage detected