100% found this document useful (1 vote)
58 views

Subject: PRF192-PFC Workshop 06 Nguyen Tien Dat - DE160068 Objectives: Managing Arrays

The document describes a program to validate Canadian Social Insurance Numbers (SINs) and International Standard Book Numbers (ISBNs) using checksum algorithms. It provides sample SIN and ISBN validation examples. For SINs, it sums digits in alternating number/checksum arrays to calculate the checksum. For ISBNs, it multiplies each digit by a weight and takes the sum modulo 11. The document also includes code for ISBN and SIN validation programs that take a number as input and check if it is a valid SIN/ISBN by applying the respective algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
58 views

Subject: PRF192-PFC Workshop 06 Nguyen Tien Dat - DE160068 Objectives: Managing Arrays

The document describes a program to validate Canadian Social Insurance Numbers (SINs) and International Standard Book Numbers (ISBNs) using checksum algorithms. It provides sample SIN and ISBN validation examples. For SINs, it sums digits in alternating number/checksum arrays to calculate the checksum. For ISBNs, it multiplies each digit by a weight and takes the sum modulo 11. The document also includes code for ISBN and SIN validation programs that take a number as input and check if it is a valid SIN/ISBN by applying the respective algorithms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Subject: PRF192- PFC

Workshop 06
Nguyen Tien Dat – DE160068

Objectives: Managing arrays

Sample: Canadian SIN (Social Insurance Number)

SIN: 193 456 787 | check digit is 7 add first set of alternates to themselves 
9 4 6 8 9 4 6 8 18 8 12 16
add the digits of each sum 1+8+8+1+2+1+6 = 27 (T1)
add the other alternates 1+3+5+7 = 16 (T2)
total = T1+T2 = 27+16=43
Next highest integer multiple of 10 T3= 50 (50>43).
Difference T3-total = 50-43= 7 Matches the check digit, therefore this SIN is valid 

SIN: 193456787
N0 N1 N2 N3 N4 N5 N6 N7 N8 N9
1 9 3 4 5 6 7 8 7

9 4 6 8 9 4 6 8 18 8 12 16
C0 C1 C2 C3 C4 C5 C6 C7 C8 C9 C10 C11 C12

Algorithm for checking whether a number is a Canadian SIN or not

Use the array N, 10 elements, N[0] is not used


