Skip to main content

Section 26.5 Dynamic Arrays of Structures: Pentagon

Subsection 26.5.1 Declare Vertices

Activity 26.6.

Use the function createPolygon to create a pentagon (5 vertices) with vertices (0,0), (1,0), (2,1), (1,3), (0,1).

Compile and run the code below in your own C environment.

#include <stdio.h>

typedef struct {
   float x;
   float y;
} point_t;

void printPoint(point_t z);
void initializePoint(point_t *ptr, float x, float y);

int main(void) {    
   point_t z;
   
   return 0;
}

void initializePoint(point_t *ptr, float x, float y) {

}

void printPoint(point_t z) {

}

Subsection 26.5.2 Print Pentagon

Activity 26.7.

Finally... Write a function printPoly() to which you pass a polygon and a number (its number of vertices), and which prints out these vertices.

Compile and run the code below in your own C environment.

#include <stdio.h>

typedef struct {
   float x;
   float y;
} point_t;

void printPoint(point_t z);
void initializePoint(point_t *ptr, float x, float y);

int main(void) {    
   point_t z;
   
   return 0;
}

void initializePoint(point_t *ptr, float x, float y) {

}

void printPoint(point_t z) {

}

Subsection 26.5.3 Don’t forget to free()!

Activity 26.8.

One last step, free the allocated memory!

Compile and run the code below in your own C environment.

#include <stdio.h>

typedef struct {
   float x;
   float y;
} point_t;

void printPoint(point_t z);
void initializePoint(point_t *ptr, float x, float y);

int main(void) {    
   point_t z;
   
   return 0;
}

void initializePoint(point_t *ptr, float x, float y) {

}

void printPoint(point_t z) {

}