# functions stops loop from running

• 04-22-2005
a1pro
functions stops loop from running
hello

i've got an odd problem with the code like it is the program stops after it runs the function hole for the first time.

edit:

i've just tried in linux, and i get a segmentation fault, regardless of the indeces just after the function ends
And i'm using gnu compilers on both platforms.

edit II

solved the segmentation fault problem still getting rubbish data, set counter in the function hole to 0, and increased array size to 73

If i change the column index from 1 to 0 and 2 to 1 in array circle in the function hole and in the rest of the program to refer to the proper array position, the output from the function to screen is fine but it writes complete rubbish to the file.

The rest of the program writes the appropiate data

any ideas?

TIA

Code:

```#include <fstream> #include <iostream> #include <cmath> using namespace std; float hole(float radius,float pos_x,float pos_y) // This functions plots a circle around each lattice point {       int theta,counter;       float radian,circle[72][3];                for(theta=0,counter=1;theta <=360;theta = theta + 5,counter++)  // rotating the angle from 0 to 360     {     radian = M_PI*theta/180.0;/*convert the angle from degrees to radians*/         circle[counter][1]=radius*cos(radian)+pos_x;// parametric equation of circle     circle[counter][2]=radius*sin(radian)+pos_y;         cout<<circle[counter][1]<<" "<<circle[counter][2]<<" "<<counter<<endl;     }     return circle[72][3]; }          int main()     {         char str[2000],name[60];         float circle_array[72][3];                              float origin_x,origin_y,pos_x,pos_y;          int i,j,n;                 float a = 550.0; // photonic crystal parameters period and radius         float radius=110.0;                                                               origin_x=0.0;         origin_y=0.0;                                                    cout << "input file name" << endl;                 cin>>name;                  fstream data(name,ios::out);                 for(j=0;j<=5;j++) // calculate lattice points stemmed from the origin           {         for(n=0;n<=10;n++)         {           pos_x = origin_x+a*n;           pos_y = 0.0+j*sqrt(3);               data<<pos_x<<" "<< pos_y<<endl;                 circle_array[72][3]=hole(radius,pos_x,pos_y);// get hole data                 cout<<"hello"<<endl;                 for(i=0;i<=72;i++)         {         data<<circle_array[i][1]<<" "<<circle_array[i][2]<<endl;//<<"Main"<<endl;  //hole data to file                      }                 }                        }         origin_x=  a*0.5;              origin_y= a*0.5*sqrt(3);                 for(j=0;j<=5;j++) // lattice points stemmed from (1/2,sqrt(3)/2)           {           for (n=0;n<=10;n++)           {           pos_x = origin_x+a*n;           pos_y = origin_y+j*sqrt(3);                 data<<pos_x<<" "<< pos_y<<endl;           }                        }                                       data.close();           return 0;      }```
• 04-22-2005
joshdick
Quote:

Originally Posted by a1pro
Code:

```#include <fstream> #include <iostream> #include <cmath> using namespace std; float hole(float radius,float pos_x,float pos_y) // This functions plots a circle around each lattice point {       float radian,circle[72][3];  . . .     return circle[72][3]; }```

Arrays don't quite work like that, mang. What is it you're really trying to return? Why is circle a 2-d array?
• 04-22-2005
a1pro
it's just a way to store the positions of the circumference, i.e.each individual x,y value, so what i'm trying to return is a set of coordinates for each circunference around certain positions.
( these positions are lattice points on a 2d lattice)
• 04-22-2005
Lithorien
Quote:

Originally Posted by a1pro
it's just a way to store the positions of the circumference, i.e.each individual x,y value, so what i'm trying to return is a set of coordinates for each circunference around certain positions.
( these positions are lattice points on a 2d lattice)

If you want to return a set of coordinates, why not use a struct or a class, and modify that?
• 04-22-2005
swoopy
>so what i'm trying to return is a set of coordinates for each circunference around certain positions.
As everyone has said, your hole() function returns is a single float, not an array.