|
I feel really dumb for asking this. For the this equation:
y = x * n
How can I calculate what the smallest integer n that I can multiply the rational number x by so that y also becomes an integer? Can this be done analytically, or will I need an algorithm?
Thanks
Joel Holdsworth
|
|
|
|
|
Could you rephrase the question. Not quite sure what you are trying to do.
|
|
|
|
|
So if x = 12.5 , the smallest n that I can multiply it by is 2, if I want an integer value of y (which in this case is 25). Is guess it might get more tricky if x = 3 1/3 or something.
Joel Holdsworth
|
|
|
|
|
In your origional post you referred to x and y as integers. In C/C++ integers have no decimal points, they are always rounded down to the nearest whole number. Perhaps you meant double or float .
As for the problem, divide 1 by the decimal.
1 / ( 12.5 - 12 ) == 2
|
|
|
|
|
Joel Holdsworth wrote: integer n that I can multiply the rational number x by so that y also becomes an integer
n : integer
x : rational
y : value of x * n which must equal to an integer
for this Joel, you must find what is the equivalent fraction of x ; i mean, 1.33333 is actually 4/3.
only then you have this, you know n because it equals the denominator of that fraction (here, 3 ).
but that's not that simple, because is the rational number equates to a too big fraction, you will find dumb values
|
|
|
|
|
y = x * n
Where:
“y” is an integer.
“x” is a rational number.
“n” is an integer.
Now a rational number is simply a number which can be represented in the form i1/i2 where both “i1” and “i2” are integers. So we have:
y = i1/i2 * n
If we reduce “x” (which equals i1/i2 ) to its simplest form then the number you’re after (“smallest integer n that I can multiply the rational number x by so that y also becomes an integer”) is simply “i2”. To reduce “x” to its simplest form simply divide “i1” and “i2” by the greatest common divisor of “i1” and “i2”.
Here’s a function to find the greatest common divisor of two integers:
int gcd(int a, int b)
{
for ( ;; )
{
if (a==0)
{
return (b<0) ? -b : b;
}
b %= a;
if (b==0)
{
return (a<0) ? -a : a;
}
a %= b;
}
}
With this function you can find the answer you’re after like this:
n = i2/gcd(i1, i2)
This information may be of some help. Still you have to be able to get “i1” and “i2” from “x”.
Steve
|
|
|
|
|
Joel,
I assume that because you are posting the question here you have to solve this using a representation of y as either a float or double, and this makes it tricky. If you have y, a rational and y=p/q, then as long as it is resuced form, your answer is q. If you represent y as a float or a double, then it is represented in base 2, so if q has any factors other than 2, then the ideal representation is infinite, and so what number is in stored for y as a double, is not exactly equal to y.
This is most easily seen in base 10, any fractions with factors other than 2 or 5 on the denominator, have infinite decimal expansions, and any finite expansion is approximate. So if y = 1/3, y approx = 0.3333333 to 7 places, and with this approximate y the answer to your question is 10000000. The same thing will happen with floats and doubles in programming.
Is there a way around this - well if you know the uncdertainty in the approximation, or if the denominator is less than some maximum value, then you can undo the rounding. The fast way to do this is to use a continued fraction expansion (something to read up on if you don't know of them) they are pretty easy. For example, the continued fraction expansion of 0.3333333 gives as approximations (1/3, 3333332/9999997, 3333333/10000000 - the last one is always exact) and it is pretty easy to pick 1/3 as the likely one. This took about 20 operations.
|
|
|
|
|
i have a function
AddItem(unsigned short*,...)
the strings which i have to pass to the AddItem function are obtained at runtiem and even the number of the strings too, with the code similar to the following.
OnButtonSubmit()
{
int count = GetItemCount();
CStringArray strArr;
for(int index=0;index<count;index++)
{
strArr.Add(db.GetColumnValue(index));
}
}
Now what i want is to pass the string which are added to the strArr as arguments to the AddItem function.
How can i do that?
Please help me.
Thank you.
KIRAN PINJARLA
|
|
|
|
|
I've to admit, I don't understand your problem. Strings are passed to functions like everything else ...
string strArr;<br />
<br />
AddItem(unsigned short*,string StrToPass);
If you are passing different strings number you should try to pass array of strings defined dynamically.
AddItem(unsigned short*,string *StrToPass,int Size);
string *strArr;
.<br />
.<br />
.<br />
int Size=30;
.<br />
.<br />
.<br />
strArr = new string [Size];
.<br />
.<br />
.<br />
AddItem(...,strArr,Size);
.<br />
.<br />
.<br />
delete [] strArr;
strArr=NULL;
Using dynamic arrays is similar to static arrays, you can also use
pointer notification instead of f.e. strArr[2] in order to increase program's speed.
Does it solve your problem?
-- modified at 6:24 Saturday 10th March, 2007
|
|
|
|
|
Thank you for your responce Newbie00.
Newbie00 wrote: I've to admit, I don't understand your problem.
Let me explain you.
My function
AddItem(char*,...)
takes n number of arguments.
Suppose at runtime i get my total number of values is 2 and the values are "one" and "two" i need to pass those values to the AddItem as
AddItem("one","two")/pre>
if the number of values are 3 then i need to write as
<pre> AddItem("one","two","three")
and so on...
one solution for this is switch() case statemnt.
switch(noOfVals)
{
case 1: AddItem(strArr.GetAt(0));
case 2: AddItem(strArr.GetAt(0),strArr.GetAt(1));
.... and so on
}
but the number of values can not be guessed. So i need to write a logic which may suit to any number of values.
Do you get my problem now?
KIRAN PINJARLA
|
|
|
|
|
I think you are going about this the wrong way. The common method of dealing with multiple arguments is by using the va_list set of macros.
Another option is to pass the strings to the function one at a time and count them.
|
|
|
|
|
I don't think there is possibility to do something like you want at runtime. Besides what if number of strings you want to pass to the function==10,100 or more?? Do you really want to define function like: AddItem("one","two",.........................."ten"..."hundred)?
I think this is very bad idea. Why don't you choose my solution which I gave you earlier?
If you want to use char instead of string you can do this like this:
char **StrArr;<br />
int Size1=30,Size2=10
StrArr= new char*[30];
<br />
for(int i=0;i<30;i++)<br />
StrArr[i]=new char[Size2];
after this you can write strings into Str[][] array and pass whole array into function giving the Size1 and Size2 as additional parameters near the parameter: char **StrToPass
If you want to use your idea you can do something like this for lets say finite and small set of strings, use "default parameters" (I don't know if I call this properly in English):
void function (char *Str1="",char *Str2="",char *Str3="",char *Str4="") <br />
<br />
function("blablaba");
|
|
|
|
|
Maybe I'm misunderstanding the question, but can't you just pass a reference or pointer to
a CStringArray?
OnButtonSubmit()
{
int count = GetItemCount();
CStringArray strArr;
for(int index=0;index ...
{
strArr.Add(db.GetColumnValue(index));
}
AddItem(strArr);
}
AddItem(CStringArray &strArr)
{
for (int i = 0; i < strArr.GetCount(); ++i)
{
}
}
"Great job, team. Head back to base for debriefing and cocktails."
(Spottswoode "Team America")
|
|
|
|
|
how to make a mfc program autorun
|
|
|
|
|
[Autorun]
open=YourMFCApp.exe
What's wrong with this?, Btw is that you Jeepo?
-- modified at 6:01 Saturday 10th March, 2007
Press: 1500 to 2,200 messages in just 6 days? How's that possible sir?
Dr.Brad :Well,I just replied to everything Graus did and then argued with Negus for a bit.
|
|
|
|
|
this will popup a window and make user to select the exe. but what i need is to execute the exe when i insert the drive no need for the pop up window
|
|
|
|
|
This is not something you can control. Autorun is a feature which users can select to disable or automate from within the OS. This is a security feature for the user.
|
|
|
|
|
ok but i have to do that for my project. can you please help me to achieve that. how can i do this
|
|
|
|
|
I just told you that you can't do it.
|
|
|
|
|
what about shelexec project
|
|
|
|
|
I will say it again, autorun is a feature of the OS. The the user does not want programes to autorun then he/she may disable the feature. The only way you can change this is to edit the registry. Changing this setting without the users permission is bad programing and borderline on being a virus.
|
|
|
|
|
So, you asked it from your other id?
|
|
|
|
|
Hi friends
Is any solution to send email without using smtp server
KK
|
|
|
|
|
Did you see Internet section(Email) on the codeproject?
|
|
|
|
|
I have placed autorun.inf file inside the drive.when i insert the drive the popup window will appear.then double click the icon ,the autorun exe file is executed.
I want to execute file automatically when insert a drive without user interaction.is it possible?
How to do this ?
Any software is needed to install?
|
|
|
|