title: Sum and bubble sort about funtion
body: <p>I am a beginner in C language.</p>
  <p>I studied all night and did my assignment.</p>
  <p>But I wonder if there is a more efficient way.</p>
  <p>The original structure is this.</p>
  <pre><code>typedef struct student {
      int id;
      int prob_1;
      int prob_2;
      int prob_3;
      int total_score;
      char grade;
  }Student
  </code></pre>
  <p>I used typedef to change it to Student.</p>
  <p>And I'll show you the contents of the function.</p>
  <pre><code>void cal_total_score(Student* s) {
      /*
      Problem (A):
      Implement the function &quot;cal_total_score&quot;.
      - In each student, total_score must be the sum of prob_1, prob_2, and prob_3.
      - The codes outside the function must not be changed.
      */
      int i;
      for (i = 0; i &lt; NUMSTUDENT; i++) {
          s[i].total_score = s[i].prob_1 + s[i].prob_2 + s[i].prob_3;
      }
  }
  
  void student_bb_sort(Student* s) {
      /*
      Problem (B):
      Implement the function &quot;student_bb_sort&quot;.
      - The student array &quot;s&quot; must be sorted according to total_score.
      - Bubble sort algorithm should be used for the sorting.
      - The codes outside the function must not be changed.
      */
      Student tmp;
      int i, j;
  
      for (i = 0; i &lt; NUMSTUDENT - 1; i++) {
          for (j = 0; j &lt; NUMSTUDENT - 1 - i; j++) {
              if (s[j].total_score &gt; s[j + 1].total_score) {
                  tmp = s[j];
                  s[j] = s[j + 1];
                  s[j + 1] = tmp;
              }
          }
      }
  }
  </code></pre>
  <p>I'll omit the main function for a length.</p>
  <p>Please let me know if there are any more wrong or dangerous phrases or efficient ways.</p>
  <p>Thank you.</p>
  <p>============================================================================</p>
  <p>This is a given main function in advance, so please read it if you need it.</p>
  <pre><code>typedef struct student {
      int id;
      int prob_1;
      int prob_2;
      int prob_3;
      int total_score;
      char grade;
  }Student;
  
  void cal_total_score(Student* s);
  void student_bb_sort(Student* s);
  
  int main() {
      Student s[NUMSTUDENT];
      int i;
  
      for (i = 0; i &lt; NUMSTUDENT; i++) {
          s[i].id = i;
          s[i].prob_1 = rand() % 30 + 1;
          s[i].prob_2 = rand() % 30 + 1;
          s[i].prob_3 = rand() % 40 + 1;
          printf(&quot;Student %d\nP1: %d\tP2: %d\tP3: %d\n&quot;, s[i].id, s[i].prob_1, s[i].prob_2, s[i].prob_3);
      }
  
      for (i = 0; i &lt; NUMSTUDENT; i++) {
          cal_total_score(&amp;s[i]);
          printf(&quot;Student %d\tScore: %d\n&quot;, s[i].id, s[i].total_score);
      }
  
      student_bb_sort(s);
      printf(&quot;\nAfter Bubble Sort\n&quot;);
  
      for (i = 0; i &lt; NUMSTUDENT; i++) {
          if ((float)i &lt; (float)NUMSTUDENT / 3) {
              s[i].grade = 'C';
          }
          else if ((float)i &lt; (float)NUMSTUDENT / 3 * 2 &amp;&amp; (float)i &gt;= (float)NUMSTUDENT / 3) {
              s[i].grade = 'B';
          }
          else {
              s[i].grade = 'A';
          }
          printf(&quot;Student %d\tScore: %d\tGrade: %c\n&quot;, s[i].id, s[i].total_score, s[i].grade);
      }
  </code></pre>