1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Digiron [165]
4 years ago
7

Use the loop invariant (I) to show that the code below correctly computes the product of all elements in an array A of n integer

s for any n ≥ 1. First use induction to show that (I) is indeed a loop invariant, and then draw conclusions for the termination of the while loop.
Algorithm 1 computeProduct(int[ ] A, int n)
p = a[0]
i = 0
while i < n − 1 do
//(I) p = a[0] · a[1] · · · a[i] (Loop Invariant)
i + +
p = p · a[i]
end while
return p
Computers and Technology
1 answer:
NeTakaya4 years ago
6 0

Answer:

Given Loop Variant P = a[0], a[1] ... a[i]

It is product of n terms in array

Explanation:

The Basic Step: i = 0, loop invariant p=a[0], it is true because of 'p' initialized as a[0].

Induction Step: Assume that for i = n - 3, loop invariant p is product of a[0], a[1], a[2] .... a[n - 3].

So, after that multiply a[n - 2] with p, i.e P = a[0], a[1], a[2] .... a[n - 3].a[n - 2].

After execution of while loop, loop variant p becomes: P = a[0], a[1], a[2] .... a[n -3].a[n -2].

for the case i = n-2, invariant p is product of a[0], a[1], a[2] .... a[n-3].a[n-2]. It is the product of (n-1) terms. In while loop, incrementing the value of i, so i=n-1

And multiply a[n-1] with p, i.e P = a[0].a[1].a[2].... a[n-2].

a[n-1]. i.e. P=P.a[n-1]

By the assumption for i=n-3 loop invariant is true, therefore for i=n-2 also it is true.

By induction method proved that for all n > = 1 Code will return product of n array elements.

While loop check the condition i < n - 1. therefore the conditional statement is n - i > 1

If i = n , n - i = 0 , it will violate condition of while loop, so, the while loop will terminate at i = n at this time loop invariant P = a[0].a[1].a[2]....a[n-2].a[n-1]

You might be interested in
The school has determined that finding the absolute best schedule cannot be solved in a reasonable time. Instead they have decid
Anettt [7]

Answer:

i dont know what to say

Explanation:

... speechlessss

4 0
3 years ago
Pls someone help me with these four questions
ipn [44]

Answer:

16. Grace Hopper. 1959.

8 0
3 years ago
A _____ is an area hosted by a Web server in which project members and colleagues can share documents, models, photos, and other
pshichka [43]

Answer: Shared work-space

Explanation:

Shared work-space is one of the area that are technically hosted by the web server where all the member of the project ad also colleagues in the office can sharing the various types of documents, photos and information about the topics of status of the given project and based on the common interest.    

A shared work-space is the type of virtual and physical work space environment where the organization or company employees worked together and shared lots of information.  

 

 

4 0
3 years ago
ap csp The local, remote, and upstream _______ can each have multiple ___ _____. When a participant in a collaborative group on
ratelena [41]

Answer:

The Local, Remote and Upstream repository can each have multiple push /pull requests. When  a Participant in a collaborative group on Github is ready to have their work used by the group,  the participants makes a pull request.

Explanation:

These are simple blanks which are answer here

The Local, Remote and Upstream <u>repository</u> can each have multiple <u>push /pull</u> requests. When  a Participant in a collaborative group on Github is ready to have their work used by the group,  the participants makes a <u>pull request</u>.

8 0
4 years ago
Suppose that you created an robot that was so advanced it could act independently in very complex situations. It made its own de
Mashutka [201]
It should not be capable of free action because there's no telling if it would be friendly for good remarks or mean for rude remarks
8 0
4 years ago
Other questions:
  • An array created during the execution of a program is called a(n) ____ array.
    11·1 answer
  • Write a static method named evenNumbers that accepts a string of text as a parameter. Assume that the text is a series of intege
    5·2 answers
  • There are several different formats for storing images. They are prints, slides, negatives, and digital. Which is the best forma
    8·1 answer
  • 1. How fast do human beings walk?
    15·2 answers
  • Dereck works for long hours on his computer. He frequently experiences physical strain by the end of the day because he does not
    13·2 answers
  • Suppose that x = 1565.683, y = 85.78, and z = 123.982. What is the output of the following statements? cout &lt;&lt; fixed &lt;&
    8·1 answer
  • You are creating a mobile version of a community discussion site for busy moms. Users post questions and other topics for discus
    10·1 answer
  • In the lungs,blood picks up carbon dioxide and releases oxygen true or false
    7·1 answer
  • Specialization of computer engineering ?<br>​
    7·1 answer
  • Help me! I’ll mark you brainly
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!