c++ math function

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

Header file needed when using rand or srand

#include <cstlib>

When this operator is used with string operands it concatenates them, or joins them together.

+

Assume that x is an int variable. What value is assigned to x after the following assignment statement is executed? x = -3 + 4 % 6 / 5;

-3

Write a complete C++ program to compute and output the square root of PI; PI is approximately 3.14159. The const double PI is predefined in cmath. You are encouraged to use this predefined constant.

//Computes the square root of 3.14159 #include <iostream> #include <cmath> //provides sqrt and PI. using namespace std; int main () { cout << "The square root of " >> PI << sqrt(PI) << endl; return 0; }

What must the compiler know about the function before it is called?

1) name 2) return type 3) number of parameters 4) data type of parameter.

1. sqrt(16.0) 2. pow(2, 3) 3. abs(3) 4. fabs(-3.0) 5. ceil(5.1) 6. floor(5.8) 7. 7/abs(-2) 8. sqrt(16) 9. pow(2.0, 3) 10. abs(-3) 11. fabs(-3.5) 12. ceil(5.8) 13. pow(3.0, 2)/2.0 14. (7 + sqrt(4.0))/3.0 15. pow(2.0, 3.0) 16. pow(1.1, 2) 17. abs(0) 18. fabs(3.5) 19. floor(5.1) 20. pow(3.0, 2)/2 21. sqrt(pow(3, 2))

1. 4.0 2. 8.0 3. 3 4. 3.0 5. 6.0 6. 5.0 7. 3 8. 4.0 9. 8.0 10. 3 11. 3.5 12. 6.0 13. 4.5 14. 3.0 15. 8.0 16. 1.21 17. 0 18. 3.5 19. 5.0 20. 4.5 21. 3.0

1. (𝒙 + 𝒚)^(1/2) 2. x^(y + 7) 3. (𝒂𝒓𝒆𝒂 + 𝒇𝒖𝒅𝒈𝒆)^(1/2) 4. (𝒕𝒊𝒎𝒆 + 𝒕𝒊𝒅𝒆)^(1/2) /n𝒐𝒃𝒐𝒅𝒚 5. (−𝒃 ± (𝒃^𝟐 − 𝟒𝒂𝒄)^(1/2))/𝟐𝒂 6. |𝒙 − 𝒚|

1. sqrt(x + y) 2. pow(x, x + 7) 3. sqrt(area + fudge) 4. sqrt(time _tide)/nobody 5. (-b + sqrt(b b - 4 a c)) / (2 a) 6. abs (x - y) or labs(x - y) or fabs(x - y)

What will the value of result be after the following statement executes? result = 6 - 3 * 2 + 7 - 10 / 2 ;

2

to get absolute value 1) sqrt(x) 2) abs(x) 3) setw() 4) rand( )

2

what is the value of the following sqrt(sqrt(pow(2.0,4.0)));

2

The rand function generates a data value of the type: 1. short int. 2. unsigned int. 3. long int. 4. int.

2. unsigned int.

the following C++ statement, what will be executed first according to the order of precedence? result = 6 - 3 * 2 + 7 - 10 / 2 ;

3*2

srand: 1. Should be used instead of rand to generate truly random numbers. 2. Is unnecessary in C++. 3. Can use the time function's return value as an optimal seed value. 4. Should be called before each call to rand.

3. Can use the time function's return value as an optimal seed value.

Which line in the following program will cause a compiler error? 1 #include <iostream> 2 using namespace std; 3 4 int main() 5 { 6 const int MY_VAL = 77; 7 MY_VAL = 99; 8 cout << MY_VAL << endl; 9 return 0; 10 }

7

What is the output produced by the following code fragment? int i = 3; cout << "The value of i is " << sqrt(pow(i,4.0)) << endl;

9 (with showpoint 9.0)

The function, pow(x, 5.0), requires this header file.

<cmath>

When using the sqrt function you must include this header file.

<cmath>

Where are functions with a power in?

<cmath>

formal parameter

A formal parameter is used like a variable within the function's body, but it is declared in the function's parameter list; it is not declared in the function's body. A formal parameter is a parameter as used in the formal definition of the function. example: int prompt(int n)

cstdlib

A library header that contains the absolute value functions abs (absolute value for int) and labs (absolute value for long), as well as srand (seeded random number generator) and rand (random number). Make sure that if you use any of these functions that you have the include directive #include <cstdlib> .

sqrt(x)

An argument of double data is passed to sqrt (square root) function EX: double val = sqrt (9.0); Square root of 9 = 3 Requires #include<cmath> ' '

1) The ________ causes a program to wait until information is typed at the keyboard and the Enter key is pressed. A) Output stream B) cin object C) cout object D) Preprocessor E) None of the above

B

power function

Performs operation x^y Syntax: pow(x,y) pow(2,7) is 128

Purpose of a function prototype

Prototypes are placed before the main function so that the program can compile correctly when the function definitions are after the main function.

fabs

The absolute value returned as a FLOAT EX: float val = fabs(-5.003); // returns 5.003 Requires #include<cmath> Ex: fabs (-5.1) is 5.1

abs

The absolute value returned as an INTEGER EX: int val = abs(-5); // returns 5 Requires #include<cmath> #include <stdlib>

What are values passed to a function called?

arguments, or parameters

Parameters

ariables in a function that hold the values passed as arguements like the pow

To find the length of a variable a) cin.get(var) b) var.length() c) rand( ) d) setw()

b

Used to name a variable that is a letter, not a number, but put into '...' a) CPU b) char c) int d) fixed

b

What's used to demote or promote values (eg turning a double into an integer) a) #include <cstlib> b) static_cast<datatype>(value) c) #include <string> d) #include <iomanip>

b

This manipulator causes the field to be left-justified with padding spaces printed to the right.

left

square root function (give syntax and parameters)

sqrt(x) expects 1 double type expression >= 0.0 as its argument

sqrt (x)

square root of x (where x is a nonnegative value) sqrt (9.0) is 3.0

Returns the index in the string of the start of the search string or the bigger number than the length of the string if not found

str.find('R') str.find("dog") str.find('R', 3) str.fin("dog", 3)

Returns the number of characters currently stored in the string object

str.length()

Syntax for converting a string variable to a c-string

strVar.c_str()

the function that will return or set the character at a given location of a string.

string characters use 0 based indexing ex. word = "programming"; ch = word.at(3); // ch gets g - ch is a char word.at(0) = 'k'; // p is changed to k word.at(5) = ch; // a is changed to g

the function that will return a string consisting of characters from the invoking string.

substr ( ) takes 2 parameters, the starting point and the number of characters to copy. the return type for substr( ) is string ex. word = "programming"; w = word.substr(3, 4); // w gets gram t = word.substr(7,2); // t gets mi left = word.substr(5); // left gets amming

Which of the following is not included in math library? 1. log. 2. pow. 3. floor. 4. ln

log

log10(x)

logarithm of x (base 10) log 10( 100.0) is 2.0

log (x)

natural logarithm of x (base e) Ex: log (2.718282) is 1.0

To set a maximum value on the random number generator you must type this. (the variable = number)

number = rand() % max +1

How to put 4.0 to the power of 2:

pow(4.0,2)

power function (give syntax and parameters)

pow(x,y) expects 2 double type expressions if x=0.0, y must be positive if x<=0.0, y must be a whole number returns x^y

exp(x)

raise "e" to a power exponential function e^x Ex: exp( 1.0 ) is 2.718282

How to get a random number.

rand() <cstdlib>

fmod (x , y)

remainder of x/y as a floating point number <cmath> Ex: fmod (2.6,1.2) is 0.2

x = fabs(x); what does it do?

