3 Replies - 309 Views - Last Post: 09 August 2010 - 01:02 PM Rate Topic: -----

#1 jim1963  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-September 09

Trying to figure this out

Posted 09 August 2010 - 07:45 AM

What possible values can the following code snippet generate? Assume that rand(n) returns an integer between 0 and n-1, inclusive.

int random_value = floor(((2.5*rand(3)) + 2)^2)
Is This A Good Question/Topic? 0
  • +

Replies To: Trying to figure this out

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Trying to figure this out

Posted 09 August 2010 - 07:46 AM

Is it a test question?

You could check if you run the code.
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Trying to figure this out

Posted 09 August 2010 - 11:31 AM

Just break it down.

int random_value = floor(((2.5*rand(3)) + 2)^2)

Possible values for rand(3) can be 0, 1, and 2

Then do basic math.

((2.5*0) + 2) ^ 2 = ...

((2.5*1) + 2) ^ 2 = ... Then round down to the nearest integer.

((2.5*2) + 2) ^ 2 = ...

If you need further help, well, you shouldn't.

This post has been edited by eker676: 09 August 2010 - 11:32 AM

Was This Post Helpful? 3
  • +
  • -

#4 jim1963  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 11-September 09

Re: Trying to figure this out

Posted 09 August 2010 - 01:02 PM

View Posteker676, on 09 August 2010 - 10:31 AM, said:

Just break it down.

int random_value = floor(((2.5*rand(3)) + 2)^2)

Possible values for rand(3) can be 0, 1, and 2

Then do basic math.

((2.5*0) + 2) ^ 2 = ...

((2.5*1) + 2) ^ 2 = ... Then round down to the nearest integer.

((2.5*2) + 2) ^ 2 = ...

If you need further help, well, you shouldn't.


Thank you. I understand now.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1