https://rentry.org/PPP2_p141

// Code derived from Stroustrup's PPP2 book
// § 5.5 Run-time errors
//  -and beginning on p 141

#include <iostream>

using std::cout;

// calculate area of a rectangle
int area(int length, int width) { return length * width; }

// calculate area within frame
int framed_area(int x, int y) { return area(x - 2, y - 2); }

int main()
{
  int x = -1;
  int y = 2;
  int z = 4;

  // . . .

  int    area1 = area(x, y);
  int    area2 = framed_area(1, z);
  int    area3 = framed_area(y, z);
  double ratio = double(area1) / area3;  // convert to double to get
                                         // floating-point division

  cout << x << '\n'      //
       << y << '\n'      //
       << z << '\n'      //
       << area1 << '\n'  //
       << area2 << '\n'  //
       << area3 << '\n'  //
       << ratio << '\n';
}

build & run:

g++ -std=c++20 -O2 -Wall -pedantic ./ch_05/main_p141.cpp && ./a.out

PrevUpNext

Edit
Pub: 20 Feb 2023 11:23 UTC
Edit: 29 Apr 2023 09:06 UTC
Views: 566