Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Once you have read a number from the input file, you will need to reverse this number mathematically. The easiest way to reverse a number

image text in transcribedimage text in transcribed

Once you have read a number from the input file, you will need to reverse this number mathematically. The easiest way to reverse a number with an unknown number of digits mathematically, you need to be able to pull off the last digit of the number. Using the first number in our example input file, 123456, we would need to be able to separate it into two different numbers: 12345 and 6. What arithmetic operator or operators might assist in this endeavor? Will addition or subtraction help? Not really. So that leaves mulitplication and division. Does multiplication seem to apply? Not really. So that leaves division. Recall that our number system is a base ten system. This means the right most digit is ones (or 10^0), the next one to the left is tens (10^1), the next one to the left is hundreds (10^2) and so on. This means that as you progress from right to left, each digit is a multiple of 10 more than the one to the right. What happens if you divide 123456 by 10? Remember, if both operands of the division operator are integers, the result will be an integer. Dividing the number by 10 will remove the last digit. That's all well and good, but how do we obtain that last digit that gets dropped? When both operands of the division operator are integers, integer division or long division is performed. It would look something like this:

__1____ 10 | 123456 10 2

__1____ 10 | 123456 10 23 __12___ 10 | 123456 10 23 20 3 __123__ 10 | 123456 10 23 20 34 30 4 __1234_ 10 | 123456 10 23 20 34 30 45 40 5 __12345 10 | 123456 10 23 20 34 30 45 40 56 50 6

We actually have two operators dealing with integer division. The division operator determines the quotient while the modulus operator determines the remainder.

__12345 10 | 123456 10 23 20 34 30 45 40 56 50 6

Now that we know how to grab that last digit and then remove it from the number, we need to be able to do this repeatedly for an unknown number of digits. If you overwrite, for our example, 1234 with 123 in a single variable, you can create a loop based on that one variable. Focusing on the division operator, 1234 divided by 10 is 123, 123 divided by 10 is 12, 12 divided by 10 is 1 and finally, 1 divided by 10 is 0. This would be an event-controlled loop where the event would be for the variable to have a value of 0.

You can build up a number digit by digit just as you can break down a number digit by digit. The opposite of division is multiplication. Since you use division to do the breaking down, use multiplication to build a number back up. The first digit obtained from our example number of 1234 is 4. The next is 3. In order to join these two numbers together in the correct order, the 4 needs to move from the ones position to the tens position. If you multiply 4 by 10 you get 40. You can then add the 3 to get 43. The next digit obtained from our example number is 2. 43 multiplied by 10 becomes 430. Add the 2 and you have 432. Do the same thing again, multiply by 10 and add the next digit to get 4321.

Add code to the program that will repeatedly read an integer from the input file, reverse the number and then write the expected output to the output file. Be sure to close both the input file and the output file when you run out of data.

Reminder: Write your program using incremental development. For example, write code that creates an integer and initializes it to 1234, for example. Then write code that will mathematically separate the number into 123 and 4. Test this code. Then write code that will strip off the last digit until there the number is 0. Test this code (display each digit to the screen for verification). Continue to write small amounts of code and then test that code before moving on.

Here's an algorithm to help out.

Algorithm
get the original number get the last digit from the original using the modulus operator intialize the reversed number to the last digit of the original number remove the last digit from the original number while loop (looping until the number to reverse is 0) get the last digit from the original using the modulus operator multiply the reversed number by 10, then add the last digit just obtained remove the last digit from the original number using the division operator

Using c++. I am learning while and for right now.

Please do not use so difficult information. first, I usually use int main() {}

Thank you for your helping

DESCRIPTION You are strongly encouraged to write this assignment using incremental development. For example, you might first attempt to successfully read all values in the input file. Once you can do this, begin the process of reversing a number while and event-controlled repetition Reading an unknown number of values from an Input file The file numbers.txt consists of an unknown number of integers, with each number on a separate line. You are to write a program that will read each integer value from the file numbers. txt, reverse the number mathematically and then store the new number in the file reversed. txt, along with the original number as shown below. Read on for more details and assistance. For testing purposes the file numbers.txt has been provided with the following values. 3 numbers.txt 123456 173 693412 7239 64564564 This sample input file would produce the following output file. Use a field width of 20 on all output. reversed.txt Original Number 123456 73 693412 7239 64564564 Reversed Number 654321 37 214396 9327 46546546 The integers in numbers.txt along with their reversed values will be within the range of values which can be stored in the unsigned int data type. Only the int and unsigned int data types should be used to reverse and store these numbers in your program. Additionally, the integers in the input file will not end with the digit 0. Recall that the stream extraction operator can be used to read numbers from a plain text file once the file has been opened in the program. Since our input file contains only numbers, each on a separate line, the stream extraction operator can be used again and again to read all the numbers in the file. Since the input file may contain any number of values, we need a way to determine if there is any more data in the file; there are multiple ways to do this. One way is to use the eof() member function. eof stands for end of file. This function will return true if the end of the file has been reached. Use this function by adding .eof() to the end of the file handle. Nothing should be inside the parenthesis. Another way to check for the end of file is to verify the attempt to read by the stream extraction operator. If a value was read successfully, the operator results in true. The following code demonstrates checking for successful input. Code Illustration if (myInput File >> myvalue) cout

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

Describe your ideal working day.

Answered: 1 week ago