
In this lab, you do the same project from Lab 1 in C++ with the additional requirement that the program should let the user enter pairs of numbers as long as the user wants to test new pairs.
Recall that a digital river is a sequence of numbers where every number is followed by the same number plus the sum of its digits. In such a sequence 123 is followed by 129 (since 1 + 2 + 3 = 6, and 123+6=129), which is followed by 141.
We call a digital river river K, if it starts with the value K.
For example, river 7 is the sequence beginning with {7, 14, 19, 29, 40, 44, 52, … } and river 471 is the sequence beginning with {471, 483, 498, 519, … }.
Digital rivers can meet. This happens when two digital rivers share the same values. River 32 meets river 47 at 47, while river 471 meets river 480 at 519. We thus say that 519 is the meeting point of river 471 and river 880.
For this lab given two meeting digital rivers print out the meeting point.
You start with two numbers. Say 1078 and 1069. What you would do is find the successor of each:
- 1078 + 1 + 7 + 8 = 1094
- 1069 + 1 + 6 + 9 = 1085
Since 1085 is the least, we replace the 1069 with 1085, giving us the pair 1078 and 1085 and go again:
- 1078 + 1 + 7 + 8 = 1094
- 1085 + 1 + 8+ 5 = 1099
So we now replace the 1078 giving us the pair 1094 and 1099. Next:
- 1094 + 1 + 9 + 4 = 1108
- 1099 + 1 + 9+ 9 = 1118
So next is 1108 and 1099:
- 1108 + 1 + 1 + 8 = 1118
- 1099 + 1 + 9 + 9 = 1118
Since BOTH end on the same number, the rivers join, and we are done!
How to get the digits?
We can get the lowest digit with the following: n%10. E.g. 1078%10 = 8
We can remove the low digit with n//10. E.g. 1078//10 = 107
So we just need a while loop that:
- gets the lowest digit
- adds the digit to a sum
- removes the the lowest digit
- loops until the we get a zero after we remove the lowest digit (simple while loop)
Hints
- you should write code the sums up the digits of a number. This can be done as follows:
n = number to sum digits ofsum = 0while n > 0d = low digitsum = sum + digitdivide d by 10sum now holds the sum of the digits
- You will need a while loop that runs until the two rivers meet.
r_1 and _r2 are the starting valueswhile r_1 != r_2
if r_1<r_2, then replace r_1 with r_1+sum of the digits of r_1
otherwise, replace r_2 with r_2+sum of the digits of r_2print the matching value
Example input 1
3247
Example output 1
47
Example input 2
75
Example output 2
620
Test Data (run for each pair, copy output, place in file “runs.txt”)
| Name | River 1 | River 2 |
| We will meet at 47 | 32 | 47 |
| r1 < r2 | 57 | 78 |
| r1 > r2 | 7 | 5 |
| Hundreds | 483 | 456 |
| Thousands | 1158 | 2085 |
| River 2489 | 5026 | 2489 |
| River 13 | 13 | 14689 |
| Primes | 991 | 997 |
| Even Bigger | 15485863 | 13000000 |
Submit
Submit the project on onlineGDB and turn in the project link to Moodle. Be sure to include the standard information and the honor statement at the top of your program file. Any submission that is missing the Academic Integrity Statement will not be graded.
Grading
| Requirements | Grading Comments | Points | Score |
|---|---|---|---|
| Program passes all test cases | 70 | ||
| Fully commented code, with name, date, description, the honor statement, and comments on all functions. | 10 | ||
| Meaningful identifier names (variables, functions) | 10 | ||
| The program lets the user enter new pairs of numbers as long as the user wishes. | 10 | ||
| Total | 100 |
