# Evaluating values

Randy Smith

Ranch Hand

Posts: 44

posted 4 years ago

Greetings,

Assume that m and n are declared as valid int variables. What will be the output of the program segment below?

A. m +n

B. m * n (correct answer)

C. m

D. n

Hi guys, I can't figure out how can I work this problem, I get very confused when questions like this comes without any brace. Is there a strategy on working on these type of questions?

Assume that m and n are declared as valid int variables. What will be the output of the program segment below?

A. m +n

B. m * n (correct answer)

C. m

D. n

Hi guys, I can't figure out how can I work this problem, I get very confused when questions like this comes without any brace. Is there a strategy on working on these type of questions?

Randy Smith

Ranch Hand

Posts: 44

posted 4 years ago

Here's another one, I have no idea what am I suppose to do, appreciate if someone can explain to me

Assuming x and y are int variables, the statements

y = y + x;

y = y + x;

can be replaced with the assignment statement

A) y = 2*x + 2*y;

B) y = (x + y)/2;

C) y = y + 2*x; (correct answer)

D) y = x + 2*y;

Assuming x and y are int variables, the statements

y = y + x;

y = y + x;

can be replaced with the assignment statement

A) y = 2*x + 2*y;

B) y = (x + y)/2;

C) y = y + 2*x; (correct answer)

D) y = x + 2*y;

Harsha Smith

Ranch Hand

Posts: 287

Harsha Smith

Ranch Hand

Posts: 287

Campbell Ritchie

Sheriff

Posts: 48652

56

posted 4 years ago

Where on earth did you find those questions? They look designed to confuse. You would have to go through the execution of the code with a pencil and paper, and you can write down the values after each operation. With the two nested loops, you can tell that the loops are executed

`n × m`times, but only if both numbers are positive; if either number is negative, you get an output of 0.
Randy Smith

Ranch Hand

Posts: 44

posted 4 years ago

these are past year exams questions from my uni, i freak out when I have to work on these kind of questions under exam condition, trying to practise as much as possible. I'm actually quite unhappy that during this age and time, i have to work on programming exams using pencil and paper. I wish we are instead required to complete a small programming project within 2 - 3 hours with an IDE instead.

Campbell Ritchie wrote:Where on earth did you find those questions? They look designed to confuse. You would have to go through the execution of the code with a pencil and paper, and you can write down the values after each operation. With the two nested loops, you can tell that the loops are executedn × mtimes, but only if both numbers are positive; if either number is negative, you get an output of 0.

these are past year exams questions from my uni, i freak out when I have to work on these kind of questions under exam condition, trying to practise as much as possible. I'm actually quite unhappy that during this age and time, i have to work on programming exams using pencil and paper. I wish we are instead required to complete a small programming project within 2 - 3 hours with an IDE instead.

Campbell Ritchie

Sheriff

Posts: 48652

56

posted 4 years ago

I think I agree with you about those questions. There are good reasons for using pencil and paper (usually accompanied by a large eraser ) in programming, for example when you are designing pseudo-code to implement algorithms, or similar. Those questions do test your understanding of a program, but I think having to create an application would be a far better test of your abilities. Round here, the undergraduates usually get an application to create and two months to create it in. During that time, they are allowed to ask for help, within certain limits.

Randy Smith

Ranch Hand

Posts: 44

Campbell Ritchie

Sheriff

Posts: 48652

56

posted 4 years ago

You are arguing from the particular to the general; that is not valid. You need to go from the general to the particular.

Let’s work out its weakest-precondition transformer effect, to establish

[y = y + x; y = y + x]

[y = y + x][y = y + x]

[y = y + x]

[y = y + x]

You can now substitute 1 for x and 1 for y and get 3.

Let’s work out its weakest-precondition transformer effect, to establish

*z*= y, which means the value of y at the end of the program.[y = y + x; y = y + x]

*z*= y ≡ by the Sequence Rule[y = y + x][y = y + x]

*z*= y ≡ by the assignment rule on the right[y = y + x]

*z*= y 〈y \ x + y〉 ≡ by substitution[y = y + x]

*z*= x + y ≡ by the assignment rule on the left*z*= x + y 〈y \ x + y〉 ≡ by substitution*z*= x + y + y ≡ by algebra*z*= x + 2y QEDYou can now substitute 1 for x and 1 for y and get 3.

Harsha Smith

Ranch Hand

Posts: 287

Campbell Ritchie

Sheriff

Posts: 48652

56

Randy Smith

Ranch Hand

Posts: 44

posted 4 years ago

I think I have solved it.

y = y + x;

y = y + x; <-assign new value

y = (y + x) + x

y = y + 2x;

Yayyyy!

Campbell Ritchie wrote:You are arguing from the particular to the general; that is not valid. You need to go from the general to the particular.

Let’s work out its weakest-precondition transformer effect, to establishz= y, which means the value of y at the end of the program.

[y = y + x; y = y + x]z= y ≡ by the Sequence Rule

[y = y + x][y = y + x]z= y ≡ by the assignment rule on the right

[y = y + x]z= y 〈y \ x + y〉 ≡ by substitution

[y = y + x]z= x + y ≡ by the assignment rule on the left

z= x + y 〈y \ x + y〉 ≡ by substitution

z= x + y + y ≡ by algebra

z= x + 2y QED

You can now substitute 1 for x and 1 for y and get 3.

I think I have solved it.

y = y + x;

y = y + x; <-assign new value

y = (y + x) + x

y = y + 2x;

Yayyyy!