97.4k views
2 votes
Task:create a struct that looks like:typedef struct person{ int age; double height;} Person;Create an array of Persons. Read data for multiple persons from the keyboard. Stop when a negative age is found. Then find the average height, the average age, and the average ratio of age to height(age/height

1 Answer

3 votes

Answer:

Check the explanation

Step-by-step explanation:

person.c

#include <stdio.h>

#include <stdlib.h>

typedef struct person

{

int age;

double height;

}Person;

void getAverage(Person persons[], int max)

{

int i, totAge=0;

double avgAge = 0.0, avgHeight = 0.0, ratio=0.0, totHeight = 0;

//calculate the Of age, height and the ratio Of to height(age/height):

for(i=0;i<max;i++)

{

totAge = totAge + persons[i].age;

totHeight = totHeight + persons[i].height;

}

//calculate the average

avgAge = totAge/max;

avgHeight = totHeight / max;

ratio = avgAge / avgHeight;

//output the results

printf("\\the average of age is: %lf", avgAge);

printf("\\the average of height is: %lf", avgHeight);

printf("\\the average ratio of age to height is: %lf", ratio);

}

int main()

{

//variables declaration

int id=0;

Person p[10];

//read data from keyboard, which are the age and the height of person

while(1)

{

printf("\\id: %d", id + 1);

printf("\\please enter the age: ");

scanf("%d", &p[id].age);

if(p[id].age < 0)

break;

printf("please enter the height: ");

scanf("%lf", &p[id].height);

id++;

}

//call getAverage function

getAverage(p, id);

return 0;

}

Kindly check the Screenshot and Output in the attached images below:

Task:create a struct that looks like:typedef struct person{ int age; double height-example-1
User Thomaszter
by
3.3k points