Continued Fraction Expansion using called data

I am writing a program to calculate real numbers from continued fraction expansions. I have a text file named “problem1data.txt” will contain the expansions, with one per line, which consist of an integer, followed by a semicolon, followed by additional integers, separated by commas. It looks like:

3;7,15,1,292,1,1,1,2,1,3
1;2,2,2,2,2,2
2;1,2,1,1,4,1,1,6,1,1,8

The program should process this file, compute the continued fraction
expansions of each line, and write them to an output file named “problem 1 output.txt” with 16 digits of precision.

I started working on calling the data and have attached that code, please correct if there are mistakes. I need help calculating the real numbers now from the continued fractions. I don't know how to use the called up data to do this. Someone suggest storing the integers in a vector and then immediately calculating by starting at the back of the first fraction and iterating back through the numbers (using the reciprocal and the previous number at every iteration) and writing the result to a file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 #include <iostream>
#include <fstream>
#include <sstream>
#include <cctype>
#include <vector>
#include <cmath>
#include <math.h>
#include <string>
#include <iomanip>
#include <random>
#include <functional>

using namespace std;

int main(){
    ifstream input_file("problem1data.txt");
    
    if(!input_file.is_open()){
        cout << "The file did not open!" << endl; // this was in an example code but idk
        return 0;
    }
    
    if(input.is_open()){
        while(!input.eof()){
            string first_one;
            getline(input, first_one';'); // reading the number
            int first_number = stoi(first_one); // i think this is converting to integer
            while(getline(idk, next, ',')){
                int next_number = stoi(next);
                integers.push_back(next_number);
            }
        }
    }
}
storing the integers in a vector and then immediately calculating by starting at the back of the first fraction and iterating back through the numbers (using the reciprocal and the previous number at every iteration)


Yes, that's certainly one way of doing it, if there is no pattern to the input data. See:
https://en.wikipedia.org/wiki/Continued_fraction

However, you need to read your integers first AND CHECK THEM (just write them out).

Just by inspection of your code (which plainly won't compile):
- you are pushing back into a vector<int> called integers that you haven't defined
- you changed your input file stream from input_file to input ... and then, bizarrely, to idk
- you didn't declare variable next (as a string)
- you have incorrect syntax in getline (missing a comma between arguments) on line 26
- you should test the state of the stream after an attempted read, not use eof to detect end of file
- if you don't know why you are using an included header then don't do so (tell me why you think you will use random or functional here?)

You appear to be cobbling together code from other examples without much understanding of what it is doing.

You should fix the file reading first, before you even think about evaluating continued fractions. Check all the integers that you (think you have) read, by writing them out to screen. Only after that should you worry about continued fractions. When you do get to that stage, be careful with integer division.

You can check your continued fractions when you finally get to them, because the examples that you give are (truncated) values of pi, square root of 2 and e.
Last edited on
Topic archived. No new replies allowed.