The C++ language includes functions organized into a series of libraries. Your t
ID: 3679197 • Letter: T
Question
The C++ language includes functions organized into a series of libraries. Your text documents some of these libraries in Appendix F.
Part A Refer to the functions contained in the cmath (math.h) library. Identify a function that will find the absolute value of a number. Indicate how many pieces of data must be passed to the function. Indicate, what (if anything) will be returned by the function. Demonstrate the appropriate use of the function in a short code fragment.
Part B Refer to the functions contained in the cmath (math.h) library. Identify a function that will determine the value of x raised to the y power. Indicate how many pieces of data must be passed to the function. Indicate, what (if anything) will be returned by the function. Demonstrate the appropriate use of the function in a short code fragment.
Explanation / Answer
PART A:
------
abs (cstdlib) : Absolute value (function )
// cmath's abs example
#include <iostream> // std::cout
#include <cmath> // std::abs
int main ()
{
std::cout << "abs (3.1416) = " << std::abs (3.1416) << ' ';
std::cout << "abs (-10.6) = " << std::abs (-10.6) << ' ';
return 0;
}
Output :-
abs (3.1416) = 3.1416
abs (-10.6) = 10.6
. Function abs can be used to calculate the absolute value.
. Two values (3.1416 and -10.6) are passed to the function abs
. The function abs returns the absolute values, which are 3.1416 and 10.6
. The above code demonstrates the appropriate use of this function.
Another example can be shown using the fabs function, which can also be used to return the absolute value:
/* fabs example */
#include <stdio.h> /* printf */
#include <math.h> /* fabs */
int main ()
{
printf ("The absolute value of 3.1416 is %f ", fabs (3.1416) );
printf ("The absolute value of -10.6 is %f ", fabs (-10.6) );
return 0;
}
Output :-
The absolute value of 3.1416 is 3.141600
The absolute value of -10.6 is 10.600000
PART B:
----------
The below program calculates the value of x raised to the power y using a function called pow(number,power):
#include<math.h>
#include<conio.h>
#include<iostream.h>
void main()
{clrscr();
long int x,y;
long int r;
cout<<"value of x : ";
cin>>x;
cout<<"value of y : ";
cin>>n;
r=pow(x,y);
cout<<"answer is : "<<r;
getch();
}
. Function pow is used to calculate the power y. We must pass the date for x and y in order to get the result, r .
. The value of r will be returned as a result.
. The above code demonstrates the appropriate use of this function
If you still have any doubtconcern. Please feel free to comment. Happy to Help :)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.