Question: 2. Program in C: Structs I) Create a struct Person with attributes char name[20], int age, double score. Use typedef such that you can refer
2. Program in C: Structs
I) Create a struct Person with attributes char name[20], int age, double score. Use typedef such that you can refer to struct Person using Person. You may assume that no Person has a name longer than 19 chars, i.e., it will fit into the array of length 20.
II) Write a function Person createPerson(char name[], int age,double score) that returns a person with attributes as specified by the inputs. (Remember that you have to use a string function to copy over the name to the the Person.)
III) Write a function void printPerson(Person p) that prints the attributes of the Person p to the screen. Print the attributes using printf and the format string %s %d %d , i.e., printf(%s %d %d , ).
Ex.
Name age score
IV) Write a function void changeAge(Person * p,int newAge) that changes the age of a person to new age.
V) Create an void printPeople(Person people[],int length) function that prints the people stored in an array to screen. length is the number of people that are initialized in the array.
The function prototypes and a sample main are provided below:
Person createPerson(char name[],int age,double score);
void printPerson(Person p);
void changeAge(Person * p,int newAge);
void printPeople(Person people[],int length);
void main(){ Person p = createPerson("John",25,95);
printPerson(p);
changeAge(&p,30);
printPerson(p);
Person people[3];
people[0] = createPerson("Andy",30,86);
people[1] = createPerson("Casey",24,55);
people[2] = createPerson("Desirae",21,74);
printPeople(people,3);
};
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
