Write an ADT specification for the data structure: student Values are id (intege
ID: 3561818 • Letter: W
Question
Write an ADT specification for the data structure: student
Values are id (integer)
, name (string)
, age (integer)
, gpa (float)
A valid student ADT requires that id must be > 0 and name must be a string of at least 1
char.
There are 3 operators:
1. Make Student, a data type definition (i.e., constructor) which takes id and
name as inputs. This operator assumes that id > 0 and name has at least 1 char.
2. Set Age, which takes student and age as inputs, and if age is > 0, assigns it to
the student
Explanation / Answer
typedef struct {
int id;
char name[32];
int age;
float gpa;
}STUDENT;
Now you can instantiate your STUDENT struct like this:
void fct()
{
STUDENT s = {10, "MichaelInScarborough", 18, 3.5}
}
Here some C sample code:
#include <stdio.h>
#include <string.h>
typedef struct {
int id;
char name[32];
int age;
float gpa;
}STUDENT;
void fct(int id, char *name, int age, float gpa)
{
STUDENT s={1, "MichaelInScarborough", 18, 3.7f};
STUDENT t;
t.id = id;
strcpy(t.name, name);
t.age = age;
t.gpa = gpa;
STUDENT d = t;
printf("%03d %20s %3d %5.2f ", s.id, s.name, s.age, s.gpa);
printf("%03d %20s %3d %5.2f ", t.id, t.name, t.age, t.gpa);
printf("%03d %20s %3d %5.2f ", d.id, d.name, d.age, d.gpa);
}
int main()
{
fct(1, "Student", 15, 3.7f);
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.