Lab 1 Part 2: Validate Your Matric Number

This part of the lab is worth 5 marks.


Every student in NUS has been assigned an undergraduate matriculation number of the form Ud1d2d3d4d5d6X in which U (denoting Undergraduate) is followed by a six-digit number d1..d6 and a single check-letter X. The check-letter is useful in checking the validity of the matriculation number especially in computerised systems which are prone to human error during the data entry process.


Objective

Write a C program matric.c that repeatedly accepts the following input: We shall assume that the user obeys instructions and you do not have to explicitly check the validity of both input. For each number entered, the corresponding check-letter should be computed and checked against the check-letter input by the user. The program terminates when the user enters a negative number. The algorithm for determining the check-letter is given in the next section. The skeleton code and sample output are also provided. Remember to submit your program before the stipulated deadline for your lab session. The file that you submit must be named matric.c, any other name is not allowed.

Note that you are NOT to use any form of arrays in your program.


Algorithm

Let the six-digit number be denoted d1d2d3d4d5d6
  1. Compute the weighted sum (2 × d2) + (6 × d3) + (2 × d4) + (4 × d5) + d6
    (we ignore the digit d1 as undergraduates for this decade would have it as 0 )
  2. Divide the result in step 1 by 13 and obtain its remainder.
  3. Subtract the remainder from 13.
  4. Locate the check-letter using the table below:
    Result from step 3Check-Letter
    1M
    2B
    3N
    4A
    5R
    6E
    7U
    8H
    9W
    10J
    11X
    12L
    13Y


Program Skeleton

The following code continuously accepts the matric number and check-letter from the user and simply prints the matric number entered. The program terminates when the matric number is zero or less. You are encouraged to modify and extend the skeleton code in order to achieve your task.

#include <stdio.h>

int main()
{
   int  matric;
   char ref;

   /* Input a six-digit matric number */
   printf("Enter the six-digit matric number (0 or -ve to exit): ");
   scanf("%d", &matric);

   while (matric > 0)
   {
      /* Input the check-letter */
      printf("Enter the capitalized check-letter: ");

      /* flush input stream to clear any existing character(s) and then scanf*/
      fflush(stdin);
      scanf("%c", &ref);

      printf("The matric number entered is U%d%c\n", matric, ref);

      /* Input another six-digit matric number */
      printf("Enter the six-digit matric number (0 or -ve to exit): ");
      scanf("%d", &matric);
   }

   /* Terminating program */
   printf("\nProgram Terminated...\n");
   return 0;
}


Sample Output

The following is a sample session. User input is denoted in bold.

Enter the six-digit matric number (0 or -ve to exit): 012345
Enter the capitalized check-letter: X
The matric number U12345X is valid.

Enter the six-digit matric number (0 or -ve to exit): 012345
Enter the capitalized check-letter: A
The matric number U12345A is INVALID!

Enter the six-digit matric number (0 or -ve to exit): 0

Program Terminated...

You are encouraged to run your program using your own matric number, as well as the matric numbers of your neighbours or even your lab tutors (as long as their matric number begins with a U).