A grade school teacher wants to develop a program to generate values to a series of numbers in different base systems. The teacher is limiting the bases to be between 2 and 9, inclusive. Each line of input will have a base value and a sequence of characters representing a nonnegative number in that base. However, each number (sequence of characters) has been placed “backwards”. That is the rightmost digit is first in the sequence, the second digit from the right is encountered next, and so on.
The program reads the digits one a time. As each digit is read, the program should translate that digit into its corresponding decimal value, and then multiply it by the appropriate power of the base. There is an arbitrary number of blanks between the base and the first digit in the sequence.
Suppose that the teacher wants the students to convert the binary number 1011.
The program would require this example input line:
The first number, 2, represents the base. Base 2 is designated as binary. The 1101 represents a character sequence of binary digits of the number to convert. IT IS BACKWARDS! This means that the first 1 is in the one’s position or 2 to the 0 power. The second 1 is in the two’s position or 2 to the 1 power. The 0 is in the four’s position or 2 to the second power. And lastly, the rightmost 1 is in the eight’s position or 2 to the third power. Remember the number that was to be converted was 1011.
The value of this number is 1 1 + 1 2 + 0 4 + 1 8 which equals 11. Please see the sample output on how values are to be displayed.
Ask your instructor to give another example in a different base.
However, the teacher’s grade school students have entered the data values. This data CANNOT be trusted as being valid data.
Your program needs to check for valid bases and if the base is invalid write out a message that says,
Invalid base given, throwing away the rest of the line.
To throw away a line of input use cin.ignore( ). Your instructor will talk about this in class. In addition, your program needs to check for valid digits in the given base. For example, if the base is 2, any digit equal to 2 or higher is NOT legitimate. Your error message should say (i.e. input line is : 2 234510)
For the given base 2, the number is NOT valid!
You will need to throw away the rest of the line here as well.
Lastly, your program is supposed to add up the values of all valid numbers given and print this value as the final part of the output. If there are NO valid inputted numbers given, then the sum is zero (0).
Again, please see the sample output on how values are to be displayed.
The following additional requirements must be followed:
Your main function should adhere to the following pseudocode:
--- Pseudocode ----
Set totalSum to zero
Read until a valid base is read or end of file
while not at end of file
write "For the given base ", base
Assign to numberValue the translated number from the input
if numberValue is not valid
Write " the number is NOT valid!"
Accumulate numberValue int totalSum
Write numberValue appropriately labelled
Read until another valid base is read or end of file
Write totalSum appropriately labelled
The above pseudocode suggests some functions. Keep these functions single-minded, performing a focused task that can be well-named. You should design pseudocode for each of these functions. All function bodies, including the body of main, should be NO more than 30 lines long, including braces, blank lines and comments.
You must NOT use arrays. You will lose ALL points if you do.
You must follow the programming standards.
You must follow the formatting of the sample I/O below.
You must thoroughly test your program.
You must use the following functions and you MUST add the documentation for the parameters for functions that have parameters.
To get credit for the assignment, your solution must minimally work on Test Case # 1 below.
Below are two sample runs.
They do NOT cover all cases.
Test Case # 1
Input for Run 1:
2 1101 3 1212 5 66 2 1111
The output for Test Run 1:
2 1101 For the given base 2, the decimal value of the input string is 11. 3 1212 For the given base 3, the decimal value of the input string is 70. 5 66 For the given base 5, the number is NOT valid! 2 1111 For the given base 2, the decimal value of the input string is 15. The total sum of all valid values is 96.
Test Case # 2
Input for Test Run 2:
1 hello 10 this is bad too -1 as well as this
Output for Run 2:
1 hello Invalid base given, throwing away the rest of the line. 10 this is bad too Invalid base given, throwing away the rest of the line. -1 as well as this Invalid base given, throwing away the rest of the line. The total sum of all valid values is 0