Use the array C, 12 elements, C[0] is not used
From n, computing N[i]:
From N, computing C[i]:
Compute
T1= C9/10 + C9%10 + C10/10 + C10%10 + C11/10 + C11%10 + C12/10 + C12%10
T2= N1 + N3 + N5 + N7;
Total= T1+T2
T3= (Total/10+1) *10; ( Total=43  T3 = (4+1)*10
If (T3-Total == N9) return “Valid”
else return “Invalid”
Refer to the sample above, write the following problem.

Problem 1 (4 marks)

An ISBN consists of exactly 10 digits.  The rightmost digit is the check digit.  The check digit is
validated modulo 11. 

 multiply each digit from the first to the ninth by a weight from 10 to 2 respectively (the first
digit by 10, the second by 9,..., the ninth by 2). 
 the sum of the products plus the check digit should be divisible without remainder by 11. 
 if there is a remainder, the whole number is not a valid ISBN. 

Consider the following example:


ISBN 0003194876 | check digit is 6 add first set of alternates to themselves
0 0 0 3 1 9 4 8 7 10 9 8 7 6 5 4 3 2 0 0 0 21 6 45 16 24 14 = 126
add check digit 6 total 132 divide by 11 12 remainder 0 Therefore this ISBN is valid.

I1 I2 I3 I4 I5 I6 I7 I8 I9 I10
0 0 0 3 1 9 4 8 7 6

C1 C2 C3 C4 C5 C6 C7 C8 C9
0*10=0 0*9=0 0*8=0 3*7=21 1*6=6 9*5=45 4*4=16 8*3=24 7*2=14

T= C1+ C2+C3+C4+C5+C6+C7+C8+C9 + I10; (T=132)


If (T%11==0) print out “Valid” else print out “Invalid”

Write a program that will accept a number (>=1 000 000 000) then show whether the number is an ISBN or
not.

ISBN Validator ============== ISBN (0 to quit): 0003194876 


This is a valid ISBN.
ISBN (0 to quit): 0003194875
This is not a valid ISBN. ISBN (0 to quit): 0
Have a Nice Day!

#include <stdio.h>
int checkISBN (int n) {
int N[11];
int C[11];
int k[9] = {10,9,8,7,6,5,4,3,2};
int i, result = 0;
int sum;
if (n >= 1000000000) {
for (i = 10; i > 0; i--) {
N[i] = n%10;
n = n/10;
}
sum = 0;
sum = sum + N[10];
for (i = 1; i < 10; i++) {
C[i] = k[i-1] * N[i];
sum = sum + C[i];
}
if (sum%11 == 0) result = 1;
}
return result;
}
int main() {
int n;
do {
printf("Enter ISBN (O to quit): ");
scanf("%d", &n);
if (checkISBN(n) == 1) puts("This is a valid ISBN\n");
else puts("This is not a valid ISBN\n");
}
while (n != 0);
return 0;
}
Problem 2 (6 marks)
Develop a C-program that helps user managing an 1-D array of real numbers(maximum of 100 elements) ,
with initial number of elements is 0, using the following simple menu:
1- Add a value
2- Search a value
3- Print out the array
4- Print out values in a range
5- Print out the array in ascending order
Others- Quit

- When the option 1 is selected, user will enters a value then it is added to the array
- When the option 2 is selected, user will enters a value then number of it’s existences will be printed out.
- When the option 3 is selected, values in the array will be printed out.
- When the option 4 is chosen, user will enter 2 values, minVal and maxVal, the values in array which are
between minVal and maxVal are printed out (minVal <=value<=maxVal)
- When the option 5 is chosen, values in array will be printed out in ascending order but their position
are preserved. ( sorting based their pointers only)

#include <stdio.h>
#include <math.h>
const int max = 100;
double checkNum(int *n, char buf);
void addArray(int array[], int *size);
void searchArray(int array[], int size);
void printArray(int array[], int size);
void arrayAscend(int array[], int size);
void showOp();
void rangeOfArray(int array[], int size);
int main() {
int array[max];
int size = 0;
int choice, n, buf;
do {
showOp();
do {
choice = checkNum(&n, buf);
if(choice < 1 || choice > 6) {
printf("Nhap 1-6: ");
}
}
while(choice < 1 || choice > 6);
switch(choice) {
case 1: {
addArray(array, &size);
printf("\n");
break;
}
case 2: {
searchArray(array, size);
printf("\n");
break;
}
case 3: {
printArray(array, size);
printf("\n");
break;
}
case 4: {
rangeOfArray(array, size);
printf("\n");
break;
}
case 5: {
arrayAscend(array, size);
printf("\n");
break;
}
}
}
while(choice != 6);
}
void showOp(void) {
printf("\n1- Add a value.");
printf("\n2- Search a value");
printf("\n3- Print out the array");
printf("\n4- Print out values in a range");
printf("\n5- Print out the array in ascending order");
printf("\n6- Quit");
printf("\n__________________________________________");
printf("\nChoose options: ");
}
double checkNum(int *n, char buf) {
do {
scanf("%d", n);
scanf("%c", &buf);
fflush(stdin);
if(buf != 10){
printf("\Enter number !!! ");
printf("\nRetype: ");
} else {
return *n;
}
}
while(buf != 10);
}
void addArray(int array[], int *size) {
printf("\nSize: ");
scanf("%d", size);
for (int i = 0; i <= *size - 1; i++) {
printf("\nArray[%d]: ", i);
scanf("%d", &array[i]);
}
printf("\n--------------");
printf("\nDone!");
}
void searchArray(int array[], int size) {
if (size == 0) {
printf("\nNothing to printf!!");
return;
}
int key;
printf("\nEnter key: ");
scanf("%d", &key);
int count = 0;
printf("\nThe position of %d is: ", key);
for (int i = 0; i<= size -1; i++) {
if (array[i]== key) {
printf("%5d", i);
count++;
}
}
if (count == 0) {
printf("\nThere is not in the array!", key);
}
}
void printArray(int array[], int size) {
if (size == 0) {
printf("\nNothing to printf!!");
return;
}
printf("\n");
for (int i = 0; i <= size - 1; i++) {
printf("%5d", array[i]);
}
}
void rangeOfArray(int array[], int size) {
if (size == 0) {
printf("\nNothing to printf!!");
return;
}
int min, max;
printf("\nMinVal: ");
scanf("%d", &min);
printf("\nMaxVal: ");
scanf("%d", &max);
printf("\nThe values in the array are between min & max: ");
for (int i = 0; i <= size - 1; i++) {
if (array[i] >= min && array[i] <= max) {
printf("%5d", array[i]);
}
}
}
void arrayAscend(int array[], int size) {
if (size == 0) {
printf("\nNothing to printf!!");
return;
}
int minIndex;
int i, j;
for (i = 0; i <= size - 1; i++) {
for (j = i + 1; j <= size - 1; j++) {
if (array[i]>array[j]) {
int t = array[i];
array[i] = array[j];
array[j] = t;
}
}
}
printArray(array, size);
}

You might also like