//Para imprimir el resultado al lugar decimal deseado por el usuario.
cout << fixed << setprecision(num3);
//Devuelve el valor ramdom de numeros reales.
cout << random(float val1, float val2, int val3);
Definition
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
//Para calcular numeros aleatoreos para numeros reales.
double random(float num1, float num2, int num3)
{
//Para darle valor a result.
double result;
//Para darle valor entero a num1 y num2.
int
num1 = num1 * 100,
num2 = num2 * 100;
result = num1 + rand() % (num2 - num1 +1)
static_cast <float> (result);
return result;
}
Another thing, Do I must define the (int num3) listed on the Parameter list in the function of definition?, so it'll be useful in the invocation...
//Devuelve el valor ramdom de numeros enteros.
cout << random( val1, val2);
//Para imprimir el resultado al lugar decimal deseado por el usuario.
cout << fixed << setprecision(num3);
//Devuelve el valor ramdom de numeros reales.
cout << random( val1, val2, val3);
num 3 its the decimal place chosen by the user.
I used cout << fixed << setprecision (num3); on call
Its that ok? to put it on call and use a variable for the value of setprecision?
Yeah I know, because the function its double and result is double WHy am I casting it to float?!
Ok, so If I have it as part of the arguments listed in the Function I should define it as part of the function, but How I do that? If I won't need to use main here, I'm just need to show this 3 phases and its functions.
num3 its the value the user inputs as decimal place and it will show in call as cout << fixed << setprecision(num3);
DO I need to declare result and num1 and num2 with those data types to use it inside the function? I mean num1 and num2 even if I declare them int inside the function still says its double...
I Changed the name of the variable inside the function for the data type to be used (int) and promotion its not used by the system.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
//Para calcular numeros aleatoreos para numeros reales.
double random(float num1, float num2, int num3)
{
//Para darle valor a result.
double result;
//Para darle valor entero a num1 y num2.
int
numA = num1 * 100,
numB = num2 * 100;
result = (numA + rand() % (numB - numA +1))/100;
return result;
}
Also the result variable should be double or it can be float too? Which one its more convenient?
Sorry for the load of Questions I just want to get all this clear.
Ok, so If I have it as part of the arguments listed in the Function I should define it as part of the function, but How I do that?
num3 its the value the user inputs as decimal place and it will show in call as
cout << fixed << setprecision(num3);
You don't seem to be getting it.
You know what num3 does: it's passed to setprecision() from some function (I'm assuming main()). But, why would random() be interested in what the value of num3 is? It's obviously not using it for any formula, so why are you passing it?
DO I need to declare result and num1 and num2 with those data types to use it inside the function?
//Para calcular numeros aleatoreos para numeros reales.
double random(float num1, float num2,)
{
//Para darle valor a result.
double result;
//Para darle valor entero a num1 y num2.
int
numA = num1 * 100,
numB = num2 * 100;
result = (numA + rand() % (numB - numA +1))/100;
return result;
}
another point of view
--------------------------------------------------------------------------------------------------
Hey what about if I had to use the num3 inside that function, how would I use it regarding to set precision in the call? How would I define it to use it just for setprecision in call? It that would be possible and relevant or impossible and irrelevant?
Thanks man, I just want to figured this all and that's it, then I'll can sleep in calm
There's an extra comma in the signature.
result should be divided by 100. (Just do return result/100;)
Hey what about if I had to use the num3 inside that function, how would I use it regarding to set precision in the call? How would I define it to use it just for setprecision in call? It that would be possible and relevant or impossible and irrelevant?
I'm not sure I'm getting what you're asking. Are you asking whether you shouyld pass num3 to random() and let it call setprecision()? If that's the case, the answer is no. The function is called random(). All it should do is return a random number inside a range. It shouldn't concern itself with IO.