Name: ____________________________________________________ Alpha: _____________________

Describe help received: _________________________________________________________________

Problem 1

Write prefix and postfix expressions for: w = 3.0/z + y*sqrt(-x)

Problem 2

Given the Pascal operator-precedence table
	  PRECEDENCE     OPERATORS
 
highest        NOT
   .           *, /, DIV, MOD, AND
   .           +, -, OR
lowest         <, <=, <, >, =, >=, >, IN
Fully parenthisize NOT x < 42 * 5 AND 6 = y

Problem 3

Label each of the following with fail/succeed according to whether it will fail or succeed in a language in which assignment is a statement, not an expression.
	if ((x = getchar()) && c != ';') {
	  ...
	}


	
	do {
	  x = getchar();
	  if (x == ';') break;
	  sum += x - '0';
	} while(true);

Problem 4

Consider the code fragment below. Draw pictures of the state of the program after this code is executed assuming the value model is used, and then assuming the reference model is used (and assuming ints are immutable!).
	sum = 0;
	i = 1;
	while (i < 4) {
	  sum = sum + i;
	  i = i + 1;
	}
      

Problem 5

In C++, the following compiles except for the line p.mag() = 3*4;. Explain why it fails, and then explain why it cannot be fixed by changing the defintion
of mag() to: double& mag() { return sqrt(x*x + y*y); }
#include <iostream>
#include <cmath>
using namespace std;

class Point {
public:
  double x, y;
public:
  Point(double x, double y) { this->x = x; this->y = y; }  
  double& getX() { return x; }  
  double mag() { return sqrt(x*x + y*y); }  
};

int main()
{
  Point p(3,5);
  p.getX() = 7*5;
  p.mag() = 3*4;
  return 0;
}