fork download
  1. #include <stdio.h>// ใช้สำหรับฟังก์ชันพื้นฐาน เช่น printf(), scanf()
  2. #include <string.h> // ใช้สำหรับจัดการ string เช่น strcpy(), strlen() เป็นต้น
  3. typedef struct{
  4. char id[12];// รหัสนักเรียน (string ขนาดไม่เกิน 11 ตัวอักษร + '\0')
  5. char name[50]; // ชื่อนักเรียน
  6. int year;// ปีการศึกษา (หรือปีที่เรียนอยู่)
  7. float gpa;// เกรดเฉลี่ย
  8. } Student;// ตั้งชื่อ struct นี้ว่า Student
  9. void printStudent(Student s){
  10. // แสดงข้อมูลนักเรียนในรูปแบบ id ชื่อ ปี GPA (ทศนิยม 2 ตำแหน่ง)
  11. printf("%s\t%s\t%d\t%.2f\n", s.id, s.name, s.year, s.gpa);
  12. }
  13. int main(){
  14. int n; // ตัวแปรสำหรับเก็บจำนวนนักเรียน
  15. printf("n = ");
  16. scanf("%d", &n); // รับค่าจำนวนของนักเรียนจากผู้ใช้
  17. Student a[100]; // ประกาศ array ของ Student รองรับได้สูงสุด 100 คน
  18. for(int i=0;i<n;i++){ // วนลูปเพื่อรับข้อมูลนักเรียนแต่ละคน
  19. printf("id name year gpa: ");
  20. scanf("%11s %49s %d %f", a[i].id, a[i].name, &a[i].year, &a[i].gpa);
  21. }
  22. // หาค่าสูงสุด
  23. // หานักเรียนที่มี GPA สูงสุด
  24. int idx=0; // เริ่มต้นด้วย index 0 เป็นค่าที่มากที่สุดชั่วคราว
  25. for(int i=1;i<n;i++) if(a[i].gpa > a[idx].gpa) // เริ่มต้นด้วย index 0 เป็นค่าที่มากที่สุดชั่วคราว
  26. idx=i; // เปลี่ยน index max เป็น index นี้
  27. printf("Max GPA: "); // แสดงข้อมูลนักเรียนที่มี GPA สูงสุด
  28. printStudent(a[idx]); // เรียกฟังก์ชันแสดงข้อมูลของนักเรียนคนนั้น
  29. return 0;// จบโปรแกรม
  30. }
Success #stdin #stdout 0.03s 25920KB
stdin
Standard input is empty
stdout
#include <stdio.h>// ใช้สำหรับฟังก์ชันพื้นฐาน เช่น printf(), scanf()
#include <string.h> // ใช้สำหรับจัดการ string เช่น strcpy(), strlen() เป็นต้น
typedef struct{
 char id[12];// รหัสนักเรียน (string ขนาดไม่เกิน 11 ตัวอักษร + '\0')
 char name[50]; // ชื่อนักเรียน
 int year;// ปีการศึกษา (หรือปีที่เรียนอยู่)
 float gpa;// เกรดเฉลี่ย
} Student;// ตั้งชื่อ struct นี้ว่า Student
void printStudent(Student s){
      // แสดงข้อมูลนักเรียนในรูปแบบ id ชื่อ ปี GPA (ทศนิยม 2 ตำแหน่ง)
 printf("%s\t%s\t%d\t%.2f\n", s.id, s.name, s.year, s.gpa);
}
int main(){
 int n; // ตัวแปรสำหรับเก็บจำนวนนักเรียน
 printf("n = ");
 scanf("%d", &n); // รับค่าจำนวนของนักเรียนจากผู้ใช้
 Student a[100]; // ประกาศ array ของ Student รองรับได้สูงสุด 100 คน
 for(int i=0;i<n;i++){ // วนลูปเพื่อรับข้อมูลนักเรียนแต่ละคน
 printf("id name year gpa: ");
 scanf("%11s %49s %d %f", a[i].id, a[i].name, &a[i].year, &a[i].gpa);
 }
 // หาค่าสูงสุด
  // หานักเรียนที่มี GPA สูงสุด
 int idx=0; // เริ่มต้นด้วย index 0 เป็นค่าที่มากที่สุดชั่วคราว
 for(int i=1;i<n;i++) if(a[i].gpa > a[idx].gpa)  // เริ่มต้นด้วย index 0 เป็นค่าที่มากที่สุดชั่วคราว
    idx=i; // เปลี่ยน index max เป็น index นี้
 printf("Max GPA: ");  // แสดงข้อมูลนักเรียนที่มี GPA สูงสุด
 printStudent(a[idx]);  // เรียกฟังก์ชันแสดงข้อมูลของนักเรียนคนนั้น
 return 0;// จบโปรแกรม
}