Hi Every One,
I am working on an assignment that needs to be submitted on 08-08-2008. The IDE in use is Dev-C++ 4.9.9.2.
Basically, everything works well untill the program calls for values which are not avalable for a given energy 'energy[0]'. It will run but at these points it spits out garbage for the needed values 'parameters'.
Discription of function:
1. The function must return a pointer to an arrary of six elements of type double
2. Accept two arrarys as parameters
a) The first arrary is of type double and takes two elements. The second element is redundant. ele[0] gives the point for which the parameters are to be computed.
b) The second arrary is of type char and gives the name of the data file from which the interpolation is to be done.
3. The preferred interpolation method is the akima spline (can be found in the GSL).

Below is the code showing were I have reached in the assignment.
Thanks for Your interest.

/*
add interpolation to take care of Zs with no parameter files
*/
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>
#include<math.h>
#include<process.h>

using namespace std;


double *geoParaFunc(const int,double[]);  // function declaration

int main()
{
    string line1,line2,line3;
    double Zeq[2];
    
    /* sets the atomic numbers for which GP parameters are available */
    int atNumb[23]={4,5,6,7,8,11,12,13,14,15,16,18,19,20,26,29,42,50,57,64,74,
                    82,92}; 
      
    ifstream fin("Zeq.txt");
    ofstream fout("Parameters.txt");
    
    if (fin.is_open()&& fout.is_open())
    {
       getline(fin,line1);  // skips three lines to get to data
       getline(fin,line2);
       getline(fin,line3);
       
       /* caption for the output file */
       fout <<"G-P Energy Absorption Buildup Factor Coefficients\n "  
            <<"\t\tSerpentine\n"<<"_______________________________________"
            <<"__________________\n"
            <<"MeV\tb\tc\ta\t\tX\td\n"<<"______________________________"
            <<"___________________________\n";

       while (!fin.eof())
        {
             double *Para=new double[6];
             double *Para1=new double[6];
             double *Para2=new double[6];
             double temp;
             int Z1=0,Z2=0;
                    
             for(int i=0;i<2;i++) //read in data from Zeq.txt one line
                                             // at a time
             {
                 fin >> Zeq[i];  
             }
             temp=Zeq[0]/1000.0;
             Zeq[0]=temp;
             
             int i=0;
             while (i<23)
             {
                  /* generation of parameter file starts here */
                  
                 /* one */ 
                 if (atNumb[i]==Zeq[1]) 
                 { 
                    Para=geoParaFunc(atNumb[i],Zeq); // function call
                    
                    fout<<Para[0]<<"\t"<<Para[1]<<"\t"<<Para[2]
                        <<"\t"<<Para[3]<<"\t        "<<Para[4]<<"\t"
                        <<Para[5]<<endl;
                    i=22;   // terminates loop
                 }
                 
                 /* two */
                 else if(atNumb[i]<Zeq[1] && atNumb[i+1]>Zeq[1])
                 { 
                    Z1=atNumb[i];
                    Z2=atNumb[i+1];
                    Para1=geoParaFunc(Z1,Zeq);  // function calls
                    Para2=geoParaFunc(Z2,Zeq);
                    double A,B,C,D,E,F;    // variables created to
                                          // make formula readable
                    for(int k=1;k<6;k++)
                    {
                        A=Para1[k];
                        B=log10(Z2);
                        C=log10(Zeq[1]);
                        D=Para2[k];
                        E=log10 (Z1);
                        Para[k]=(A*(B-C)+(D*(C-E)))/(B-E);
                        i=22;    // terminates loop
                    }
                    fout<<Para1[0]<<"\t"<<Para[1]<<"\t"<<Para[2]
                        <<"\t"<<Para[3]<<"\t"<<Para[4]<<"\t"
                        <<Para[5]<<endl;
                 }
                 i++;
             }
       }  
    }
    system("pause");
    return 0;
}



/*
  Name: GeoParaFunc
  Description: Opens the GPEABFC.XX file and returns 
  the parameters corresponding to a given energy passed to it.
*/

