Please describe and discuss the following 1. What is the function pow? Where is it defined? Give an example using the function pow. 2. What is type casting? Give an example. 3. What would the following statement in C++ do? result *= a + 5; 4. Write a C++ statement that generates and prints a random number. Why are random number generators needed? 5. What are setw and setprecision functions? Give example statements.
baseexponent
This is the power function, which raise the base by the power of exponent value.
Example: 7 ^ 3 means, = 343.
And 10.53 ^ 3 = 1167.575877
Code:
#include <stdio.h> /* prinf * /
#include <math.h> /* math libaray to get all math formulas */
int main ()
{
printf ("7 ^ 3 = %f\n", pow (7.0, 3.0) );
printf ("10.53 ^ 12 = %f\n", pow (10.53, 3.0) );
return 0;
}
Simply it means converting the variable from current type to another type. There is different method to convert implicit and explicit. C++ is a strong typed language, many conversation may need the explicit conversions which is known as C++ as type casting.
Different syntaxes for generic type casting: functional and c-like
double x = 10.3
Int y
y = int(x); //functional notation
y = (int) x ; // c-like cast notation
result *= a + 5;
After adding the variable a and 5 it again multiples with variable values of result. Means for example
It can also be written as
result = result * (a+5);
yields same answer. I hope you got the sense.
Random number:
This number is generated by an algorithm which results a sequence of apparently non-related numbers each time it is called.
Syntax: int rand(void)
int main()
{
int a = 0;
while(a++ < 10)
{
int r = (rand() % 100) + 1;
count << r << " ";
}
return 0;
}
Sample output :
42 35 1 70 25 74 59 63 65 68
Setw:
setw is to set the field widthto be used to print a nice formatted outputs. It takes numeric parameters.
Int main()
{
std::cout << std :: setw(10);
std::cout << 29 << std :: endl;
return 0;
}
Set precision: this function enable us to format the floating point values on output operations. Parameters will be a numerics.
It will take the parameters and prints the float output after that many number of point it will place a decimal.
int main ()
{
double f =4.514159;
std::cout << std::setprecision(5) << f << '\n';
std::cout << std::setprecision(9) << f << '\n';
return 0;
}
Get Answers For Free
Most questions answered within 1 hours.