a) Write a function that will return a random double number. The function takes two parameters (both integers). The first parameter is the maximum whole-number value that the number can be (the minimum is 0.) The second parameter is how many places of random precision the number must have.
double randDub( intwhole, int precision)
{
double val;
int decOutcomes = pow(10, precision);
int totalOutcomes = whole*decOutcomes;
val= rand() % total outcomes;
return val/decOutcomes;
}
b) Write a line of code that will call the function you just wrote above and ask it to generate a random number from 0-100 with two places of precision, saving the return value in a variable (declare it) named ran.
ran = randDub(100,2);
c) Print the value of ran to two places.
printf(
Help needed writing this C language, i need one line of code in C language fole pat c please help.?
This is the line you want:
printf("%.2f", ran);
add a \n after the f if you want a carriage return.
The ".2" portion tells printf that you want precision of two and the "f" portion says the variable that you want to print is either a float or a double.
I hope this answers your question.
Reply:That's pretty simple. You should be able to work that out yourself, just look up printf in help.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment