Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

A struct is a collection of data that can be treated as a single new data structure but whose individual components can be accessed. A

A struct is a collection of data that can be treated as a single new data structure but whose individual
components can be accessed. A struct can contain almost anything include arrays and other structs.
Heck, when we learn about pointers, well see node structs that can have pointers to
For example, a student struct could be defined as so:
struct student
{
int id;
double gpa;
int coffeesPerDay;
};
A student could then be created and modified like so:
student stu1;
stu1.id =12345;
stu1.gpa =4.0;
stu1.coffeesPerDay =4;
// Then later you can use these data
if (stu1.coffeesPerDay >5){......}
1. MyPoint
Make a point structure that has two integers: x and y. This structure will represent a position in 2D
space.
2. RGB Colour
For those of you that havent encountered this, colours on a computer are often represented as Red
Green and Blue values (hence RGB). Each of these three colors are between 0 and 255(so 1 byte) and
thus can be represented using a char each (which are 1 byte). Make an myRGB struct that contains 3
chars: red, green and blue.
3. Triangle
Now make a triangle struct that constraints 3 MyPoints and an RGB color. This struct defines a coloured
triangle in 2D space.
Make two triangles in main. For the second triangle, set the triangle values when the triangle is created.
If I wanted to make a MyPoint at position (5,10) I might write the following:
MyPoint pt ={5,10};
4. Polygon
An n-sided polygon is any convex shape with n sides. In the images above the number of sides is 4 and
6....but lets keep things simple by not checking the correctness of the points that make up the polygon.
Make a polygon struct that is an array of 20 MyPoint points, a color, and an int number of points
stored. This means that all Polygon structures have an array of 20 points but only the first n of these
points have useful information. Your polygon cannot have more than 20 sides.
Write a function createPolygon that takes the number of sides as a parameter and then (provided that
number is between 3 and 20) creates a polygon structure with that many sides. Scanf and printf
statements in a for loop should be used to get each point of the polygon (maybe only test triangles and
quadrilaterals given how long data input will take). The function then returns this newly created
polygon
Write another function printPolygon which takes a polygon as an input parameter and outputs (using
printf) information about the polygon such as each of the points and the color. The function has a
return type of void.
5. Rectangle
Finally, create a myRect struct that is made up of myPoint p1, myPoint p2, and a myRGB colour. The
two points define the area of the rectangle.
Now write a function called calcArea that takes a myRect struct as an input parameter and returns the
(integer) area of the rectangle. Tutorial 7- Structs
A struct is a collection of data that can be treated as a single new data structure but whose individual
components can be accessed. A struct can contain almost anything include arrays and other structs.
Heck, when we learn about pointers, we'll see node structs that can have pointers to other nodes.
For example, a student struct could be defined as so:
// Then later you can use these data
if (stu1.coffeesPerDay >5).
MyPoint
Make a point structure that has two integers: x and y. This structure will represent a position in 2D
space.
RGB Colour
For those of you that haven't encountered this, colours on a computer are often represented as Red
Green and Blue values (hence RGB). Each of these three colors are between 0 and 255(so 1 byte) and
thus can be represented using a char each (which are 1 byte). Make an myRGB struct that contains 3
chars: red, green and blue.
Triangle
Now make a triangle struct that constraints 3 MyPoints and an RGB color. This struct defines a coloured
triangle in 2D space.
Make two triangles in main. For the second triangle, set the triangle values when the triangle is created.
If I wanted to make a MyPoint at position (5,10) I might write the following:
MyPoint pt ={5,10}; Polygon
An n-sided polygon is any convex shape with n sides. In the images above the number of sides is 4 and
...but let's keep things simple by not checking the correctness of the points that make up the polygon.
Make a polygon struct that is an array of 20 MyPoint points, a color, and an int number of points
stored. This means that all Polygon structures have an array of 20 points but only the first n of these
points have useful information. Your polygon cannot have more than 20 sides.
Write a function createPolygon that takes the number of sides as a parameter and then (provided that
number is between 3
image text in transcribed

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

18. Men and women each have different roles to play in society.

Answered: 1 week ago