replaced the double stored in x with its absolute value, as well as not changing its original assigned statement. `

Function parts

return type- data type of value that called it name -follows same rule as varibles parameter list- variables containing values passed to function body-enclosed in {}

getline(cin, newString);

takes two parameters.The first is the name of the input stream, and the second is the name of the string variable into which the string is to be read. basically gets the entire name inputted by the user in the line and stores it in the second parameter. ex: cout << "Enter your name " << endl; cin >> s1; ouput: john doe. only john will be stored in s1 so instead getline takes john doe w/ the whitespace and stores it in s1

CLOCKS_PER_SEC

the CLOCKS_PER_SEC constant is 1,000, which means the call clock() returns the number of milliseconds that the program has been running. #include <iostream> #include <ctime> int main() { char letter; std::cout << "Enter a character: "; clock_t seconds = clock(); // Record starting time std::cin >> letter; clock_t other = clock(); // Record ending time std::cout << static_cast<double>(other - seconds)/CLOCKS_PER_SEC << " seconds\n"; }

If a function returns a value, what must it include?

the type of value must be indicated, such as int main. Main can not be void

cos(x)

trigonometric cosine of x (x in radians) Ex: cos (0.0) is 1.0

sin (x)

trigonometric sine of x (x in radians) sin(0.0) is 0

tan (x)

trigonometric tangent of x (x in radians) tan (0.0) is 0

actual parameter

value1 = prompt(1); the parameter (or argument) passed into the function, 1, is called the actual parameter. An actual parameter is the parameter actually used during a call of the function.

function prototypes

ways to notify the compiler about a function before a call to the function

Can a function have more then one parameter? Can it have none?

yes

This manipulator is used to establish a field width for the value immediately following it.

setw

strings

initial whitespace is skipped when reading data into a variable of type string and that the next whitespace character encountered stops the reading; white space denote the end of the input string

ceil(double)

input/output double,rounds up, cmath rounds x to the smallest integer not less than 1. Ex: ceil(9.2) is 10.0

labs(long)

absolute value for long, cstdlib

clock()

clock_t clock(void) Returns process time since start of program execution clock_t: unsigned long to convert to time just divide by CLOCKS_PER_SEC

example of sending returning value to cout

cout << pow(x,y);

To use the rand() function, you must #include this header file in your program.

cstdlib

Given the following include directive (to get the declaration for the pow function from the math library): #include <cmath> Now make these declarations: double base = 2, exponent = 3, power = 4; Which of the following are syntactically correct invocations for the pow function? a) power = pow(base, exponent); b) pow(power, base, exponent); c) pow(base, exponent) = power; d) base = pow(exponent, power); e) both a and d

E

When a variable is assigned a number that is too large for its data type, it: A) underflows B) overflows C) reverses polarity D) exceeds expectations E) None of the above

B

This stream manipulator forces cout to print the digits in fixed-point notation. A) setprecision(2) B) setw(2) C) fixed D) setfixed(2) E) None of these

C

When the final value of an expression is assigned to a variable, it will be converted to: A) The smallest C++ data type B) The largest C++ data type C) The data type of the variable D) The data type of the expression E) None of the above

C

Given the line of input data 123.456 A 789 what is read into inputChar by the following code? Assume that alpha and beta are type int, and inputChar is of type char. cin >> alpha >> inputChar >> beta; (A) ' ' (blank) (B) 'A' (C) '.' (period) (D) '4' (E) none of the above

C(C) '.' (period)

floor function

Calculates largest whole number that is less than or equal to its argument. rounds x to the largest integer not greater than x Ex: floor (9.2) = 9.0

The total number of digits that appear before and after the decimal point is sometimes referred to as: A) floating points B) significant digits C) precision D) B and C E) None of these

D

Given the following declarations and the function prototype for the fabs math library function: double x, y, z; double fabs (double); Which of the following shows a syntactically invalid call to fabs? (A) z = fabs (x - y); (B) z = fabs (x); (C) cout << fabs (y); (D) z = y + fabs(y - x); (E) z = fabs (x, y);

E

void

If a function doesn't return a value, its return type is void

Why do changes to the parameter in the function do not affect the value of the argument?

It is a copy. The original is still the same. it has a different memory address in the function and a different scope.

Overloading functions

The functions have the same name but different parameter lists.

How are actual and formal parameters related?

There must be a one-to-one correspondence. A function must be called with the same number of parameters with which it was declare

What are overloading functions used for?

They are used to create functions that perform the same tasks but take different parameter types or different number of parameters.

To assign a variable to a statement eg)"hello" a) string b) sqrt(x) c) setw() d) int

a

To copy a string use this a) strcpy(var," ") b) float, double c) setprecision() d) var.length()

a

example of returning value for assigning it to a value

a = pow(x,y);

RAND_MAX

a C++ built-in constant that represents the largest integer generated by the rand function; although the value of RAND_MAX varies with different computer system, its value is always at least 32767

srand function

a C++ built-in function used to initialize the rand function <cstdlib>

sqrt function

a C++ built-in function whose purpose is to return the square root of a number that has either the double or float data type; returns the square root as a double number; a program that uses the sqrt function must contain the #include <cmath> directive

rand function

a built-in C++ function that returns a random ineger that is greater than or qual to zero but less than or equal to the value stored in the RAND_MAX constant; the pseudo-random number generator in C++

time function

a built-in C++ function that returns the current time(according to your computer system's clock) as seconds elapsed since midnight on January 1, 1970; often used as the seed argument in the srand function; a program that uses the time function must contain the #include <ctime> directive

When using a string, you have to put ___ in the beginning of your code. a) #include <iomanip> b) #include <string> c) left and right d) #include <cstlib>

b

Information Hiding is analogous to using a

black-box methodology

To prevent the program from putting the output in scientific notation, use this. a) CPU b) int c) fixed d) #define

c

What you put in front of a variable to allow decimals a) #define b) int c) double d) CPU

c

to get square root a) abs(x) b) string c) sqrt(x) d) setw()

c

This function tells the cin object to skip one or more characters in the keyboard buffer.

cin.ignore(80, '\n')

To ignore a character and number in cin

cin.ignore(n, c)

When using fixed and setprecision together, the setprecisison will no longer print the number of sig figs, but it will print the:

d

cin.ignore(80, '\n')

defined in file <iostream>;has two parameters. first is an int expression and the second is a character;skips the number of characters specified in the first parameter or all the characters up to and including the character specified in the second parameter, whichever comes first; e.g. skips 80 characters or skips to the beginning of the next line depending on whether a newline character is encountered before 80 characters are skipped

Returns the index in the string of the first occurance of any character in the given string or string::npos(-1) if not found

find_first_of

what is the output of? char ch='G'; cout<<tolower(ch)<<endl;

g

________ reads a line of input, including leading and embedded spaces, and stores it in a string object.

getline( cin, stirng)

Which statement will read an entire line of input into the following string object? string address;

getline(cin, address)


Ensembles d'études connexes

(Chapter 2) Business Ethics & Social Responsibility

View Set

General Pathology Questions (Part 4/4)

View Set

Series 79 Unit 7 - Registration Process

View Set

vocabulary workshop level d units 1-5

View Set

Managing people and work Exam 2 (Chen)

View Set

Chapter 4: International Financial Reporting Standards Part I

View Set