第7回課題の解答例

課題1

#include <stdio.h>

int main()
{
  struct DATE_DATA
  {
    int year;
    int month;
    int day;
  } dx = {2024, 5, 27};

  printf("%d/%d/%d\n", dx.year, dx.month, dx.day);

  return 0;
}

課題2

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

#define N 6

int main()
{
  int i;
  struct PERSON
  {
    char family_name[20];
    char given_name[20];
    int age;
    float height;
    float weight;
  } p[N];

  strcpy(p[0].given_name, "Scarlett");
  strcpy(p[0].family_name, "Johansson");
  p[0].age=33; p[0].height=160; p[0].weight=50;

  strcpy(p[1].given_name, "Nicole");
  strcpy(p[1].family_name, "Kidman");
  p[1].age=50; p[1].height=180; p[1].weight=58;

  strcpy(p[2].given_name, "Jessica");
  strcpy(p[2].family_name, "Chastain");
  p[2].age=41; p[2].height=163; p[2].weight=56;

  strcpy(p[3].given_name, "Julianne");
  strcpy(p[3].family_name, "Moore");
  p[3].age=57; p[3].height=163; p[3].weight=54;

  strcpy(p[4].given_name, "Natalie");
  strcpy(p[4].family_name, "Portman");
  p[4].age=36; p[4].height=160; p[4].weight=50;

  strcpy(p[5].given_name, "Alicia");
  strcpy(p[5].family_name, "Vikander");
  p[5].age=29; p[5].height=168; p[5].weight=53;

  for (i=0; i<N; i++)
  {
    printf("%s %s %d %f %f\n", p[i].given_name, p[i].family_name, p[i].age, p[i].height, p[i].weight);
  }
  
  float age_total, h_total, w_total;
  age_total=0.0;
  h_total=0.0;
  w_total=0.0;
  for (i=0; i<N; i++)
  {
    age_total += p[i].age;
    h_total += p[i].height;
    w_total += p[i].weight;
  }
  printf("average of age: %f\n", age_total/N);
  printf("average of height: %f\n", h_total/N);
  printf("average of weight: %f\n", w_total/N);


  return 0;
}

Updated: