Program 2
COP2221
Dr. Riggs Sp 2002
You are to write, compile and run a program to calculate
the driver's license number according to the algorithm given in class.
The complete details appear in the material below copied from For All practical
Purposes, L. A Steno ed., W.H. Freeman Co. , 3rd Ed. 1994, p.
292. (You should read that first!) We will need some new details in order
to program the algorithm in C. These details and some explanation are based
on the description in class and in the quoted material below.
We can read a name (e.g. last name - lname in the following
way:
-
declare a character array:
char lname[20];
// this can hold up to 19 characters of name
-
prompt for and read the name:
printf(" Enter your last name: ");
scanf("%s", lname);
// note NO &
-
print the 1st character of the name:
printf("%c ", lname[0]);
// note 1st char is 0 from beginning
Since the second character is 1 from the beginning, we can
get it as lname[1]. We can also get the third, fourth and fifth characters
in a similar way. BUT, we don't want the character, we want a code number
(a-1,b-2, etc.). Since characters are really kept as numeric codes, we
can map (translate) the character into the proper number. We show how for
the second character in the last name - others are similar:
-
get code of 2nd letter in lname:
n1 = lname[1]-'a'+1;
// e.g. 'b' - 'a' = 1 + 1 = 2 etc.
The 'tricks' above can be used to get codes for all the letters
of lname (n1, n2, n3, n4). Similar code can get
the numeric values (n5, n5, n7) for the first
three letters of the first name (fname) and for
n8
which comes from mInit (the middle initial).
From these eight number you can get the first result:
result = 10017758323.0*n1 + 371538441.0*n2
+ 13779585.0*n3 + 510355.0*n4
+ 19657.0*n5 + 729.0*n6 +
27.0*n7 + n8- 385829132.0;
Note result is a BIG number. Declare result to be of type:
double.
Now, result is the first twelve digits of the number and can be printed
by the statement below:
// 0xxx prints leading 0's;
.0
= no decimal places, l => a long float (double)
// some compilers may not want the
'ell'
printf("%012.0lf", result);
Now we need only five more digits. For them we must read:
the birth month, birth day,
birth year and a value for sex
(0 for male, 1 for female). When we have read those, we can calculate the
next three digits as:
digits3 = 63*month + 2*day + sex;
// print digits3 & 1000
using "%3d" to get just 3 digits of output
These last two digits are taken as the last two digits
of year, We can print them as below:
printf("%2d", tear % 100);
Read all the values, then print the letter followed by
the calculated numbers in the order given, and you will have the correct
output. (We are assuming that the names all have at least as many characters
as we need - don't worry more about it - yet.)
Here is an example of what your program should do (as
always user entries are in bold. ) :
program 2 your_name_here
Enter your last name: singer
Enter your first name: alvy
Enter your middle initial: j
Your birth year: 1935
Your birth month: 1
Your birth day: 13
Your sex (0 - male, 1-female): 0
Your license number is : S09507457182808935
