プロが教えるわが家の防犯対策術!

c言語で「5人の学生の学籍番号,英語の点数,体重が以下の通りであった。
学籍番号 英語 体重(kg)
------------------------
9972001 84 62.3
9972002 70 54.2
9972003 65 68.9
9972004 96 82.1
9972005 82 70.4
学籍番号と英語の点数,体重を格納する構造体を作り,その構造体の配列に上記のデータを格納した状態で,英語の点数の下限と体重の下限を入力すると,英語の点数と体重が共に下限以上である学生の学籍番号のリストと該当する学生の人数を表示するプログラムを作成せよ。ただし,学籍番号と英語の点数は整数,体重は実数で表現すること。」という課題が出ました。一応作ってはみたものの学籍番号のリストと該当する学生の人数の表示の仕方がわかりません。
実行例は
「英語の点数の下限:80
体重の下限:65
英語の点数が80点以上で,体重が65.000000kg以上の人のリスト
9972004
9972005
(計2人)」となる感じです。一応僕が作ったプログラミングも載せておきます。でも間違えだらけの上に未完成です。どなたか教えて下さい。お願いします。

「c言語で「5人の学生の学籍番号,英語の点」の質問画像

A 回答 (2件)

例えば、



#include <stdio.h>

struct profile
{
  int no; // 学籍番号
  int eng; // 英語の点数
  double weight; // 体重
};

int main();
void init(struct profile student[]);
void set_prof(struct profile *pstudent, int no, int eng, double weight);

int main()
{
  struct profile student[5];
  int btm_eng; // 英語点数の下限
  double btm_weight; // 体重の下限
  int i, count; // カウンタ、生徒数カウント

  // 初期化
  init(student);

  printf("英語の点数の下限:");
  scanf_s("%d", &btm_eng);
  printf("体重の下限:");
  scanf_s("%lf", &btm_weight);

  printf("英語の点数が%d点以上で,体重が%9.6lfkg以上の人のリスト\n", btm_eng, btm_weight);
  count = 0;
  for (i = 0; i <= 4; i++)
  {
    if ((btm_eng <= student[i].eng) && (btm_weight <= student[i].weight))
    {
      printf("%d\n", student[i].no);
      count++;
    }
  }

  printf("(計:%d人)\n", count);

  return 0;
}

void init(struct profile student[])
{
  set_prof(&student[0], 9972001, 84, 62.3);
  set_prof(&student[1], 9972002, 70, 54.5);
  set_prof(&student[2], 9972003, 65, 68.9);
  set_prof(&student[3], 9972004, 96, 82.1);
  set_prof(&student[4], 9972005, 82, 70.4);
}

void set_prof(struct profile *pstudent, int no, int eng, double weight)
{
  pstudent->no = no;
  pstudent->eng = eng;
  pstudent->weight = weight;
}
    • good
    • 1
この回答へのお礼

ありがとうございます。参考にします。

お礼日時:2019/05/19 00:07

まずは、構造体について理解して、


構造体を作って下さい。

構造体を関数に渡して判定する様にすれば全体が見やすくなると思います。
    • good
    • 0

お探しのQ&Aが見つからない時は、教えて!gooで質問しましょう!