Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Alice and Bob, two students of CS 3 3 1 , were asked to provide algorithms for a simple pro - gram. The program is
Alice and Bob, two students of CS were asked to provide algorithms for a simple pro gram. The program is supposed to receive a stream of integers as input. After each input integer, the program must report the average of the integers it has seen so far. Alice and Bob came up with the following algorithms to do this simple task, as in Algorithm and respectively.
State whether Alice and Bob have provided correct algorithms, ignoring truncation due to integer division. Also, either prove the algorithm to be right, or show how it is wrong. If an algorithm is correct then prove the correctness of the algorithm using the concept of loop invariant. For the incorrect algorithm it is enough to specify how to fix the algorithm and no need to prove the correctness of your suggested fix.
Assignment
Problem
Algorithm : Alices Algorithm
Initially sum is and count of integers read is ;
while the input stream is not empty do Read the integer i ;
Add i to the sum ;
Report average as sum count ; Increase count by ;
Algorithm : Bobs Algorithm
Initially average is and count of integers read is ;
while the input stream is not empty do
Read the integer i ;
Modify average as average count icount ; Increase count by ;
Report average; write a loop invariant proof
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started