Chapter 4

Ace your homework & exams now with Quizwiz!

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.

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

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

#include <iostream> using namespace std; int main( ) { cout << "hello world" << endl; return 0; } b. //To determine if the compiler will allow spaces //between the # and include in the #include: # include<iostream> using namespace std; //The rest of the program can be identical to the above.

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) { if (first_par >= second_par) return 'W'; else return 'H'; }

. Wow

Write a function declaration and a function definition for a function that takes one argument of type double. The function returns the character value 'P' if its argument is positive and 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 that 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); }

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

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_digit that takes one argument of type char and returns a bool value. The function returns true if the argument is a decimal digit; otherwise, it returns false

bool is digit(char ch) { return ('0' <= ch) && (ch <= '9'); }

Write a function definition for a function is_root_of that takes two arguments 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); }


Related study sets

US History Chap. 3 - The Revolutionary War

View Set

Unit #5 - Chapter 12 Study Guide

View Set

(1/8)Legal Exam #2: CH 5 Standards of Care

View Set

Chapter 29 - Acute Respiratory Distress Syndrome

View Set