CS chapter 4

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

What is the output produced by the following program? #include <iostream> using namespace std; char mystery(int first_par, int second_par); int main() { cout << mystery(10, 9) << "ow\n"; return 0; } char mystery(int first_par, int second_par); int main() { if (first_par >= second_par) return „W‟; else return „H‟; }

wow

Functions

Subparts of a C++ program. A function is the embodiment of a subtask that was created during the top-down design phase of breaking down a program into its basic elements. These subtasks are also subalgorithms of the program. Functions make it easier to understand a program and easier to change it if necessary. Functions also make a C++ program easier to write, test, and debug. Additionally, by dividing the task of writing a program into subtasks, which are then written as functions, it becomes possible to write an application or a program in a reasonable length of time. Subtasks are written by members of programming teams. Teams must be used to produce programs that run such things as electricity grids, telephone systems, and weather patterns. Other programming-language programs may call functions as subprograms, procedures, or methods.

Carefully describe the call-by-value parameter mechanism.

Suppose the function is defined with arguments, say param1 and param2. The function is then called with corresponding arguments arg1 and arg2. The values of the arguments are "plugged in" for the corresponding formal parameters, arg1 into param1, arg2 into param2. The formal parameters are then used in the function.

Function body

That part of a called function that determines what value will be returned. The function body follows the function header and completes the function definition. The function body consists of declarations and executable statements enclosed with a pair of braces. Thus, the function body is just like the body of the main part of a program. When the function body is called, the argument values are plugged in for the formal parameters and then the statements in the body are executed. The value returned by the function is determined when the function executes a return statement.

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; }

Determine the value of the following arithmetic expressions: 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

Convert each of the following mathematical expressions to a C++ arithmetic expression: 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)

Preprocessor

A compiler's preliminary processor that processes the include directives.

Global named constant

A constant variable that is used by more than one function. It is placed at the beginning of your program, outside the body of all the functions and outside the body of the main part of your program. The global named function can be used in any function definition that follows the constant declaration.

Header file

A file whose name is found between the angular brackets in an include directive. The header file is the name of a library that contains basic information that is needed to compile a program.

Call-by-value parameters

A function that can be used in a Boolean expression to control an if-else statement or to control a loop statement, or it can be used anywhere else that a Boolean expression is allowed. The returned type for such a function should be the type bool. A call to a function that returns a Boolean value of true or false can be used anywhere that a Boolean expression is allowed.

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> .

Pseudocode

A mixture of C++ (or whatever programming language you are using) and ordinary English (or whatever human language you are using). Pseudocode is typically used to express algorithms. Pseudocode allows you to state your algorithm precisely without having to worry about all the details of C++ syntax. When the C++ code for a step in your algorithm is obvious, there is little point in stating it in English. When a step is difficult to express in C++, the algorithm will be clearer if the step is expressed in English.

int to double Conversion function

A predefined function that uses the notation static_cast<double> and which will convert a value of some other type to a value of type double. For example, static_cast<double>(2)returns 2.0.This kind of return is called type casting. (Type casting can also be done with types other than double, but this will come later in this course.) SYNTAX static_cast<double>(Expression_of_Type_int) EXAMPLE int total_pot, number_of_winners; double your_winnings; ... your_winnings = static_cast<double (total_pot)/number_of_winners; The above expression follows PEMDAS rules.

Call-by-value mechanism

