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 -
#include <iostream>
-
#include <fstream>
-
#include <cstring>
-
#include <cmath>
-
#include <iomanip>
-
using namespace std;
-
-
float *calcloopcoef(float lbw,float zeta,float k);
-
int main()
-
{
-
float *arrt;
-
arrt=calcloopcoef(25,0.7,.25);
-
cout<<"The returned values are"<<endl<<arrt[0]<<endl<<arrt[1]<<endl;
-
-
getchar();
-
return 0;
-
}
-
-
float *calcloopcoef(float 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<<arrtau[0]<<endl<<arrtau[1]<<endl;
-
return arrtau;
-
}
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 -
#include <iostream>
-
#include <fstream>
-
#include <cstring>
-
#include <cmath>
-
#include <iomanip>
-
using namespace std;
-
-
float *calcloopcoef(float lbw,float zeta,float k);
-
int main()
-
{
-
float *arrt;
-
arrt=calcloopcoef(25,0.7,.25);
-
cout<<"The returned values are"<<endl<<arrt[0]<<endl<<arrt[1]<<endl;
-
-
getchar();
-
return 0;
-
}
-
-
float *calcloopcoef(float 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<<arrtau[0]<<endl<<arrtau[1]<<endl;
-
return arrtau;
-
}
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
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
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
-
float *calcloopcoef(float 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<<arrtau[0]<<endl<<arrtau[1]<<endl;
-
return arrtau;
-
delete [] arrtau;
-
}
-
Change your code like this. It may helps.
Regards
-
float *calcloopcoef(float 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<<arrtau[0]<<endl<<arrtau[1]<<endl;
-
return arrtau;
-
delete [] arrtau;
-
}
-
Change your code like this. It may helps.
Regards
hey great!....thanks that works......
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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....
|
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...
|
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...
|
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 =...
|
by: Nikolay Petrov |
last post by:
How can I return multiple values from a custom function?
TIA
|
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,...
|
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...
|
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.
...
|
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...
|
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...
|
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,...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
| |