Megan Darcy
Megan Darcy

Reputation: 582

How to count the number of computations in the loop?

I'm reading this textbook:

Randal E. Bryant, David R. O’Hallaron - Computer Systems. A Programmer’s Perspective [3rd ed.] (2016, Pearson).

Currently I am unsure about Problem 5.5 in the text book. Below, I have attached a link to the book to make it easier to find.

Problem 5.5

And this is the answer they give:

solutions

Could someone please tell me how the authors arrive at the answer for this question? I am not sure about how they count the number of computations (n additions and 2n multiplications).

Thank you in advance, any help is appreciated :)

View Textbook Online

Upvotes: 1

Views: 157

Answers (1)

hsuecu
hsuecu

Reputation: 107

result += .... is done n times.

and on rhs of line 7 and 8 there are n+n multiplications. It is clearly visible. What is that matter of confusion

Upvotes: 1

Related Questions