Use structural variables to store student names and course scores, calculate average scores, and find the names and scores of failed students

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.