Function not returning correct values

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • prads
    New Member
    • Oct 2007
    • 25

    #1

    Function not returning correct values

    Hello,
    the following code doesnot return the corect values to the main function. However it prints the correct values within the function. Also the same when written in C works absolutely fine. Pls tell me where im going wrong and correct me. U can run it and check!
    Thanks
    Prads

    [code=cpp]
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <iomanip>
    using namespace std;

    float *calcloopcoef(f loat lbw,float zeta,float k);
    int main()
    {
    float *arrt;
    arrt=calcloopco ef(25,0.7,.25);
    cout<<"The returned values are"<<endl<<arr t[0]<<endl<<arrt[1]<<endl;

    getchar();
    return 0;
    }

    float *calcloopcoef(f loat lbw,float zeta,float k)
    {
    float arrtau[2],wn;
    wn=lbw*8*zeta/(4*pow(zeta,2)+ 1);
    arrtau[0]=k/pow(wn,2);
    arrtau[1]=(2*zeta)/wn;
    cout<<"values within fn"<<endl<<arrt au[0]<<endl<<arrta u[1]<<endl;
    return arrtau;
    }[/code]
    Last edited by sicarie; Nov 1 '07, 04:23 AM. Reason: Code tags
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by prads
    Hello,
    the following code doesnot return the corect values to the main function. However it prints the correct values within the function. Also the same when written in C works absolutely fine. Pls tell me where im going wrong and correct me. U can run it and check!
    Thanks
    Prads

    [code=cpp]
    #include <iostream>
    #include <fstream>
    #include <cstring>
    #include <cmath>
    #include <iomanip>
    using namespace std;

    float *calcloopcoef(f loat lbw,float zeta,float k);
    int main()
    {
    float *arrt;
    arrt=calcloopco ef(25,0.7,.25);
    cout<<"The returned values are"<<endl<<arr t[0]<<endl<<arrt[1]<<endl;

    getchar();
    return 0;
    }

    float *calcloopcoef(f loat lbw,float zeta,float k)
    {
    float arrtau[2],wn;
    wn=lbw*8*zeta/(4*pow(zeta,2)+ 1);
    arrtau[0]=k/pow(wn,2);
    arrtau[1]=(2*zeta)/wn;
    cout<<"values within fn"<<endl<<arrt au[0]<<endl<<arrta u[1]<<endl;
    return arrtau;
    }[/code]
    Your function should retrurn pointer of float type. So you will have to take a float pointer array dynamically and then return that pointer.

    Regards

    Comment

    • prads
      New Member
      • Oct 2007
      • 25

      #3
      Originally posted by zodilla58
      Your function should retrurn pointer of float type. So you will have to take a float pointer array dynamically and then return that pointer.

      Regards
      I am sorry but I couldnot understand much from that and with whatever I interpreted, I couldnot get the output. So can u pls make the required changes to my program and post it..
      thanks,
      prads

      Comment

      • Meetee
        Recognized Expert Contributor
        • Dec 2006
        • 928

        #4
        Originally posted by prads
        I am sorry but I couldnot understand much from that and with whatever I interpreted, I couldnot get the output. So can u pls make the required changes to my program and post it..
        thanks,
        prads
        [CODE=CPP]
        float *calcloopcoef(f loat lbw,float zeta,float k)
        {
        float wn;
        float * arrtau = new float[2]; // change here
        wn=lbw*8*zeta/(4*pow(zeta,2)+ 1);
        arrtau[0]=k/pow(wn,2);
        arrtau[1]=(2*zeta)/wn;
        cout<<"values within fn"<<endl<<arrt au[0]<<endl<<arrta u[1]<<endl;
        return arrtau;
        delete [] arrtau;
        }
        [/CODE]

        Change your code like this. It may helps.

        Regards

        Comment

        • prads
          New Member
          • Oct 2007
          • 25

          #5
          Originally posted by zodilla58
          [CODE=CPP]
          float *calcloopcoef(f loat lbw,float zeta,float k)
          {
          float wn;
          float * arrtau = new float[2]; // change here
          wn=lbw*8*zeta/(4*pow(zeta,2)+ 1);
          arrtau[0]=k/pow(wn,2);
          arrtau[1]=(2*zeta)/wn;
          cout<<"values within fn"<<endl<<arrt au[0]<<endl<<arrta u[1]<<endl;
          return arrtau;
          delete [] arrtau;
          }
          [/CODE]

          Change your code like this. It may helps.

          Regards
          hey great!....thank s that works......

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Originally posted by prads
            hey great!....thank s that works......
            You were given a spoonfed solution. Do you understand what you were given?

            The danger in spoonfeeding is that you never figure this stuff out on your own and until you do that, you haven't learned it.

            Comment

            Working...