Farey Sequence Program.

I'm having problems getting this to work correctly and when I try to run it from the command line I get a Segmentation error. This program is for approximating the number using Farey Sequence. At the end of the loop the output needs to be in fraction form, the restrictions are for the loop to stop when either the
mediant denominator is larger than 1,000,000 or when the mediant is within 10-6
of x. Also the program should read the value of x from the command line, rather than prompting for it.
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
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

const double TOL=1E-6;

int main(int argc,char *argv[])  {
  int a=0, b=1, c=1, d=0;    	    //establishing endpoints
  double x;                            //the real number
  int mn = a + c;                    //mediant numerator
  int md = b + d;                    //mediant denom
  int med = mn/md;                    //mediant whole

  x = atof(argv[1]);


//1.compute mediant of endpoints
  while ( fabs(mn-x*md) >= md*TOL || md<1000000 )
  {

//2.determine whether x is > or < mediant
  //2.1 IF x larger than med, replace smaller endpoint with mediant

    if(med < x)
    {
    mn = a;
    md = b;
    }

    //2.2 IF larger than med, replace smaller endpoint with mediant
    else
    {
        mn = c;
        md = d;
    }
  }

//4. output last mediant
cout << mn << "/" << md << endl;


return 0;
}
Last edited on
closed account (48T7M4Gy)
Try:

double x = 0;

1
2
if( argc == 2)
		x = atof(argv[1]);


The reason is argv[1] is otherwise out of range given the way you are programming reading argv[]. Normally it is done with a loop based on the value of argc.
where should i put this if loop?
also i want my x value to be inputed directly from the command line (i am compiling and running this on ubuntu). I am unsure how to do this.

When I try:
./a.out 0.324235345
^some random value

into the command line nothing happens. I have to close out the terminal completely in order to type any new commands. What's happening to my cout? Why does no value for my mn/md appear?
Last edited on
closed account (48T7M4Gy)
The if statement goes just before the x = ... statement
closed account (48T7M4Gy)
http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean
ok. so i got it to work. It didn't have anything to do with x, and that is all working properly as is (I tried both with and without it). However, the problem now seems to be that the while loop isn't listening to my conditions. The only way I can get correct results is if I use || but i want the program to adhere to BOTH conditions (but I don't get acurate results with &&). Any ideas?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
#include <cmath>
#include <cstdlib>

using namespace std;

const double TOL=1e-6;

  int main(int argc,char *argv[]) {
  int a=0, b=1, c=1, d=0; 	//establishing endpoints
  double x = atof(argv[1]);	//the real number
  int mn, md; 			//mediant


//1.compute mediant of endpoints
  while ( md<1000000 || fabs(x*md-mn) <= TOL*md  )
  {

  mn=a+c;
  md=b+d;


//2.determine whether x is > or < mediant

	//2.1 IF x smaller than med, replace larger endpoint with mediant


	if(x*md < mn)
	{
	c = mn;
	d = md;
	}

	//2.2 IF larger than med, replace smaller endpoint with mediant
	

	else if (x*md > mn) 
	{
	a = mn;
	b = md;
	}

	else
	break;

  }

//4. output last mediant
 
  cout << mn << "/" << md << endl;


  return 0;
  }
Are you sure that the logic is correct for that "Farey Sequence"?
Topic archived. No new replies allowed.