1. (20%) Sorting student records.
    1. You are required to write a C program to handle student records. Each record consists of 3 fields (name, height, weight), as shown below.
      Papa 194 51
      Uniform 189 85
      Sierra 187 87
      Whiskey 195 45
      Zulu 184 55
      Juliet 177 88
      Quebec 175 68
      Tango 157 96
      Kilo 173 79
      Xray 156 89 
    2. Suppose the main function is given:
      #include <stdio.h>
      #include <ctype.h>
      #include "bmi_util.h"
      
      #define MAXSTUDENT      50
      
      int main()
      {
          Student stu[MAXSTUDENT];
          char ch;
          unsigned nStu;
          while ((ch = get_choice())) {
              ch = toupper(ch);
              switch (ch) {
                  case 'Q':
                      return 0;
                  case 'L':   /* Load file */
                      nStu = load_student(stu);
                      break;
                  case 'S':
                      store_student(stu, nStu);
                      break;
                  case 'P':
                      print_student(stu, nStu);
                      break;
                  case 'H':
                      sort_by_height(stu, nStu);
                      break;
                  case 'W':
                      sort_by_weight(stu, nStu);
                      break;
                  case 'N':
                      sort_by_name(stu, nStu);
                      break;
                  case 'B':
                      sort_by_bmi(stu, nStu);
                      break;
                  default:
                      printf("No such choice!\n");
                      break;
              }
          }
          return 0;
      }  
    3. Complete the implementation of those helping functions and store them in a "bmi_util.c" file so that it can be compiled with the main program. Store the function headers in "bmi_util.h".
    4. The program may run as follows:
      L)oad student data from a file
      S)tore student data to a file
      P)rint student data on screen
      Q)uit
      Sort by H)eight
      Sort by N)ame
      Sort by W)eight
      Sort by B)MI
      Please choose an action and press ENTER -- L
      Pleaes input filename -- student.dat
      10 records have been read.
      
      L)oad student data from a file
      S)tore student data to a file
      P)rint student data on screen
      Q)uit
      Sort by H)eight
      Sort by N)ame
      Sort by W)eight
      Sort by B)MI
      Please choose an action and press ENTER -- P
      Whiskey         195     45      11.83
      Papa            194     51      13.55
      Zulu            184     55      16.25
      Quebec          175     68      22.20
      Kilo            173     79      26.40
      Uniform         189     85      23.80
      Sierra          187     87      24.88
      Juliet          177     88      28.09
      Xray            156     89      36.57
      Tango           157     96      38.95
      Totally 10 records.
      
      L)oad student data from a file
      S)tore student data to a file
      P)rint student data on screen
      Q)uit
      Sort by H)eight
      Sort by N)ame
      Sort by W)eight
      Sort by B)MI
      Please choose an action and press ENTER -- H
      10 records sorted by height in ascending order.
      
      L)oad student data from a file
      S)tore student data to a file
      P)rint student data on screen
      Q)uit
      Sort by H)eight
      Sort by N)ame
      Sort by W)eight
      Sort by B)MI
      Please choose an action and press ENTER -- P
      Xray            156     89      36.57
      Tango           157     96      38.95
      Kilo            173     79      26.40
      Quebec          175     68      22.20
      Juliet          177     88      28.09
      Zulu            184     55      16.25
      Sierra          187     87      24.88
      Uniform         189     85      23.80
      Papa            194     51      13.55
      Whiskey         195     45      11.83
      Totally 10 records.
      
      L)oad student data from a file
      S)tore student data to a file
      P)rint student data on screen
      Q)uit
      Sort by H)eight
      Sort by N)ame
      Sort by W)eight
      Sort by B)MI
      Please choose an action and press ENTER -- B
      10 records sorted by BMI in ascending order.
      
      L)oad student data from a file
      S)tore student data to a file
      P)rint student data on screen
      Q)uit
      Sort by H)eight
      Sort by N)ame
      Sort by W)eight
      Sort by B)MI
      Please choose an action and press ENTER -- S
      Pleaes input filename -- bmi.dat
      10 records written.
      
      L)oad student data from a file
      S)tore student data to a file
      P)rint student data on screen
      Q)uit
      Sort by H)eight
      Sort by N)ame
      Sort by W)eight
      Sort by B)MI
      Please choose an action and press ENTER -- Q  
    5. The contents of bmi.dat should be
      Whiskey 195 45
      Papa 194 51
      Zulu 184 55
      Quebec 175 68
      Uniform 189 85
      Sierra 187 87
      Kilo 173 79
      Juliet 177 88
      Xray 156 89
      Tango 157 96  
  2. (20%) Using struct to compute matrix multiplication
    1. In mathematics, a rational number is any number that can be expressed as the quotient or fraction p/q of two integers, a numerator p and a non-zero denominator q.
    2. Most computer languages do not have a native data type to support the operation of rational numbers. However, with "struct" in C language, you can define your own data type and operations to handle the addition, multiplication, and many other operations of rational numbers.
    3. Suppose the main function is given as below:
      #define _CRT_SECURE_NO_WARNINGS
      #include <stdio.h>
      #include "rational_util.h"
      
      int main()
      {
          Rational  A[N][N], B[N][N], C[N][N];
          int k, m, n;
          read_2_int(&m, &n);
          read_matrix(A, m, n);
          read_matrix(B, m, n);
          add_matrix(A, B, C, m, n);
          print_matrix(C, m, n);
          printf("\n");
      
          read_3_int(&m, &k, &n);
          read_matrix(A, m, k);
          read_matrix(B, k, n);
          mul_matrix(A, B, C, m, k, n);
          print_matrix(C, m, n);
      
          return 0;
      } 
    4. Complete the implementation of those helping functions and store them in a "rational_util.c" file so that it can be compiled with the main program.
    5. The function headers are declared in "rational_util.h":
      
      #define N       10
      #define TRUE    1
      #define FALSE   0
      typedef int bool;
      
      struct Rational {
          int numerator;
          int denominator;
      };
      typedef struct Rational Rational;
      
      Rational read_rational();
      void print_rational(Rational a);
      int gcd(int a, int b);
      void reduce(Rational *a);
      void reduce_rational(Rational *a);
      Rational mul_rational(Rational a, Rational b);
      Rational add_rational(Rational a, Rational b);
      bool read_matrix(Rational A[][N], int m, int n);
      void print_matrix(Rational A[][N], int m, int n);
      void add_matrix(Rational A[][N], Rational B[][N], Rational C[][N], int m,
         int n);
      void mul_matrix(Rational A[][N], Rational B[][N], Rational C[][N], int m,
          int k, int n);
      int read_2_int(int *a, int *b);
      int read_3_int(int *a, int *b, int *c);
      
      
    6. With the following input:
      3 2
      
      1/2 1/2
      1/1 1/6
      3/10 1/24
      1/2 1/3
      1/2 1/3
      1/5 1/36
      
      2 3 2
      
      1 2 3
      2 4 6
      1 1/3
      1/2 1/2
      1/3 1
      ^Z
      ^Z
      ^Z  
      the program should generate the following output:
      1       5/6
      3/2     1/2
      1/2     5/72
      
      3       13/3
      6       26/3