Assignment 06

Due: Thursday, Oct. 11, 2018, at noon, 100 points

For this assignment you will submit (in the usual way) multiple files. These files will contain a C++ program that you have named appropriately, each file with an appropriate name and appropriate extension. Be sure that your program compiles using the g++ compiler before you submit it. To compile a program with multiple files, use the usual g++ command but instead of the single file you compiled before, compile *.cpp and the compiler will "grab" all the cpp files in that directory. So, you see, it's important that you put all the files for a particular program in their own directory! You don't have to list the header files; that will happen automatically. Use cssubmit in the usual way.

Background: assignment #4. You might have noticed while you were coding hw 4 that you were repeating code...but might not have also. As I said in class, repeat code is NOT a good situation. Why? First, a waste of time. Second, if you ever need to fix the code, you have to fix it in multiple places and you are almost guaranteed to make a mistake in one of those multiple places. Furthermore, this angers Krusty and he becomes HUGE and irate (see image below).1

This is just one motivation for using functions in your programs. I've discussed others in class. You know what they are.

Specifications: Your program is to be a re-coding of the solution to hw 4, but this time using functions for all the good reasons that you and I both know. But Krusty is wanting you to make some modifications to your program's functionality. A new menu of options will reflect those changes:

Options

-----------

1. Enter a non-negative integer, x [5-digit or less]

Note: Important...we want you to replace the "offending" digits in order right to left (units, tens, hundreds, etc)

Hmmm, interesting. So, if option 4 is chosen, your program will make the combination of x and n be valid by changing the value of the base, n. How? It will change n to the smallest integer that will make x a valid base n number. For example, suppose 14673 is entered for x, and suppose 4 is entered for n. Option 3 will clearly report that 14673 is NOT a base 4 number. Choosing option 4 will then cause n (the base entered by the user) to be changed (by your program) to .... ? What value? Well, the answer is 8, since the largest digit in x is 7. Furthermore, if option 5 is chosen after option 3 reports an invalid x and n, your program will make the combination valid by changing x. It will replace the digits of x entered by the user which make that x not a base n number with new, randomly chosen digits so that x IS a base n number. For the example above, n = 4 remains unchanged but x = 14673 would be changed to (perhaps) 12103. Notice that the first and last digits didn't change, but the middle three digits changed because they were all bigger than or equal to 4. The choice of replacements must be random! (I promise, I chose 2 and 1 and 0 randomly when I made up this example! Of course, your code will do this.)

2. Enter an integer base, n. [ 2 <= n < 10 ]

3. Verify x is a valid base n number

4. Force validation with new (smallest) base assignment

5. Force validation with new (random digits in) x

6. Convert x to base 10

7. Quit

Also Note: You will graded on how well you use functions for this assignment. Make good design decisions, and make your code clean and clear. Don't forget to document your code! What functions to make is up to you; we will not dictate functions this time nor in the future.

After having made these changes, you need to make the whole program work properly.

    • Options 1 and 2 can be chosen in any order.

    • Options 3, 4, 5, and 6 require both options 1 and 2 first

    • The functionality of options 4 and 5 will not be implemented unless the current x and n are an invalid pair; if they are valid, a message is to be output stating so.

    • Option 6 runs only if the current x and n are a valid pair; error message otherwise.

When you submit: To make your output uniform for the graders, start by seeding your random number generator with 7 . Then, during the run at submission:

  1. choose option 1 and enter 2345

  2. choose option 2 and enter 7

  3. choose option 3

  4. choose option 6

  5. choose option 4

  6. choose option 5

  7. choose option 1 and enter 2845

  8. choose option 3

  9. choose option 4

  10. choose option 6

  11. choose option 2 and enter 6

  12. choose option 3

  13. choose option 5

  14. choose option 6

  15. choose quit

As usual, ask your instructor or Krusty if you have any questions.

1Serving suggestion...