Centroid of a Triangle
Define two structured types, one for a two-dimensional Point (floating point coordinates named x
and y), the other for a triangle consisting of 3 Points. Have main declare a Triangle and then fill
in its vertex Points with random floating point numbers between 0 and 1. Write a second
function named centroid with a single input (parameter), a Triangle. The purpose of the
centroid function is, surprise, surprise, to compute and return the centroid of a triangle.
Have main print out the vertices of the Triangle and then print out the results of the call to
centroid. Here's what my printout looks like:
The triangle vertices are [0.69, 0.51], [0.59, 0.55], and [0.38, 0.26]
The centroid is [0.55, 0.44]
Notes:
When debugging your centroid function, you obviously may use printf as
necessary (that's how we debug, right?); but remove all the
debugging printf statements from centroid before you turn in your final code. In
the final version, only main may call printf to produce the printout shown above.
Use no global variables – this assignment is about passing structures into and back from a
function. Note: Global definitions for your structures will be required for this program
(why?); but global variables are forbidden.
Don't make the parameter for the centroid function an array, make it a structure (a
triangle) – you already know how to pass arrays into functions; the point here is passing
and returning structures. You can put an array of 3 Points inside the Triangle structure, if
you wish, or use three individually named components (e.g., vertexA, vertexB,
and vertexC); it's your call, It will probably be slightly simpler if you put them in an
array, called something clever like, say vertex[3], but, as I said, it's your call. But
the centroid function itself must be defined as having only a single parameter and that
must be a triangle structure. And the value returned by this function must be also be a
structured value (but it's a point, not a triangle, obviously). Note: I want the centroid
function to return a point as its return value to main; do not have the centroid function
produce a side-effect in main --- you know how to do that now (I hope;-) but that's not
what I want for this assignment.
You may wish to create a getRandomPoint function that returns a Point and then use
that in main to populate the values in the three Points of the Triangle. Like the
centroid function, it would return a point, using the return-by-value handling of structs.
What to turn in:
Upload the typescript file with the following
cat –n of your .h file(s) if you used any
cat –n of your .c file
gcc of your .c file
a.out (or whatever you called your executable
Upload your .c (and .h files if you created any)
Purchase answer to see full
attachment