Generation

generate functionMon, 12 Dec 2022

int 型の no,int 型の age をメンバとする構造体 person を定義.person 型の大きさ 10 の配列変数 hito を宣言し,no には 0 からの通し番号を,age には 0 から 100 までの乱数を格納.その後,最も age 値の大きい配列要素の内容を表示するプログラム(すべて main() 関数内で作成).

#include <stdio.h> #include <stdlib.h> #include <time.h> struct person{ int no; int age; }; int main(void){ struct person hito[10]; int i; int j; int max_age; int max_no; srand((unsigned)time(NULL)); for(i = 0; i < 10; i++){ hito[i].no = i; hito[i].age = rand() % 101; printf("番号:%2d, 年齢:%2d\n", hito[i].no, hito[i].age); } max_age = hito[0].age; max_no = hito[0].no; for(j = 1; j < 10; j++){ if(hito[j].age > max_age){

Questions about programming?Chat with your personal AI assistant