Trying to validate a number based on certain conditions

Hi folks,

So I am learning C++, and I agree with others when they say guiding through a problem is better than just providing with the solution. So please bear with me.

The condition:

For a SIN to be valid it:
1
2
3
4
5
6
7
8
To obtain the weighted sum, take the digit to the left of the check 
digit and then every second digit leftwards.  Add each of these digits to 
itself.  Then, add each digit of each sum to form the weighted sum of the 
even positioned digits.  Add each of the remaining SIN digits (except the 
check digit) to form the sum of the odd positioned digits.  Add the two sums 
and subtract the next highest number ending in zero from their total.  If 
this number is the check digit, the whole number is a valid SIN; otherwise, 
the whole number is not a valid SIN.


So, example,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
 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
 add the other alternates   1+3+5+7       = 16
 total                                    = 43
 Next highest integer multiple of 10      = 50
 Difference                               =  7
 
Matches the check digit, therefore this number is a valid SIN


How would I go about implementing this? My SIN is of the type int, not an array; so I cannot simply cycle through the SIN (this cannot be changed).

Please advise
Thanks everyone
What do you mean by "cannot be changed"?
What prevents you from creating a temporary array?

Think about:
(foo / 1) % 10
(foo / 10) % 10
(foo / 100) % 10
(foo / 1000) % 10
keskiverto,

Thank you for your help; would you say this is a good way to go about this?

1
2
	int evens = (((SIN / 10) % 10) + ((SIN / 1000) % 10) + ((SIN / 100000) % 10) + ((SIN / 10000000) % 10));
	int odds = (((SIN / 100) % 10) + ((SIN / 10000) % 10) + ((SIN / 1000000) % 10) + ((SIN / 100000000) % 10));


I see you mentioned "array," and I feel like that would be a better way to go about this.
Last edited on
So I made it into an array,

1
2
3
4
5
6
7
8
9
10
11
12
		int sum = 0;
		int check = sin % 10;
		int evens[4] = { (((sin / 10) % 10) * 2), (((sin / 1000) % 10) * 2), 
                                 (((sin / 100000) % 10) * 2), (((sin / 10000000) % 10) * 2) };
		int odds[4] = { (((sin / 100) % 10) * 2), (((sin / 10000) % 10) * 2), 
                                 (((sin / 1000000) % 10) * 2), (((sin / 100000000) % 10) * 2) };
		for (int i = 0; i < 4; i++)
		{
			evens[i] = evens[i * 2];
			sum += (((evens[i] / 1) % 10 + ((evens[i] / 10) % 10)));
			sum += odds[i];
		}


How would I be able to find the next highest integer multiple of 10?
1
2
Add the two sums and subtract the next 
highest number ending in zero from their total
Last edited on
> How would I go about implementing this? My SIN is of the type int, not an array;
> so I cannot simply cycle through the SIN (this cannot be changed).

Divide and conquer.
Break up the problem into small steps, each of which is simple; write a short function for each step.
Test each step and verify that it is working correctly before you move on to implement the next step.
Finally, put all these small steps together to get the desired program / high-level function.

Spoiler (simple brute force, without using arrays): http://coliru.stacked-crooked.com/a/fada681a53de7b94
Topic archived. No new replies allowed.