double *geoParaFunc(const int i,double energy[])
{
    string line1,line2,line3,line4,line5;  
    char filename[20];
    double *Para=new double[6];
    
    /* reads from the corresponding files */
    switch(i){
              case 4:
                   strcpy(filename,"GPEABFC.04.txt");
                   break;
              case 5:
                   strcpy(filename,"GPEABFC.05.txt");
                   break;
              case 6:
                   strcpy(filename,"GPEABFC.06.txt");
                   break;
              case 7:
                   strcpy(filename,"GPEABFC.07.txt");
                   break;
              case 8:
                   strcpy(filename,"GPEABFC.08.txt");
                   break;
              case 11:
                   strcpy(filename,"GPEABFC.11.txt");
                   break;
              case 12:
                   strcpy(filename,"GPEABFC.12.txt");
                   break;
              case 13:
                   strcpy(filename,"GPEABFC.13.txt");
                   break;
              case 14:
                   strcpy(filename,"GPEABFC.14.txt");
                   break;
              case 15:
                   strcpy(filename,"GPEABFC.15.txt");
                   break;
              case 16:
                   strcpy(filename,"GPEABFC.16.txt");
                   break;
              case 18:
                   strcpy(filename,"GPEABFC.18.txt");
                   break;
              case 19:
                   strcpy(filename,"GPEABFC.19.txt");
                   break;
              case 20:
                   strcpy(filename,"GPEABFC.20.txt");
                   break;
              case 26:
                   strcpy(filename,"GPEABFC.26.txt");
                   break;
              case 29:
                   strcpy(filename,"GPEABFC.29.txt");
                   break;
              case 42:
                   strcpy(filename,"GPEABFC.42.txt");
                   break;
              case 50:
                   strcpy(filename,"GPEABFC.50.txt");
                   break;
              case 57:
                   strcpy(filename,"GPEABFC.57.txt");
                   break;
              case 64:
                   strcpy(filename,"GPEABFC.64.txt");
                   break;
              case 78:
                   strcpy(filename,"GPEABFC.78.txt");
                   break;
              case 82:
                   strcpy(filename,"GPEABFC.82.txt");
                   break;
              case 92:
                   strcpy(filename,"GPEABFC.92.txt");
                   break;
              default:
                 cout<<"The GPEABFC."<<i<<" file could not be found.\n";
                 cout<<"Program termingating \n";
                 exit(0);
             }
    ifstream fop(filename);
     
     if(fop.is_open())
     {
         getline(fop,line1);  // skips 5 lines to get to data
         getline(fop,line2);
         getline(fop,line3);
         getline(fop,line4);
         getline(fop,line5);
         
         while(!fop.eof())
         {
             for(int i=0;i<6;i++)
             {
                 fop >> *(Para+i);    // reads in parameters one 
                                   //line at a time
             }  
          
             if(Para[0]==energy[0])
             {
                fop.close();
                return Para;   
             }  
             
         }
          /*
         THE PROBLEM STARTS FROM HERE:
         */
         else if(Para[0]!=energy[0]){
              
             // call_inpterp_function(double energy,char filename); 
             
              /* energy for unresulved energy[0] and 
               filename for data of parameters to be used
               for interpulation */
               
              fop.close();
              return Para;
         }  
         fop.close(); 
     }
}

1. I don't understand where the problem starts. You have else without previous if.

2. There are some suspicious fragments in your code. For example, in geoParaFunc you allocated three arrays (see Para... pointers) then overwrite these pointers in

...
Para=geoParaFunc(atNumb[i],Zeq); // function call
...

It's classic memory leaks.

3. if(Para[0]==energy[0]) - a very dangerous comparison for doubles (approximations of real numbers). The only safety equality check for doubles in numerical computations is if (x == 0.0) (or !=0.0).

4. Keep it simpler:

const char* filename;
switch(i)
{              
case 4:
    filename = "GPEABFC.04.txt";
    break;
case 5:
    filename = "GPEABFC.05.txt";
    break;
// ...and so on

>1. I don't understand where the problem starts. You have else without previous if.
I noticed the anomaly some time back and revised the code. Here is the correction to the said section:

/*
         THE PROBLEM STARTS FROM HERE:
         */
         
                
         // double *call_interp_function(double energy[],const char *filename); //function decla
                                                                                                                //ration
         // double *call_interp_function(energy,&filename);
         
          /* energy for unresulved energy[0] and 
           filename for data of parameters to be used
           for interpulation */
               
            
         fop.close(); 
     }
}

/*

double *call_interp_function(double energy[],const char *filename){
       double *Para=new double[6];
       string line1,line2,line3,line4,line5;
       getline(fop,line1);  // skips 5 lines to get to data
       getline(fop,line2);
       getline(fop,line3);
       getline(fop,line4);
       getline(fop,line5);
       
      
     //  FUNCTION STATMENTS
      
       
       return Para;
}*/

By the way thanks for the suggestion to make things , I did.

>2. There are some suspicious fragments in your code. For example, in geoParaFunc you allocated three arrays (see Para... pointers) then overwrite these pointers in ...
Would it help if I use the delete function to clear pointers after use?

>3. if(Para[0]==energy[0]) - a very dangerous comparison for doubles (approximations of real numbers). The only safety equality check for doubles in numerical computations is if (x == 0.0) (or !=0.0).

I really can't find any other means for the logical tests. By vertue of the computation they stand to be the only ones.
I am interested in this memory leakage thing but for the moment I really need the subroutine.
Thanks kwesi

This article has been dead for over six months. Start a new discussion instead.