CS 31 Bridge – Summer 08                                                          Homework 4
 
Name: ______________________                     Group: _________________________________            Hours taken: ___
 
You can work in group of 2-3 people. You can talk and discuss among your group. However, write your own answers for the homework. Do NOT copy each other. Make your answers CLEAR and SHORT.
 
True False:

 

An explanation is required.

 

1.      In C++ Boolean value are represented only with the int values 0 for false and 1 for true.

 

2.      A variable declared outside any function is said to be a local variable.

 

3.      A variable declared within a function block is said to be local to the function.

 

4.      Consider two blocks, one within another. If an identifier is declared as a variable in the inmost of these two blocks, one can access this variable from the outer block.

 

5.      Calling something a black box is a figure of speech that conveys the idea that you cannot see inside. You know its behavior and interface but not its implementation.

 

 

Free Form Questions:

 

1.      Convert the following mathematics expressions to C++ expressions. Use the declarations provided. Indicate the appropriate header file for any library function used. Be sure you initialize any variables whose values you are using to reasonable values for the library functions you are using.

     a)   int x, y;

           y <= |x|

 

 

 

 

 

 

     b)   double x, y;

p =

 

 

 

 

 

 

2.      Declare (give a prototype for) a function named average_grade. This function returns a double and has four double arguments, test1, test2, test3, test4. The return value should be the average, or arithmetic mean of the four arguments. Be sure to include a "prototype comment" that tells briefly what the function does.

 

 

 

 

 

3.      What is the error in the following code? Why is this wrong?

int f(int x)

{

  int x;

  // code for function body

}

 

4.   Here is a complete function that purports to return one of the roots of a quadratic given suitable parameters. It looks good, but fails to compile. Why?

//returns one of the roots of the quadratic equation

//a*x*x + b*x + c = 0   a!= 0

double root1 (double a, double b, double c)

{

return (-b + sqrt(b*b-4*a*c))/(2*a);

}

 

5.      Write a function definition 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.

 

 

 

 

 

 

 

 

 

 

 

 

Multiple Choice

 

There may be more than one correct answer. You must give all correct answers for full credit. An explanation is required.

 

1.      A C++ predefined functions

a)      are usually provided in libraries

b)      make C++ harder than necessary.

c)      must #include the proper header file

d)      are usually provided with the C++ compiler

 

2.      A call to a C++ function is

a)      The name of the function followed by empty parentheses.

b)      The name of the function followed by any number of arguments, regardless of the number of parameters in the definition.

c)      The name of the function followed by a number of arguments not greater than the number of parameters in the definition.

d)      The name of the function followed by exactly the number of arguments as there are parameters in the definition.

e)      The name of the function only.

 

3.      A void function

a)      performs some action and returns a value

b)      performs some action but does not return a value

c)      is a statement

d)      call is written much like a call to a value returning function but is terminated with a semicolon.

e)      A void function may have a return statement but is not required to have one.

 

4.      A definition of a variable outside any function is called a

a)      local function definition

b)      global variable definition

c)      global function header

d)      global function definition

e)      local variable definition

 

5.      Which of the following statements about the definition and declaration of functions is not  correct?

a)      Function declaration is exactly the same as function prototype.

b)      Function definition is exactly the same as function prototype.

c)      A function header is exactly the same as function prototype except for the semicolon.

d)      A function definition is a function header followed by the function block.

e)      A function header syntax is the following:
return_type function_name parenthesized_parameter_list

 

6.      Correct statements of similarities and differences between calling user defined functions and library functions are:

a)      Similarity: A call to either requires the function name followed by parenthesized comma separated list of arguments that match the prototype in number and type.

b)      Difference: Library functions require inclusion of header files for declarations but user defined functions do not require any declaration before use.

c)      Similarity: Either library or user defined functions may be value returning or void functions.

d)      Difference: Library functions all provide a return value, user functions must be void functions.

e)      Difference Library function declarations (sometimes definitions) come from #include <header> statements. User functions require either explicit prototypes prior to use or a #include "user_header.h" for the prototypes

 

7.      Here is a small program. Which of the statements about the variables is correct?

#include <iostream>

const double NUM = 2.9345358;

double num = 3;

double numTimes(int x);

int main( )

{

using namespace std;

int value;

cout << "Enter a value, I’ll multiply it by "

<< NUM << endl;

cin >> value;

cout << "You entered " << value

<< " NUM times this is " << numTimes(value)

<< endl;

return 0;

}

double numTimes(int x)

{

double d;

d = NUM * x;

return d;

}

a)      NUM is a global variable.

b)      num is a global constant.

c)      value is local variable in the main function

d)      d is a local variable in the numTimes function. 

 

8.      Consider the following pair of functions. You are to assume that the ellipses (. . .) contain correct code sufficient that it with the code you can see will compile apart from the two variables named sam in the two functions.

. . . .

void func1( )

{

int sam;

. . .

}

void func2( )

{

int sam;

. . .

}

int main( )

{

func1();

func2();

. . .

}

a)      This code will not compile because of the multiply defined variable sam. In the explanation give the compiler message you get on compiling.

b)      This code will compile just fine.

c)      This code will compile but will give a runtime error due to the multiply defined variable sam. In the explanation give the error message you get when this is run.

d)      This code compiles and runs without any error messages. Explain why in the explanations.

 

9.      Assume this code fragment is embedded in an otherwise correct and complete program. What should be the output from this code segment?

{

for( int i = 0; i < 10; i++)

{

     . . .

}

cout << i << endl;

}

a)      10

b)      9

c)      0

d)      The variable i is undefined in this scope, so this should not compile