8.5. Structures as parameters

You can pass structures as parameters in the usual way. For example,

void print_point (point p) {
  cout << "(" << p.x << ", " << p.y << ")" << endl;
}

print_point takes a point as an argument and outputs it in the standard format. If you call print_point (blank), it will output (3, 4).

The active code below uses the print_point function. Run the code to see the output!

As a second example, we can rewrite the distance function from Section [distance] so that it takes two points as parameters instead of four doubles.

double distance (point p1, point p2) {
  double dx = p2.x - p1.x;
  double dy = p2.y - p1.y;
  return sqrt (dx*dx + dy*dy);
}

The active code below uses the updated version of the distance function. Feel free to modify the code!

Construct a function that takes in three point structures and prints the average of the x coordinates and the average of the y coordinates as a coordinate. Find the x average before the y average.

You have attempted of activities on this page