The basics of programming in c++ for beginners

A task: Building a recurring decimal

There are (terminal input) right a rational fraction / b (Entry form plain fractions).
– a and b – whole numbers;
– if the fraction is correct, then a < b.

It is necessary to programmatically determine the period of the periodic fractions, and bring it in the format of a decimal point, species 0.12(34) (from school arithmetic course remember, it 0.12343434… – endless fraction).

Complicates the task of the, Tom, that number system records an infinite fraction can be arbitrary and is chosen at random (data entry).

For example, 10-cal system, we have to get:
1 / 12 = 0.08(3)
11 / 13 = 0.8(461538)
2 / 5 = 0.4
A 2-ary system, respectively:
17 / 47 = 0.(01011100100110001000001)

This is the 1st challenge. After solving this problem first, We set ourselves the task 2: to alter the program so, that at each step (determining the numbers) do a full search has previously encountered residues…

Decision:

First of all, remember as a rational fraction (1/3) converted into endless (0.3333… or 0.(3)) – it can be read in detail here – Handling of fractions to decimals and vice versa:

    • The loop is the quotient and remainder of the division of the numerator by the denominator.
    • private – this is another figure of infinite recording. Save the remnants of each step.
    • Multiply the balance in the radix – we will have a numerator for the next iteration step.
    • If the balance on some step is zero, the fraction – final.
    • Otherwise, look for the balance of those previously saved. If you found, we found during (then everything will be repeated periodically).
    • If not found, then we maintain the current balance and return to the calculation of the next discharge.

Properly, It's enough, to write the solution of the problem 1 (with the search for the remnants saved):

It is easy to see, the entire algorithm starts with a line 39 and it takes 12 strings – everything else is input and data preparation:

(The internal mechanics of what is happening is not entirely clear, therefore provides a diagnostic output mode – if you run the program with any parameter)

And now we see, what we need at each step search the resulting residue in an array (vector) in coincidence with the previously obtained. This is wasteful. Knowing, as the algorithm works, You can go on like this trick:

    • denominator N residue can only have values ​​of N 0…N-1
    • create an array of N elements, and mark its value -1, which means “a value balance is not met”…
    • but if at the next step, we obtain the value of M, in element [M] array write iteration number, where this value was…
    • Now, If after the next step, we get the value of K, then we do not need search this value throughout the array, and only need to check the item [K] an array to its positivity.

And that's what comes out of it:

2 thoughts on “A task: Building a recurring decimal

  1. The program does not work properly.
    The first digit of the period is erroneously assigned to the pre-period, and period, calculated by the program, starts with the second digit of the current period.

  2. I have one question, The author of the site will at least check the code that is sent to him? almost every program has a lot of errors and false calculations, not clear for beginners

Leave a Reply

Your email address will not be published. Required fields are marked *