-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructures.c
More file actions
94 lines (75 loc) · 2.41 KB
/
Structures.c
File metadata and controls
94 lines (75 loc) · 2.41 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/* Christopher Yonek
CSC:210 - Dr. Roosen
Structures
4/30/2022
*/
typedef struct {
float length;
int strings;
char *name;
} instrument;
// A variable vio has been defined and initialized as an instrument. Print its variables.
printf("The %d-string %s is %dcm long.", vio.strings,vio.name,(int)vio.length);
instrument *vio_p;
instrument *get_vio(int s, char *n, float l);
//Print line like: "The 4-string violin is 36cm long."
printf("The %d-string %s is %dcm long.", vio_p->strings, vio_p->name, (int) vio_p->length);
typedef struct {
int legs;
char *sound;
char *name;
} pet_t;
typedef struct {
char *name;
int age;
pet_t *pet;
} owner_t;
owner_t *kid;
owner_t *get_owner(char *,int,pet_t *);
pet_t *get_pet(char *name, char *sound, int legs);
//Print a line like: "Julie's pet parrot has 2 legs and says, "squawk"."
printf("%s's pet %s has %d legs and says, \"%s\".", kid->name, kid->pet->name, kid->pet->legs, kid->pet->sound);
/* A structure chair with members legs, height, and color, each with appropriate types. */
typedef struct {
int legs;
float height;
char *color;
} chair;
/* This function distance that consumes two "points" and returns the distance between them. */
typedef struct {
float x;
float y;
float z;
} point;
float distance(point m, point n){
float result;
result = sqrt((m.x - n.x) * (m.x - n.x) + (m.y - n.y) *(m.y - n.y)+(m.z-n.z)*(m.z-n.z));
return result;
}
/* A function finalscore that consumes a structure for a student and returns the course score. */
typedef struct {
float homework;
float midterm1;
float midterm2;
float finalexam;
} student;
float finalscore(student s) {
return (s.homework + s.midterm1 + s.midterm2 + s.finalexam) / 4;
}
/* A function evaluate that consumes a pointer-to-quadratic and a float x, and returns the value of the quadratic at x. */
typedef struct {
float a;
float b;
float c;
} quadratic;
float evaluate(quadratic *q, float x) {
return (q->a * x * x) + (q->b * x) + q->c;
}
/* A function generate that consumes two floats and returns a pointer-to-quadratic with zeros at the given floats */
quadratic* generate (float x1, float x2){
quadratic* quad = (quadratic*) malloc (sizeof(quadratic));
quad->a = 1;
quad->b = -(x1 + x2);
quad->c = x1 * x2;
return quad;
}