Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Question 1 . ( 4 0 pts . ) Consider the loop in sumN ( ) . sumN ( ) requires n > 0 &&

Question 1.(40 pts.) Consider the loop in sumN(). sumN() requires n >0 && n %2!=0(i.e., n is an odd positive integer) and
returns the sum of odd integers 1 through n. For example, sumN(5)=9.
// precondition: n >0 n %2=0
int sumN(int n){
int k =3;
int sum =1;
// LI: sum =1+3+...+(k -2) k %2=03<= k <=(n +2)
while (k <= n){
sum = sum + k;
k = k +2;
}
return sum;
}
// postcondition: sum =1+3+...+ n
Given the loop invariant sum =1+3+...+(k 2) k%2=03<= k <=(n +2), show that the loop invariant is true for the base
case before the loop executes. (10 pts.)
Use induction to show that the loop invariant holds for the general case. That is, assume it holds after some iteration m
and show that it holds after iteration m +1.(20 pts.)
Show that at exit, the loop invariant and the exit condition imply the postcondition. (10 pts.)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Understanding Databases Concepts And Practice

Authors: Suzanne W Dietrich

1st Edition

1119827949, 9781119827948

More Books

Students also viewed these Databases questions

Question

Why would unions target health care workers?

Answered: 1 week ago