A process in which the arguments from a calling function are substituted for the parameters in a called function. With respect to the substitution process in the call-by-value mechanism, you should keep in mind the following three things: 1. It is the value of the arguments that are plugged in for the formal parameters. If the arguments are variables, the values of the variables, not the variables themselves, are plugged in. 2. The first argument is plugged in for the first formal parameter in the parameter list, the second argument is plugged in for the second formal parameter in the list, and so forth. 3. When an argument is plugged in for a formal parameter (for instance, when 2 is plugged in, say, for number_par (name of a formal parameter, or just parameter)), the argument is plugged in for all instances of the formal parameter that occur in the function body (for instance, 2 is plugged in for number_par (the formal parameter under consideration herewith) each time it appears in the function body.

Overloading

A situation when the same function name is used for two or more function definitions. When you overload a function name, the function definitions must have different numbers of formal parameters or some formal parameters of different types. When there is a function call, the compiler uses the function definition whose number of formal parameters and types of formal parameters match the arguments in the function call.

return statement

A statement that consists of the keyword return followed by an expression. When the return statement is executed, the value of the subsequent expression is returned as the value of the function call.

Black box

A trope intended to convey the image of a physical device that you know how to use but whose method of operation is a mystery, because it is enclosed in a "black box" and you cannot see inside the "box" (and cannot pry it open!). With respect to programming, if a function is well designed, the programmer can use the function as if it were a black box. All the programmer needs to know is that if he or she puts appropriate arguments into the black box, then an appropriate returned value will come out of the black box.

Function declaration

An announcement that tells the compiler how a function is called. C++ protocol requires that either the complete function definition or the function declaration appears in the code before the function is called. As an example of a function declaration, we have double total_cost (int number_par, double price_par); Note that the function declaration ends in a semi-colon. Function declarations normally appear before the main part of a program. The declaration tells you everything you need to know to write a call to a function. It tells you the name of the function (in this case we have total_cost). It tells you how many arguments the function needs and what type the arguments should be; in this case, one argument will be an int and the other a double. The identifiers number_par and price_par are called formal parameters. A formal parameter is used as a kind of blank, or place holder, to stand in for the argument. When you write a function declaration, you do not know what the arguments will be, so you use the formal parameters in place of the arguments. The names of the formal parameters can be any valid identifiers; while you get used to working with parameters, your textbook will add '_var' to the end of a formal parameter name. This is done to ensure the distinction between formal parameter names and other items in a program.

Function prototype

An announcement that tells the compiler how a function is called. C++ protocol requires that either the complete function definition or the function prototype appears in the code before the function is called. As an example of a function prototype, we have double total_cost (int number_par, double price_par); Note that the function prototype ends in a semi-colon. Function prototypes normally appear before the main part of a program. The prototype tells you everything you need to know to write a call to a function. It tells you the name of the function (in this case we have total_cost). It tells you how many arguments the function needs and what type the arguments should be; in this case, one argument will be an int and the other a double. The identifiers number_par and price_par are called formal parameters. A formal parameter is used as a kind of blank, or place holder, to stand in for the argument. When you write a function prototype, you do not know what the arguments will be, so you use the formal parameters in place of the arguments. The names of the formal parameters can be any valid identifiers; while you get used to working with parameters, your textbook will add '_var' to the end of a formal parameter name. This is done to ensure the distinction between formal parameter names and other items in a program.

Function call

An expression consisting of the function name followed by arguments enclosed in parentheses. If there is more than one argument, the arguments are separated by commas. A function call is an expression that can be used like any other expression of the type specified for the value returned by the function. SYNTAX Function_Name (Argument_List) where the Argument_List is a comma-separated list of arguments: Argument_1, Argument_2, . . . , Argument_Last EXAMPLES side = sqrt(area); cout << "2.5 to the power 3.0 is " << pow(2.5, 3.0); A function call is also called a function invocation.

Function invocation

An expression consisting of the function name followed by arguments enclosed in parentheses. If there is more than one argument, the arguments are separated by commas. A function call is an expression that can be used like any other expression of the type specified for the value returned by the function. SYNTAX Function_Name (Argument_List) where the Argument_List is a comma-separated list of arguments: Argument_1, Argument_2, . . . , Argument_Last EXAMPLES side = sqrt(area); cout << "2.5 to the power 3.0 is " << pow(2.5, 3.0); A function invocation is also called a function call.

Argument

An input, or value, supplied to a function. More than one value may be supplied to a function

Function definition

Describes how the function computes the value it returns. If you think of a function as a small program within your program, then the function definition is like the code for this small program. Take note of the fact that the syntax for the definition of a function is very much like the syntax for the main part of a program. A function definition consists of a function header followed by a function body. Keep in mind that a function is like a small program and to understand a function, consider the following three points: A function definition is like a small program, and calling the function is the same thing as running this "small program." A function uses formal parameters, rather than cin, for input. The arguments to the function are the input, and they are plugged in for the formal parameters. A function of the kind discussed in Chapter 4 does not normally send any output to the screen, but it does send a kind of "output" back to the program. The function returns a value, which is like the "output" for the function. The function uses a return statement instead of a cout statement for this "output."

Information hiding

Designing a function so that it can be used as a black box to emphasize that the programmer acts as if the body of the function were hidden from view.

include directives

Directives (orders) given to the compiler to include certain files that are needed for the processing of the program at hand. The mentioned files are called header files, and they are given inside angular brackets, to wit <>. Include directives deliver the basic information about a library. This information allows the linker (mentioned in Chapter 1) to find object code for the functions in the library so that they can correctly link the library to your program. Remember that the linker is a program that combines its own object code for various routines with the object code of your own program through a process called linking.

Suppose a function named Function1 has a variable named sam declared with the definition of Function1, and a function named Function2 also has a variable named sam declared within the definition of Function2. Will the program compile (assuming everything else is correct)? If the program will compile, will it run (assuming that everything else is correct)? If it runs, will it generate an error message when run (assuming everything else is correct)? If it runs and does not produce an error message when run, will it give the correct output (assuming everything else is correct)?

Everything will be fine. The program will compile (assuming everything else is correct). The program will run (assuming that everything else is correct). The program will not generate an error message when run (assuming everything else is correct). The program will give the correct output (assuming everything else is correct).

Predefined functions

Functions that have been written for libraries used in a programminglanguage program. These functions can be easily used in program writing

Formal parameters

Identifiers between the parentheses after the name of a function declaration (function prototype) or function definition. These identifiers are used blanks or place holders that stand in for arguments that are sent to them. When you write a function declaration, you do not know what the arguments will be, so you use the formal parameters in place of the arguments. The names of the formal parameters can be any valid identifiers, but for a while, we will end our formal parameter names with _par so that it will be easier for us to distinguish them from other items in a program.

If you use a variable in a function definition, where should you declare the variable? In the function definition? In the main part of the program? Any place that is convenient?

If you use a variable in a function definition, you should declare the variable in the body of the function definition.

Carefully describe the process of program testing.

In order to increase your confidence in your program, you should test it on input values for which you know the correct answers. Perhaps you can calculate the answers by some other means, such as pencil and paper or hand calculator.

Floating-point numbers

Numbers with a fraction after the decimal point. Numbers with type double are floating-point numbers.

Global variables

Ordinary variables without the const modifier that are accessible to all function definitions in the file. This is done the same way that it is done for global named constants, except that the modifier const is not used in the variable declaration. However, there is seldom any need to use such global variables. Moreover, global variables can make a program harder to understand and maintain, so we will not use any global variables. Once you have had more experience designing programs, you may choose occasionally to use global variables. However, it's better not to use them.

List the similarities and differences between the use of a predefined (that is, library) function and a user-defined function.

Predefined (library) functions usually require that you #include a header file. For a programmer defined function, the programmer puts the code for the function either into the file with the main part of the program or in another file to be compiled and linked to the main program.

What is the purpose of the comment that accompanies a function declaration?

The comment explains what value the function returns and gives any other information that you need to know in order to use the function.

Look at the program in Display 4.18. The main function contains the using directive: using namespace std; Why doesn't the method unitprice contain this using directive?

The definition of unitprice does not do any input or output and so does not use the library iostream. In main we needed the using directive because cin and cout are defined in iostream and thosedefinitions place cin and cout in the std namespace.

Function description

The depiction of a function that takes into account both the function declaration and the function definition.

cmath

The file name of the math library used in an include directive. This library contains predefined functions such as sqrt (square root), pow (powers), fabs (absolute value for double), ceil (ceiling, i.e., rounding up of a number), and floor (rounding down of a number). Make sure that if you use any of these functions that you have the include directive #include <cmath> .

Function header

The first line of a function. It is written the same way as the function declaration, except that the header does not have a semicolon at the end.

Suppose you have two function definitions with the following function declarations: double score(double time, double distance); int score(double points); Which function definition would be used in the following function call and why would it be the one used? (x is of type double.) final_score = score(x);

The function call has only one argument, so it would use the function definition that has only one formal parameter.

Suppose you have two function definitions with the following function declarations: double the_answer(double data1, double data2); double the_answer(double time, int count); Which function definition would be used in the following function call and why would it be the one used? (x and y are of type double) x = the_answer(y, 6.0);

The function call has two argument of type double, so it would use the function corresponding to the function declaration with two arguments of type double (that is, the first function declaration).

Write a function declaration and a function definition for a function that takes one argument of type double. The function returns „N‟ if its argument is zero or negative.

The function declaration is: char positive_test(double number); //Returns „P‟ if number is positive. //Returns „N‟ if number is negative or zero. The function definition is: char positive_test(double number) { if (number > 0) return „P‟; else return „N‟; }

Write a function declaration and a function definition for a function that takes one argument of type int and one argument of type double, and that returns a value of type double that is the average of the two arguments.

The function declaration is: double ave(int n1, double n2); //Returns the average of n1 and n2. The function definition is: Double ave(int n1, double n2) { return ((n1 + n2)/2.0); }

Write a function declaration and function definition for a function called read_filter that has no parameters and that returns a value of type double. The function read_filter prompts the user for a value of type double and reads the value into a local variable. The function returns the value read provided this value is greater than or equal to zero and returns zero if the value read is negative.

The function declaration is: double read_filter(); //Reads a number from the keyboard. Returns the number //read provided it is >= 0; otherwise returns zero. The function definition is: //uses iostream double read_filter() { using namespace std; double value_read; cout << "Enter a number:\n"; cin >> value_read; if (value_read >= 0) return value_read; else return 0.0; }

Write a function declaration and a function definition for a function that takes three arguments, all of type int, and which returns the sum of its three arguments.

The function declaration is: int sum(int n1, int n2, int n3); //Returns the sum of n1, n2, and n3. The function definition is: int sum(int n1, int n2, int n3) { return (n1 + n2 + n3); }

The following function is supposed to take as arguments a length expressed in feet and inches and return the total number of inches in that many feet and inches. For example, total_inches(1, 2) is supposed to return 14, because 1 foot and 2 inches is the same as 14 inches. Will the following function perform correctly? If not, why not? double total_inches (int feet, int inches) { inches = 12 * feet + inches; return inches; }

The function will work fine. That is the entire answer, but here is some additional information. The formal parameter inches is a call-by-value parameter and, as discussed in the text, it is therefore a local variable. Thus, the value of the argument will not be changed.

Stepwise refinement

The method used to write a program. One first designs the method that the program will use and then writes out this method in English, as if the instructions were to be followed by a human clerk. These instructions are the algorithm. A good plan of attack for designing the algorithm is to break down the task to be accomplished into a few subtasks, decompose each of these subtasks into smaller subtasks, and so forth. Eventually the subtasks become so small that they are trivial to implement in C++. Topdesign is also called top-down design or, more graphically, divide and conquer.

Divide and conquer

The method used to write a program. One first designs the method that the program will use and then writes out this method in English, as if the instructions were to be followed by a human clerk. These instructions are the algorithm. A good plan of attack for designing the algorithm is to break down the task to be accomplished into a few subtasks, decompose each of these subtasks into smaller subtasks, and so forth. Eventually the subtasks become so small that they are trivial to implement in C++. Topdesign is also called top-down design or stepwise refinement.

Top-down design

The method used to write a program. One first designs the method that the program will use and then writes out this method in English, as if the instructions were to be followed by a human clerk. These instructions are the algorithm. A good plan of attack for designing the algorithm is to break down the task to be accomplished into a few subtasks, decompose each of these subtasks into smaller subtasks, and so forth. Eventually the subtasks become so small that they are trivial to implement in C++. Topdesign is also called stepwise refinement, or more graphically, divide and conquer.

Call-by-value formal parameters

The parameters given in a called function.

Placing of using directive

The placing of a using directive, such as using namespace std;, at the beginning of the file is not always a good idea. Bear in mind also, that in due course we will be using more namespaces than just std. In fact, we may be using different namespaces in different function definitions. If you place the directive using namespace std; inside the brace { that starts the body of a function definition, then the using directive applies to only that function definition. This will allow you to use two different namespaces in two different function definitions, even if the two function definitions are the same file and even if the two namespaces have some name(s) with different meanings in the two different namespaces. Placing a using directive inside a function definition is analogous to placing a variable declaration inside a function definition. If you place a variable definition inside a function definition, the variable is local to the function; that is, the meaning of the variable declaration is confined to the function definition. If you place a using directive inside a function definition, the using directive is local to the function definition; in other words, the meaning of the using directive is confined to the function definition.

What is the principle of procedural abstraction as applied to function definitions?

The principle of procedural abstraction says that a function should be written so that it can be used like a black box. This means that the programmer who uses the function need not look at the body of the function definition to see how the function works. The function declaration and accompanying comment should be all the programmer needs to know in order to use the function.

Program testing

The procedure of increasing one's confidence in one's program by testing a number of input values to ensure that the answers produced are correct. This should be done with paper and pencil (pen will do too) or a handheld calculator. However, be aware of the fact that your program may still not be entirely correct. An incorrect program can sometimes give the correct answer, despite the fact that it will give incorrect answers on some other inputs. You may have tested an incorrect program on one of the cases for which the program happens to give the correct output.

Local scope

The scope of a local variable that refers to the part of a program that can directly access variable.

Global scope

The scope of global identifiers declared at the beginning of your program, outside the body of all the functions.

Block scope

The scope that characterizes both local scope and global scope. A block is some C++ code enclosed in braces, with the exception of the "global block," which is an implied outermost block that encompases all code. The scope rule states that the identifiers declared within their block are local to that block and accessible only from the point they are defined to the end of their block. Blocks are commonly nested. For example, the braces of the main function defines a block and a for loop inside main defines a nested block.

Suppose you have two function definitions with the function declarations given in Self-Test Exercise 25. Which function definition would be used in the following function call and why would it be the one used? x = the_answer(5, 6.0)

The second argument is of type double and the first argument would be automatically converted to type double by C++ if needed, so it would use the function corresponding to the function declaration with two arguments of type double (that is, the first function declaration).

Suppose you have two function definitions with the function declarations given in Self-Test Exercise 25. Which function definition would be used in the following function call and why would it be the one used? x = the_answer(5, 6)

The second argument is of type int and the first argument would be automatically converted to type double by C++ if needed, so it would use the function corresponding to the function declaration with the first argument of type double and the second argument of type int (that is, the second function declaration).

Value returned

The value that a function computes on the basis of the input that it has received. A function cannot have more than one returned value

This question has to do with the Programming Example "Revised Pizza-Buying Program." Suppose the evil Pizza parlor that is always trying to fool customers introduces a square pizza. Can you overload the function unitprice so that it can compute the price per square inch of a square pizza as well as the price per square inch of a round pizza? Why or why not?

This cannot be done (at least not in any nice way). The natural ways to represent a square and a round pizza are the same. Each is naturally represented as one number, which is the diameter for a round pizza and the length of a side for a square pizza. In either case the function unitprice would need to have one formal parameter of type double for the price and one formal parameter of type int for the size (either radius or side). Thus, the two function declarations would have the same number and types of formal parameters. Specifically, they would both have one formal parameter of type double and one formal parameter of type int.) Thus, the compiler would not be able to decide which definition to use. You can still defeat this evil pizza parlor's strategy by defining two functions, but they will need to have different names.

Local variables

Variables that are declared within the body of a function definition. We can also say that the function with which they are associated is their scope (i.e., the limits of their use). Variables that are declared within the main part of the program are said to be local to the main part of the program or to have the main part of the program as their scope. When we say that a variable is a local variable without any mention of a function and without any mention of the main part of the program, we mean that the variable is local to some function definition. If a variable is local to a function , then you can have another variable with the same name is declared in the main part of the program or in another function definition, and these will be two different variables, even though they have the same name.

What does it mean when we say the programmer who uses a function should be able to treat the function like a black box? (Hint: This question is very closely related to the previous question.)

When we say that the programmer who uses a function should be able to treat the function like a black box, we mean the programmer should not need to look at the body of the function definition to see how the function works. The function declaration and accompanying comment should be all the programmer needs to know in order to use the function.

Loop body as a function call

Whenever you have a loop nested within a loop, or any other complex computation included in a loop body, make the loop body a function call. This way you can separate the design of the loop body from the design of the rest of the program. This divides your programming task into two smaller subtasks.

Procedural abstraction

Writing and using functions as if they were black boxes. When programming in C++, it might make more sense to call it functional abstraction. However, procedure is a more general term than function. Computer scientists use the term procedure for all "function-like" sets of instructions, and so they use the term procedural abstraction. The term abstraction is intended to convey the idea that when you use a function as a black box, you are abstracting away the details of the code contained in the function body. You can call this technique the black-box principle or the principle of procedural abstraction or information hiding. The three terms mean the same thing. Whatever you call this principle, the important point is that you should use it when designing and writing your function definitions. Keep in mind that the principle of procedural abstraction, when applied to a function definition, should be written so that it can be used like a black box. This means that the programmer who uses the function should not need to look at the body of the function definition to see how the function works. The function declaration and the accompanying comment should be all the programmer needs to know in order to use the function. To ensure that your function definitions have this important property, you should strictly adhere to the following rules: HOW TO WRITE A BLACK-BOX FUNCTION DEFINITION (THAT RETURNS A VALUE) The function declaration comment should tell the programmer any and all conditions that are required of the arguments to the function and should describe the value that is returned by the function when called with these arguments. All variables used in the function body should be declared in the function body. (The formal parameters do not need to be declared, because they are listed in the function declaration.)

Consider two possible definitions for the function unitprice. One is the definition given in Display 4.10. The other definition is the same except that the type cast static_cast<double>(2) is replaced with the constant 2.0; in other words, the line radius = diameter/static_cast <double>(2); is replaced with the line radius = diameter/2.0; Are these two possible function definitions black-box equivalent?

Yes, the function would return the same value in either case, so the two definitions are black-box equivalent.

Alternate form for function declarations

You are not required to list formal parameter names in a function declaration. The following two function declarations are equivalent: double total_cost (int number_par, double price_par); and double total_cost (int, double); We will always use the first form so that we can refer to the formal parameters in the comment that accompanies the function declaration. This alternate form applies only to function declarations. Function headers must always list the formal parameter names.

Write and compile short programs to test the following issues: a. Determine whether your compiler will allow the #include <iostream> anywhere on the line, or if the # needs to be flush with the left margin. b. Determine whether your compiler will allow a space between the # and the include.

a. //To determine whether the compiler will tolerate //spaces before the # in the #include: #include <iostream> using namespace std; int main () { cout << "hello world" << endl; return 0; } b. //To determine whether the compiler will allow spaces //between the # and include in the #include: # include <iostream> using namespace std; int main () { cout << "hello world" << endl; return 0; }

Write a function definition for a function called even that takes one argument of type int and returns a bool value. The function returns true if its one argument is an even number; otherwise, it returns false.

bool even(int n) { return ((n % 2) == 0); } or bool is_digit(char ch) { return („0‟ <= ch) && (ch <= „9‟); }

Write a function definition for a function called in_order that takes three arguments of type int. The function returns true if the three arguments are in ascending order; otherwise, it returns false. For example, in_order(1, 2, 3) and in_order(1, 2, 2) both return true, while in_order(1, 3, 2) returns false.

bool in_order(int n1, int n2, int n3) { return ((n1 <= n2) && (n2 <= n3)); }

Write a function definition for a function is_root_of that takes one argument of type int and returns a bool value. The function returns true if the first argument is the square root of the second; otherwise, it returns false.

bool is_root_of(int root_candidate, int number) { return (number == root_candidate * root_candidate); }


Set pelajaran terkait

English 12B Unit 3: Goodbye to Romance (The Enlightenment/Neoclassic, 1660-1798, & Romantic Period, 1798-1837)

View Set

Nagelhout Ch. 26 Cardiac Surgery

View Set

Intermediate Financial Management Ch. 16; Capital Structure Decisions

View Set

Theorems About Roots of Polynomial Equations

View Set