469,588 Members | 2,688 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,588 developers. It's quick & easy.

Function not returning correct values

25
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <iomanip> 
  6. using namespace std;
  7.  
  8.      float *calcloopcoef(float lbw,float zeta,float k);
  9.       int main()
  10.       {
  11.           float *arrt;
  12.           arrt=calcloopcoef(25,0.7,.25);
  13.           cout<<"The returned values are"<<endl<<arrt[0]<<endl<<arrt[1]<<endl; 
  14.  
  15.                  getchar();
  16.           return 0;
  17.           }
  18.  
  19.           float *calcloopcoef(float lbw,float zeta,float k)
  20.           {
  21.                 float arrtau[2],wn;
  22.                 wn=lbw*8*zeta/(4*pow(zeta,2)+1);
  23.       arrtau[0]=k/pow(wn,2);
  24.       arrtau[1]=(2*zeta)/wn;
  25.       cout<<"values within fn"<<endl<<arrtau[0]<<endl<<arrtau[1]<<endl;
  26.       return arrtau;
  27.       }
Nov 1 '07 #1
5 2193
Meetee
931 Expert Mod 512MB
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

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <cmath>
  5. #include <iomanip> 
  6. using namespace std;
  7.  
  8.      float *calcloopcoef(float lbw,float zeta,float k);
  9.       int main()
  10.       {
  11.           float *arrt;
  12.           arrt=calcloopcoef(25,0.7,.25);
  13.           cout<<"The returned values are"<<endl<<arrt[0]<<endl<<arrt[1]<<endl; 
  14.  
  15.                  getchar();
  16.           return 0;
  17.           }
  18.  
  19.           float *calcloopcoef(float lbw,float zeta,float k)
  20.           {
  21.                 float arrtau[2],wn;
  22.                 wn=lbw*8*zeta/(4*pow(zeta,2)+1);
  23.       arrtau[0]=k/pow(wn,2);
  24.       arrtau[1]=(2*zeta)/wn;
  25.       cout<<"values within fn"<<endl<<arrtau[0]<<endl<<arrtau[1]<<endl;
  26.       return arrtau;
  27.       }
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
Nov 1 '07 #2
prads
25
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
Nov 1 '07 #3
Meetee
931 Expert Mod 512MB
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
Expand|Select|Wrap|Line Numbers
  1. float *calcloopcoef(float lbw,float zeta,float k)
  2. {
  3.        float wn;
  4.        float * arrtau = new float[2]; // change here
  5.        wn=lbw*8*zeta/(4*pow(zeta,2)+1);
  6.        arrtau[0]=k/pow(wn,2);
  7.        arrtau[1]=(2*zeta)/wn;
  8.        cout<<"values within fn"<<endl<<arrtau[0]<<endl<<arrtau[1]<<endl;
  9.        return arrtau;
  10.        delete [] arrtau;
  11. }
  12.  
Change your code like this. It may helps.

Regards
Nov 1 '07 #4
prads
25
Expand|Select|Wrap|Line Numbers
  1. float *calcloopcoef(float lbw,float zeta,float k)
  2. {
  3.        float wn;
  4.        float * arrtau = new float[2]; // change here
  5.        wn=lbw*8*zeta/(4*pow(zeta,2)+1);
  6.        arrtau[0]=k/pow(wn,2);
  7.        arrtau[1]=(2*zeta)/wn;
  8.        cout<<"values within fn"<<endl<<arrtau[0]<<endl<<arrtau[1]<<endl;
  9.        return arrtau;
  10.        delete [] arrtau;
  11. }
  12.  
Change your code like this. It may helps.

Regards
hey great!....thanks that works......
Nov 1 '07 #5
weaknessforcats
9,208 Expert Mod 8TB
hey great!....thanks 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.
Nov 1 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

41 posts views Thread by Materialised | last post: by
19 posts views Thread by anguo | last post: by
16 posts views Thread by Nikolay Petrov | last post: by
11 posts views Thread by aarklon | last post: by
4 posts views Thread by barcaroller | last post: by
reply views Thread by suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.