Solution for Final

Problem 1:

int hasRealRoots (float, float, float, float&, float&);
void hasRealRoots (float, float, float, int&, float&, float&);

Problem 2:

A B C
D Unknown n 5

16 (or j not declared)

12

20

3

Problem 3:

1 2 3 5 8
the nth Fibonacci number

Problem 4:


(F) In the expression p || q, if p is false, then q will not be evaluated.

(F) When a function performs several actions (e.g., input, computation 
of several values, and output), it makes a program easier to modify 
since there are fewer functions in the program.

(T) By writing conditions such as (k == 7) with the constant on the 
left (7 == k) the programmer who accidentally replaces the == operator 
with = will be protected by the compiler.

(T) If the call fabs(x) in:
      y = 3 + fabs(x);
modified the value of x, that would be an example of a side effect.

(T) Consider the following prototype:
      int rocky(int n);
If a variable n is declared outside main(), it is not visible inside the 
block associated with rocky().

(F) In general, it is not risky to use a reference parameter in a value 
returning function.

(F) The lifetime of a identifier is the portion of a program in which the 
identifier can be used.

(T) In general, each function should be limited to performing a single, 
well-defined task. This contributes to a good program design.

(F) A variable declared outside of any block or function is an automatic 
variable.

(T) A compound statement (i.e., a block) may be placed anywhere in a 
program that a single, executable statement can be placed.
Problem 5:
int getNumberOfDigits (int i) {
  int power = 1;
  while ((int)pow(10,power) <= i) power++;
  return(power);
}
Problem 6:
int getDigit (int i, int j) {
  return (i % (int)pow(10,j)) / (int)pow(10,j-1)
}
Problem 7:
int getNumberOfDigits (int);
int getDigit (int, int);

void main () {
  int ndigits, i, result = 0;
  int x;
  cout << "Enter a (positive) octal number: ";
  cin >> x;

  ndigits = getNumberOfDigits(x);
  for (i=1; i<=ndigits; i++) 
    result = result + getDigit(x,i) * (int)pow(8,i-1);
  cout << result;
}

sabry@cs.uoregon.edu