104k views
4 votes
Write C program and use typedef to create a structure called twoDShape that contains

character arrays name[20], an integer numberOfEdges, and two float radius and
area. The program then reads a list of 2D shapes from the user (up to 10 shapes).
Remember a circle needs radius instead of edges.
Using a pointer shapePtr that points to the list of shapes, display the information back
to the users together with the area of the shape. For triangle you need to ask for values
of the height(h) and base(b), while for square and circle use your own common-sense.
Use the formula below for triangle. All values are of type float and π = 3.14159

User Herve Thu
by
7.1k points

1 Answer

6 votes

#include <stdio.h>

typedef struct {

char name[20];

int numberOfEdges;

float radius;

float area;

} twoDShape;

int main() {

twoDShape shapeList[10];

twoDShape *shapePtr;

shapePtr = shapeList;

for (int i = 0; i < 10; i++) {

printf("Enter name of shape: ");

scanf("%s", shapePtr->name);

printf("Enter number of edges (or -1 for a circle): ");

scanf("%d", &shapePtr->numberOfEdges);

if (shapePtr->numberOfEdges == -1) {

printf("Enter radius of circle: ");

scanf("%f", &shapePtr->radius);

shapePtr->area = 3.14159 * (shapePtr->radius * shapePtr->radius);

} else if (shapePtr->numberOfEdges == 3) {

float h, b;

printf("Enter value of height: ");

scanf("%f", &h);

printf("Enter value of base: ");

scanf("%f", &b);

shapePtr->area = (h * b) / 2;

} else if (shapePtr->numberOfEdges == 4) {

float l;

printf("Enter value of length: ");

scanf("%f", &l);

shapePtr->area = l * l;

}

shapePtr++;

}

shapePtr = shapeList;

for (int i = 0; i < 10; i++) {

printf("Shape: %s\\", shapePtr->name);

printf("Number of edges: %d\\", shapePtr->numberOfEdges);

printf("Area: %f\\", shapePtr->area);

shapePtr++;

}

return 0;

}

User Ejack
by
7.3k points