|
Immunity18 wrote: printf("%lf",double) ? i do this and it print me 0.00000 :/
What should it print? What is the value of double ?
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
a fault in my code :P now it prints the right values !
thank for the intresting ![Big Grin | :-D](https://www.codeproject.com/script/Forums/Images/smiley_biggrin.gif)
|
|
|
|
|
I dont know how to create a fuction that return 2 values and how i give these values in 2 varablies in (int main() )
i found on google something but i think its for C#
I am working on C
#include <stdio.h>
#define N 10
#include <stdlib.h>
#include <math.h>
void insert_array ( int p[] , int nCount , int a , int b);
(double , double) find_values( int p[] ,int nCount );
void print_array1( int p[] , int nCount);
void print_array2( long p[][2] , int fCount);
int main()
{
int pinakas[N];
double mid , s ;
(mid , s) = find_values(pinakas , N);
printf ("%lf %lf",s ,mid);
return 0;
}
(double ,double) find_values(int p[] ,int nCount )
{
double mid ;
double s ;
for ( int i =0 ; i < nCount ; i++ )
{
mid = mid + p[i];
}
mid = mid/nCount;
for (int z = 0 ; z < nCount ; z++)
{
s = s + (p[z] - mid)*(p[z] -mid);
}
s = pow((s/(nCount -1)) , (1/2));
return (mid , s);
}
|
|
|
|
|
I'm no C++ expert, but won't it be easier to send references of the variables?
void find_values(int p[] ,int nCount, double *mid, double*s )
--Star
|
|
|
|
|
well not because you dont know mid and s .
fuction find_values will find the mid and s (and return them)
but i dont know what reference is about !![Big Grin | :-D](https://www.codeproject.com/script/Forums/Images/smiley_biggrin.gif)
|
|
|
|
|
When we reference a variable we send its address(i.e. original variable not a copy), while in usual function calling, you send a copy of the variable.
In other words, any changes made to a variable, will remain in the function. But if you sent a reference, any changes to that reference of the variable will return to main.
Hope that explains it a little.
Well there is another way, you can create a global array of type double that holds two elements, knowing that you will assign mid and s as the first and second elements respectively. When you call the function it will access the array, and you can add the values of mid and s. Later you can access them in main.
Or if you know about referencing a little, you can create an array inside main, and send its reference -the address- and you can assign its values inside the function. The function will return void, but because you sent the array's reference (not a copy) you will assign the values of mid and s.
Hope it's clear.
--Star
|
|
|
|
|
Well, from the logical (i.e. language independent) standpoint, functions can get three categories of arguments:
(1) The IN arguments, i.e. values set by the caller and used inside the function. This category is the most used one.
(2) The OUT arguments, i.e. values set by the function and returned to the caller.
(3) The INOUT arguments, i.e. values set by the caller and returned (usually modified) by the function.
Of course, you need two arguments of the second category (OUT ).
In C language every argument is passed by value, hence you can implement OUT arguments only by means of pointers (as already suggested by Star08):
void find_values(int p[] ,int nCount, double * pmid, double *ps);
int main()
{
int pinakas[N];
double mid , s ;
find_values(pinakas , N, &mid, &s);
printf ("%lf %lf",s ,mid);
return 0;
}
Of course you need to know a bit of pointers usage.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
hehe i see ! thx for help !
|
|
|
|
|
my problem is that exercise says : create a fuction find_values (bla bla bla) that takes as input (1) Array's name (2) Array's Size and as OUTput (1) mid (2) s
mid = x1+x2+x3...+xn /n
s2 =... :P difficult to right it in text
is there a way to not use pointers? or if its nessesary can u tell me how i have to write the void find_array { } fuction ?
|
|
|
|
|
Immunity18 wrote: is there a way to not use pointers?
Oh yes, and it simple: write two functions!
Immunity18 wrote: or if its nessesary can u tell me how i have to write the void find_array { } fuction ?
I will show how to use a pointer as out parameter (I don't want to build the whole exercise for you... ).
In the following code snippet, the mySqr function accepts an int as [IN] argument and returns, via a pointer, the square of that value:
void mySqr(int x, int *px2)
{
*px2 = x * x;
return;
}
int main()
{
int i;
int iSquared;
i=5;
mySqr(i, &iSquared);
printf("value=%d, squared value=%d\n", i, iSquared);
}
Hope that helps
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
yes thank you very much !
Actually its not my exercise but a friend's :P
I have other 3 project in C++ (one is a program that you give a query with words + a folder of txt files and it return 10 files that might be what you are looking for , something like google searching machine ) (DONE) \o/
I am scrap with pointers :P and anyway scrap in programming craft programs with fuse of C and C++ :P
-- modified at 17:15 Saturday 19th May, 2007
|
|
|
|
|
Star08 wrote: ...won't it be easier to send references of the variables?
References would be denoted:
void find_values( int p[] ,int nCount, double &mid, double &s );
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
|
|
|
|
|
You can't do that in C or C++. In C++ you could return a std::pair<double,double> but in C you have to return the double s via output parameters.
|
|
|
|
|
thank mike pallini and star08 already suggest me pointers for C so i used them ![Big Grin | :-D](https://codeproject.freetls.fastly.net/script/Forums/Images/smiley_biggrin.gif)
|
|
|
|
|
You can't return a struct in C ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Actually, quite simple. Declare a Structure:
<br />
struct RESULT {double Res1,Res2};<br />
and let your function return this structure
LateNightsInNewry
|
|
|
|
|
|
Hey guys, my first post here =)
Anyways, I'm trying to use EndTask to close a Window, but unfortunately it doesn't work. I get an error 'EndTask' : undeclared identifier. I looked on MSDN http://msdn2.microsoft.com/en-us/library/ms633492.aspx and every thing appeared good, this is the code I'm using: HWND hWin;
hWin = FindWindow(NULL,"Application");
EndTask(
hWin,
FALSE,
TRUE
);
Hope you guys can help me out, thanks
|
|
|
|
|
I suggest to you to pay attention to the Remarks section of the page you referenced and hence to use other functions, see here [^].
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Thanks or the reply, I really want to use the EndTask function, the only one that will work in my case, and I looked in the remark section, and I think this is what you were talking about "accessible using the appropriate Include file and library linkage" well I included windows.h and imported the User32.lib. I'm really lost right now. But thanks for the reply and if you could help again it would be great!
|
|
|
|
|
Have you made an attempt to link the DLL explicitely, i.e. via LoadLibrary and GetProcAddress ?
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Hello,
Through the program I want to access different tree items, what is the pointer or how can I declare a pointer to access these items in the tree?
Thank you.
--Star
|
|
|
|
|
Star08 wrote: what is the pointer or how can I declare a pointer to access these items in the tree?
How did you create the tree?
led mike
|
|
|
|
|
linklist, but I'm trying to display it using Tree Control.
So far, I can display the root and its children. But when I try to post the grandchildren, they are posted as the root's children.
--Star
-- modified at 16:27 Saturday 19th May, 2007
|
|
|
|
|
Items in a treeview control are identified by a handle (HTREEITEM type).
You can navigate the tree (item handle) heirarchy with TVM_GETNEXTITEM. You can get an items
information with TVM_GETITEM.
Instead of a pointer to an item, you'll probably want to work with a HTREEITEM handle.
Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder
|
|
|
|