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

In c source code: Question 1: Define a struct pet with properties name, age, wei

ID: 3721174 • Letter: I

Question

In c source code:

Question 1: Define a struct pet with properties name, age, weight, and type. Use appropriate data types for the different properties. You may assume that the strings will never be longer than 19 chars. Don't forget to add an extra char for the NULL character that terminates the strings. Question 2: Using a single line of code, define and initialize a variable "pl" storing a pet with appropriate values. pl On the next line in the main, create a pointer called "ptP1" that points to the pet pl. Next, print the name of the pet to screen using the pointer ptrP1. Print the age of the pet to screen using the variable int mainOt //define your pet "pl" I/define the pointer "ptrP1" /print the name using ptrP1 print the age using pl Question 3 Write a function "changeWeight" that takes pet and a new weight as the input to the function. Hint: Should the pet be passed by value or reference? I/ write you changeWeight function below

Explanation / Answer

#include <stdio.h>

struct Pet
{
char name[20];
int age;
double weight;
char type[20];
};

void changeWeight(struct Pet *p,double newWeight)
{
p->weight = newWeight;
}
int main(void) {

struct Pet p1 = {"Kitty",3,20.4,"cat"};

struct Pet *ptrP1 = &p1;

printf("name of pet : %s",ptrP1->name);

printf(" age of pet : %d",p1.age);

printf(" weight of pet : %.2lf",p1.weight);

changeWeight(ptrP1,15.67);

printf(" new weight of pet : %.2lf",p1.weight);

return 0;
}

Output:

name of pet : Kitty
age of pet : 3
weight of pet : 20.40
new weight of pet : 15.67

Question 1:

struct Pet
{
char name[20];
int age;
double weight;
char type[20];
};

Question 2:

int main(void) {

struct Pet p1 = {"Kitty",3,20.4,"cat"};

struct Pet *ptrP1 = &p1;

printf("name of pet : %s",ptrP1->name);

printf(" age of pet : %d",p1.age);

printf(" weight of pet : %.2lf",p1.weight);

changeWeight(ptrP1,15.67);

printf(" new weight of pet : %.2lf",p1.weight);

return 0;
}

Question 3:

void changeWeight(struct Pet *p,double newWeight)
{
p->weight = newWeight;
}

Pointer or reference to struct Pet need to be used as argument in the function

Do ask if any doubt. Please upvote.

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