containing loop

when I compiling this program it gives me this error
//////////////////////////////////////////////////
////////////////////////////////////////////////
In function 'double inflexion(double*)':
27:1: warning: control reaches end of non-void function [-Wreturn-type]
In function 'int ninflexion(double*)':
37:1: warning: control reaches end of non-void function [-Wreturn-type]
In function 'int main(int, char**)':
69:15: warning: iteration 49u invokes undefined behavior [-Waggressive-loop-optimizations]
68:3: note: containing loop
60:39: warning: iteration 49u invokes undefined behavior [-Waggressive-loop-optimizations]
57:4: note: containing loop
/////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////

and I don't know how to solve it please someone who can find me where is the problem .

#include <iostream>
#include <cmath>
#include <fstream>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <stdio.h>
#include <math.h>
using namespace std;

const double pi=3.141592;
const double d=65;
const double c1=0.8, c2=7.5, c3=48;
const int n=50;
const double delta=0.01;

double inflexion(double x[]){
double dif,punto;
for(int i=n-1;i>1;i--){
dif=abs(x[i]-x[i-1]);
if(dif<0.01){
punto=x[i-1];
return punto;
}
}
}
int ninflexion(double x[]){
double dif;
int punto=0;
for(int i=n-1;i>1;i--){
dif=abs(x[i]-x[i-1]);
punto=punto+1;
if(dif<0.01)
return n-punto;
}
}

int main(int arg,char *argv[])
{
cout << setprecision(10);
double x,y;
x=atof(argv[1]);
y=atof(argv[2]);

double t[n-1];
double xv[n-1];
double yv[n-1];

double pyinf;
int infn;

ofstream fil2;
fil2.open("BTA60mm.txt");
fil2 << setprecision(10);

for(int i=0;i<n;i++){
double j=i*0.5;
double r=(j-c3)/30;
t[i]=c1+c2*exp(-0.5*(pow(r,10)));
xv[i]=x*(d*tan(j*pi/180)-sin(j*pi/180)*t[i]);
yv[i]=y*cos(j*pi/180)*t[i];
}

pyinf=inflexion(yv);
infn=ninflexion(yv);

for(int i=0;i<n;i++){
yv[i]=yv[i]-pyinf;
}

double xinf,yinf,m;
xinf=xv[infn];
yinf=yv[infn];
m=yinf/xinf;
yv[0]=0.00001;

for(int i=1;i<infn;i++){
yv[i]=m*xv[i];
}
for(int i=0;i<n;i++){
fil2<<xv[i]<<" "<<yv[i]<<endl;
}
return 0;
}
PLEASE learn to use code tags, they make reading and commenting on source code MUCH easier.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

HINT: you can edit your post and add code tags.

Some formatting & indentation would not hurt either
1
2
3
4
5
6
7
8
9
10
11
a typed function must return a value. 

int foo()
{
    if(1==2)
      return 3; //this does not happen if 1 does not equal 2.  
      ///nothing is returned: the return was skipped. 
     //control (the place in the program where it was executing) 
    //has reached the end (here) of a  non void function (its type int) without return. 
}


the infinity (?) loops over i appear to go out of range in the array bounds. This is undefined.
you also take the undefined index via uninitialized variables.
Last edited on
@abdelrx01

In function 'double inflexion(double*)':
27:1: warning: control reaches end of non-void function [-Wreturn-type]

