Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Given below is the outline of a loop, a loop invariant, and the condition which

ID: 3650535 • Letter: G

Question

Given below is the outline of a loop, a loop invariant, and the condition which must be true when the loop terminates. Write a complete program that reads in two numbers x and y, validating them (prompting the user until valid numbers are entered) and runs such that the three given assertions will always be true and the program will terminate provided the user enters appropriate values for x and y. Include the loop invariant assertion at the four points in your program where it must be true. Part 1 of your program may not use the multiplication operator, except in assert(...) statements. assert(x>0 && y>0); while(...) { assert(sum == i*(x+1)); ... ... } assert(sum == y*(x+1));

Explanation / Answer

int main () { int x; int y; int i=1; int sum=0; cout> x >> y; assert(x>0 && y>0); while(y!=i) { assert(sum == i*(x+1)); cout> x >> y; i++; } assert(sum == y*(x+1)); return 0; } Here are the full instructions: Given below is the outline of a loop, a loop invariant, and the condition which must be true when the loop terminates. Write a complete program that reads in two numbers x and y, validating them (prompting the user until valid numbers are entered) and runs such that the three given assertions will always be true and the program will terminate provided the user enters appropriate values for x and y. Include the loop invariant assertion at the four points in your program where it must be true. Part 1 of your program may not use the multiplication operator, except in assert(...) statements. assert(x>0 && y>0); while(...) { assert(sum == i*(x+1)); ... ... } assert(sum == y*(x+1)); Extend your program by including the following code (after the code for Part 1) and include the loop invariant and post condition (assertion after the loop) as comments, not assert statements, that most specically describe what the loop does and what is known about the relationship among the three variables when the loop terminates. You should analyze what is going on with these variables and what is known about them before you enter the loop, after each iteration of the loop, when the loop will terminate, and after the loop terminates. You may assume the user enters a positive value for c. cin >> c; assert(c>0); a = 1; b = 0; while(c - a > 0) { a = a * 5; b = b + 1; }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote