Question: Your HW1 program starts by printing out to the screen a quick description of what an HCN is. The program continues by asking the interactive
Your HW1 program starts by printing out to the screen a quick description of what an HCN is. The program continues by asking the interactive user for an integer from 1 to 10,000,000, inclusive. I will call that number X. The prompt should be explicit about what X should be.
If the user enters something that is not an integer, then your program should give an appropriate error message and reprompt for X. If the user enters something that is an integer, but is outside the specified range, then the program should give a different, appropriate error message and reprompt for X. This should continue until the user enters an X that is legal according to the specification above.
When a legal X has been entered by the interactive user, your program should print out the HCN that is closest to X, with an explanation. Perhaps there could be a tie between two HCNs where HCN1 < HCN2, and (X HCN1) = (HCN2 X); if that is the case, the program should print out both HCN1 and HCN2, with an appropriate explanation. If X is itself an HCN, then X is the closest to X, and your program should print out an appropriate explanation.
With a little thought, you can imagine at several different strategies to approach this problem:
1. Have your program generate a list of HCNs long enough to be sufficient for the 10,000,000 limit described in the specification, and use that list in solving the problem. This has a major advantage if (in the future) someone wanted to expand the 10,000,000 limit. Your program could change one constant, and would be ready to go.
2. Find a list of HCNs on the internet, and hard code in the necessary numbers for your program. This is less useful than point 1 if someone decides they want to improve the program by raising the 10,000,000 limit.
3. Find a way to determine if a particular number is an HCN, and look at X, then X-1 and X+1, then X-2 and X+2, and so on until you find an HCN (or two HCNs).
4. Look for code in your favorite language that does something useful for you when trying to solve this problem. It is OK to find and use code on the internet to do this, as long as you GIVE CREDIT WHERE CREDIT IS DUE by mentioning in your opening comments the source of the code. Also, give a credit using internal comments where you use the code you found.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
