functions stops loop from running

This is a discussion on functions stops loop from running within the C++ Programming forums, part of the General Programming Boards category; hello i've got an odd problem with the code like it is the program stops after it runs the function ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    16

    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;   
        }
    Last edited by a1pro; 04-22-2005 at 09:18 AM.

  2. #2
    Senior Member joshdick's Avatar
    Join Date
    Nov 2002
    Location
    Phildelphia, PA
    Posts
    1,146
    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?
    FAQ

    "The computer programmer is a creator of universes for which he alone is responsible. Universes of virtually unlimited complexity can be created in the form of computer programs." -- Joseph Weizenbaum.

    "If you cannot grok the overall structure of a program while taking a shower, you are not ready to code it." -- Richard Pattis.

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    16
    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)
    Last edited by a1pro; 04-22-2005 at 09:24 AM.

  4. #4
    Registered User
    Join Date
    Aug 2004
    Location
    San Diego, CA
    Posts
    313
    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?

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    >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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An array of macro functions?
    By someprogr in forum C Programming
    Replies: 6
    Last Post: 01-28-2009, 06:05 PM
  2. Function (modules) and loop help.
    By GCNDoug in forum C Programming
    Replies: 5
    Last Post: 04-07-2007, 12:43 AM
  3. program terminates when loop stops
    By Jherry in forum C++ Programming
    Replies: 3
    Last Post: 07-29-2005, 08:23 PM
  4. Running functions simultaneously
    By CDIrk in forum C Programming
    Replies: 3
    Last Post: 11-29-2003, 12:25 PM
  5. Running two functions at once
    By SMB3Master in forum C Programming
    Replies: 1
    Last Post: 07-18-2003, 07:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21