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

Respuesta :

Answer:

Check the explanation

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("\nthe average of age is: %lf", avgAge);

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

  printf("\nthe 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("\nid: %d", id + 1);

      printf("\nplease 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:

Ver imagen temmydbrain