Unless a particular condition (which isn't correct BTW) is met, the routine inflexion will not return a value. But it promised to return a double, didn't it: double inflexion(double x[])


In function 'int ninflexion(double*)':
37:1: warning: control reaches end of non-void function [-Wreturn-type]

Same issue(s), different function.


69:15: warning: iteration 49u invokes undefined behavior [-Waggressive-loop-optimizations]
68:3: note: containing loop
60:39: warning: iteration 49u invokes undefined behavior [-Waggressive-loop-optimizations]
57:4: note: containing loop

You declared arrays t[], xv[] and yv[] with one too few elements for the loops that initialise them:
1
2
3
double t[n-1];
double xv[n-1];
double yv[n-1];

These should be declared with length n, not n-1.


Your compiler didn't pick it up, but the following two lines are (currently) illegal in standard c++ because the array size isn't known at run time:
1
2
xinf=xv[infn];
yinf=yv[infn];



As @FurryGuy says, please use code tags so that it preserves indentation and we can read your code easily. It would also be useful to state what your code is supposed to do. As far as I can tell it's working out the parametric coordinates of some space curve - but who knows.
Last edited on
For C++, the includes are:

1
2
3
4
5
6
7
8
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <ctime>
#include <iomanip>
#include <cstdio>
#include <cmath> 


Why are time.h (ctime) and stdio.h (cstdio) included?
@lastchance (5669)
thank you, my dear friend.
with this c++ code, I want to simulate a bow-tie filter exactly the filter thickness at each angle theta of the ray coming from the source [computed tomography].

T (φ) = c 1 + c 2 exp −0.5(|φ − c 3 |/30) 10 ,
0 o < φ < 27.5 o
http://dx.doi.org/10.1118/1.3264616
This 'runs' but you have a lot of explaining to do regarding the correct form of the command line data input - see the parser comment.

By the look of it you would be better not bothering with that and just get the two values via cin statements.

Your data file handling and setprecision stuff needs work. Small point but if you open a file you should explicitly close it.

I'm no tomographist but this code appears very complicated if all it's doing is applying the formula you quoted.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <cmath>
#include <fstream>
//#include <stdlib.h>
//#include <time.h>
#include <iomanip>
//#include <stdio.h>
//#include <math.h>

using namespace std;

const double pi=3.141592;
const double d=65;
const double c1=0.8, c2=7.5, c3=48;
const int n=50;
//const double delta=0.01; // <-- NOT USED

double inflexion(double x[])
{
    double dif,punto;
    for(int i=n-1; i>1; i--)
    {
        dif=abs(x[i]-x[i-1]);
        if(dif<0.01)
        {
            punto = x[i-1];
            return punto;
        }
    }

    return -999.999; // <-- WHAT IS THE CORRECT RETURN VALUE?
}

int ninflexion(double x[])
{
    double dif;
    int punto=0;
    for(int i=n-1;i>1;i--)
    {
        dif=abs(x[i]-x[i-1]);
        punto=punto+1;
        if(dif<0.01)
            return n-punto;
    }
    return -999; // <-- WHAT IS THE CORRECT RETURN VALUE?
}

int main(int argc, char *argv[])
{
    // NEED AN ARGUMENT PARSER
    if (argc < 2)
    {
        std::cerr
        << "Usage: " << argv[0] << "\n*** SOME MANPAGE MESSAGE ***\n";
        return 1;
    }

    cout << setprecision(10);
    double x, y;
    x = atof(argv[1]);
    y = atof(argv[2]);

    double t[n-1];
    double xv[n-1];
    double yv[n-1];

    double pyinf;
    int infn;

    ofstream fil2;
    fil2.open("BTA60mm.txt");
    fil2 << setprecision(10);

    for(int i=0;i<n;i++){
        double j=i*0.5;
        double r=(j-c3)/30;
        t[i]=c1+c2*exp(-0.5*(pow(r,10)));
        xv[i]=x*(d*tan(j*pi/180)-sin(j*pi/180)*t[i]);
        yv[i]=y*cos(j*pi/180)*t[i];
    }

    pyinf=inflexion(yv);
    infn=ninflexion(yv);

    for(int i=0;i<n;i++){
        yv[i]=yv[i]-pyinf;
    }

    double xinf,yinf,m;
    xinf=xv[infn];
    yinf=yv[infn];
    m=yinf/xinf;
    yv[0]=0.00001;

    for(int i=1;i<infn;i++){
        yv[i]=m*xv[i];
    }
    for(int i=0;i<n;i++){
        fil2<<xv[i]<<" "<<yv[i]<<endl;
    }
    return 0;
}
abdelrx01 wrote:
with this c++ code, I want to simulate a bow-tie filter exactly the filter thickness at each angle theta of the ray coming from the source [computed tomography].

T (φ) = c 1 + c 2 exp −0.5(|φ − c 3 |/30) 10 ,
0 o < φ < 27.5 o
http://dx.doi.org/10.1118/1.3264616


OK, your expression for t[] is equation (19) in that reference. Where are your expressions for xv[] and yv[] in the paper? Also, Table 1 in the paper gives c2 as 6.0 (for aluminimum) rather than 7.5.

If your routine inflection() is supposed to return a point of inflection then it won't (looks more like a turning point/stationary point). Not sure what you are trying to do here.
Last edited on
This also runs and produces some output to file.
I've made some changes to make it clear (to me at least) wtf goes on with constants vs variables but this does not mean the program does what it you meant it to do.
The journal(s) seem to be tackling a more complex problem, even involving Monte Carlo methods. maybe that's why you included time/math/rand but didn't use them?
You could make a lot of use of ordinary coding conventions with meaningful variable names and adopting the principles of self-documenting code along with selective but effective commenting. Single letter variable names, to put it bluntly, is bad practice and essentially crap.
The constant LIMIT could possibly introduce an off-by-one-error with what I have done? I have not checked.

Be careful you don't burn someone :)

Well at least it spits out some text.

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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <iostream>
#include <cmath>
#include <fstream>
#include <iomanip>

const double D{65};
const double C1{0.8}, C2{7.5}, C3{48};
const int LIMIT{50};

const double DELTA = 0.01;

double inflexion(double x[])
{
    double dif{0}, punto{0};
    for(int i = LIMIT - 1; i>1; i--)
    {
        dif = abs( x[i]-x[i-1] );
        if(dif < DELTA)
        {
            punto = x[i-1];
            return punto;
        }
    }
    return punto; // <-- WHAT IS THE CORRECT RETURN VALUE?
}

int ninflexion(double x[])
{
    double dif;
    int punto{0};
    for(int i = LIMIT - 1; i > 1; i--)
    {
        dif = abs( x[i] - x[i-1] );
        punto++;
        if(dif < DELTA)
            return LIMIT - punto;
    }
    return punto; // <-- WHAT IS THE CORRECT RETURN VALUE?
}

int main()
{
    double x, y; // WHO KNOWS WHAT THEY ARE??
    x = 0.8; // atof(argv[1]); // ?????????????
    y = 0.75; //atof(argv[2]); // ?????????????

    double t[LIMIT];
    double xv[LIMIT];
    double yv[LIMIT];

    double pyinf;
    int infn;

    std::ofstream file_2;
    file_2.open( "BTA60mm.txt" );

    for(int i = 0; i < LIMIT; i++)
    {
        double j = i*0.5;
        double r = (j - C3) / 30;
        t[i] = C1 + C2 * exp( -0.5 * (pow(r,10) ) );
        xv[i] = x * ( D * tan( j * M_PI/180 ) - sin( j * M_PI/180) * t[i] );
        yv[i] = y * cos(j * M_PI/180) * t[i];
    }

    pyinf = inflexion(yv);
    infn = ninflexion(yv);

    for(int i = 0; i < LIMIT; i++)
    {
        yv[i] = yv[i] - pyinf;
    }

    yv[0] = 0.00001;
    for(int i = 1; i < infn; i++)
    {
        yv[i] = yv[infn] / xv[infn] * xv[i];
    }

    // OUTPUT RESULTS TO FILE
    for(int i = 0; i < LIMIT; i++)
    {
        file_2
        << std::fixed
        << std::setw(12) << std::right << xv[i]
        << std::setw(12) << std::right << yv[i]
        << '\n';
    }

    file_2.close();

    return 0;
}



    0.000000    0.000010
    0.448212    0.000338
    0.896494    0.000677
    1.344915    0.001016

    ...

   19.213341    4.665710
   19.660109    4.730203
   20.115313    4.781073
   20.578724    4.819962
   21.050104    4.848400


@againtry
thank you, my dear friend. the program works well.
I will compare these data with my experimental results and I hope they are the same.
Topic archived. No new replies allowed.