473,467 Members | 1,998 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Function not returning correct values

25 New Member
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 2559
Meetee
931 Recognized Expert Moderator Contributor
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 New Member
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 Recognized Expert Moderator Contributor
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 New Member
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 Recognized Expert Moderator Expert
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

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

Similar topics

2
by: lawrence | last post by:
Here I have two functions, the first returning a value to the second. When I put print_r() on the last line of the first function, I can see that the correct values are in the array as expected....
35
by: wired | last post by:
Hi, I've just taught myself C++, so I haven't learnt much about style or the like from any single source, and I'm quite styleless as a result. But at the same time, I really want nice code and I...
41
by: Materialised | last post by:
I am writing a simple function to initialise 3 variables to pesudo random numbers. I have a function which is as follows int randomise( int x, int y, intz) { srand((unsigned)time(NULL)); x...
19
by: anguo | last post by:
i find in many hash function use 5381,for exampla: static constmap_hash hash(char *pchData, int iLen) { unsigned char cBuf; constmap_hash ulHashId; ulHashId = 5381; while (iLen > 0) { cBuf =...
16
by: Nikolay Petrov | last post by:
How can I return multiple values from a custom function? TIA
6
by: karthi | last post by:
hi, I need user defined function that converts string to float in c. since the library function atof and strtod occupies large space in my processor memory I can't use it in my code. regards,...
11
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
4
by: barcaroller | last post by:
I am trying to adopt a model for calling functions and checking their return values. I'm following Scott Meyer's recommendation of not over-using exceptions because of their potential overhead. ...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.