Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c source code: I) Create a struct Person with attributes “char name[20]”, “in

ID: 3700346 • Letter: I

Question

In c source code:

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 create Person(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 print People 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 create Person(char name[],int age, double score); void printPerson(Person p); void change Age Person * p,int new Age); void print People Person people[],int length); void main(){ Person p = create Person ("Rickard",31,100); printPerson(p); change Age(&p;,30); printPerson(p); Person people[3]; people[0] = createPerson("John",22,83); people[1] = createPerson("Danny",24,63); people[2] = create Person ("Marry",20,81); printPeople(people,3);

Explanation / Answer

#include <stdio.h>
#include <string.h>

typedef struct Person{
char name[20];
int age;
double score;
} Person;

Person createPerson(char name[], int age, double score){
Person person;
strcpy(person.name, name);
person.age = age;
person.score = score;
return person;
}

void printPerson(Person p){
printf("%s %d %.0lf ", p.name, p.age, p.score);
}

void changeAge(Person *p, int newAge){
p->age = newAge;
}

void printPeople(Person person[], int length){
int i;
for(i = 0; i < length; i++){
printPerson(person[i]);
}
}

int main(){
Person p = createPerson("Rickard", 31, 100);
printPerson(p);
changeAge(&p, 30);
printPerson(p);
Person people[3];
people[0] = createPerson("John", 22, 83);
people[1] = createPerson("Danny", 24, 63);
people[2] = createPerson("Marry", 20, 81);
printPeople(people, 3);
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote