-
Notifications
You must be signed in to change notification settings - Fork 26
/
structAllocateMemory.c
60 lines (46 loc) · 1.54 KB
/
structAllocateMemory.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <stdlib.h>
/*
initializePoly() should receive as input a pointer to the first structure in an array of structures
as well as in integer, indicating how many points can be stored in the array. Your job is to initialize
these points in the following way: Using a for loop with i starting at 0, initialize the x-coordinate of
the point at index i as -i, and the y-coordinate as i*i.
Your main function should read the number of vertices to store in the array of points from the user, then
allocate the appropriate amount of memory, initialize the array with the function initializePoly, and
finally print the vertices of the polygon using the function printPoly().
*/
struct point{
int x;
int y;
};
void printPoint(struct point);
void printPoly(struct point *, int);
void initializePoly(struct point *, int);
int main(void) {
int numVertices;
struct point * polygon;
// Fill in your main function here
scanf("%d", &numVertices);
polygon = (struct point *) malloc(numVertices * sizeof(struct point));
initializePoly(polygon, numVertices);
printPoly(polygon, numVertices);
free(polygon);
return 0;
}
void printPoint(struct point pt) {
printf("(%d, %d)\n", pt.x, pt.y);
}
void printPoly(struct point *ptr, int N) {
int i;
for (i=0; i<N; i++) {
printPoint(ptr[i]);
}
}
// Write your initializePoly() function here
void initializePoly(struct point * poly, int N){
int i;
for (i=0; i<N; i++) {
poly[i].x = -i;
poly[i].y = i*i;
}
}