6.14: Overloading Functions, The exit() Function, Stubs and Drivers

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Assume you need to test a function named inOrder. The function inOrder receives three int arguments and returns true if and only if the arguments are in non-decreasing order: that is, the second argument is not less than the first and the third is not less than the second. Write the definition of driver function testInOrder whose job it is to determine whether inOrder is correct. So testInOrder returns true if inOrder is correct and returns false otherwise. . For the purposes of this exercise, assume inOrder is an expensive function call, so call it as few times as possible!

bool testInOrder(){ if (inOrder(0,1,2) == false) return false; if (inOrder(1,1,1) == false) return false; if (inOrder(1,2,1) == true) return false; if (inOrder(2,1,2) == true) return false; if (inOrder(2,1,0) == true) return false; if (inOrder(1,2,2) == false) return false; if (inOrder(3,2,2) == true) return false; if (inOrder(1,1,2) == false) return false; if (inOrder(2,2,1) == true) return true; if (inOrder(1,2,2) == false) return false; return true; }

Assume you need to test a function named max. The functionmax receives two int arguments and returns the larger . Write the definition of driver function testmax whose job it is to determine whether max is correct. Sotestmax returns true if max is correct and returns false otherwise.

bool testmax() { if (max(1,2) == 2 && max(2,1) == 2 && max(1,1)==1) return true; else return false; }

write the definitions for three functions named max. Each receives two parameters , of the same type , and returns the larger of the two values . Define one of these functions to apply to type double , another to type int and a third to type char .

int Max(int iInput){ static int iHighest=0; if (iInput>iHighest) iHighest=iInput; return iHighest; }

Write the definition of a function named panic. The function prints the message "unrecoverable error" and then terminates execution of the program , indicating failure..

void panic(){ cout << "unrecoverable error" << endl; exit (EXIT_FAILURE); }


Kaugnay na mga set ng pag-aaral

Chapter 1 The Role of Human Resources

View Set

CE421: Short Quiz 1 (Road Signs, Signals, and Symbols)

View Set

L'accord des adjectifs qualificatifs de couleur

View Set

Chapter 18 - Foreign Policy and Democracy

View Set