1 structStudent//Declaration Student type struct-body variable2 {3 Charname[ -];4 intscore[5];5 };6typedefstructStudent Stu;7 8 voidMain ()9 {Ten OneStu stu[3]; Defining an array of student type variables A inti,j; - for(i=0;i<3; i++)//Enter student's name and score - { theprintf"Please input student[%d] ' s name and scores:\n", i+1); -printf"Name:"); scanf ("%s", stu[i].name); -printf"\nscores:"); - for(j=0;j<4; j + +) + { -scanf"%d",&stu[i].score[j]);} +stu[i].score[j]=0; A } at voidAver (Stu s[],intn); -Aver (Stu,3); Calculate the average score - voidFail (Stu s[],intn); -Fail (Stu,3); fail to print the results of the students - } - voidAver (Stu s[],intN) in { - inti,j; to floatsum; + for(i=0; i<n;i++) - { the for(j=0, sum=0.0;j<4; j + +) * { $sum=sum+S[i].score[j];Panax Notoginseng } -s[i].score[j]=sum/J; the } +printf"Average scores:\n"); A for(i=0; i<n;i++) the { +printf"\n%s:%.1d", s[i].name,s[i].score[4]); - } $printf"\ n"); $ } - voidFail (Stu s[],intN) - { the inti,j; - for(i=0; i<n;i++)Wuyi { the for(j=0;j<4; j + +) - { Wu if(s[i].score[j]< -) - { Aboutprintf"\n%s failed:", s[i].name); $printf"\nscores:"); - for(j=0;j<4; j + +) -printf"%d\t", S[i].score[j]); - Break; A } + } the } -}
struct type and Int,char, array and other basic types, is also a C language data type, but this is a custom data type, we can be based on the requirements of various basic data types constructed.
Follow the general rules used by variables when using struct variables. A struct type declaration is generally placed at the very front of the code, which can be used in the entire program file or in a header file.
Use struct variables to store student names and course scores, calculate average scores, and find the names and scores of failed students