Answered step by step
Verified Expert Solution
Link Copied!

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 331, 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 1 and 2, 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 1
Problem 2
Algorithm 1: Alices Algorithm
Initially sum is 0 and count of integers read is 0 ;
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 1;
Algorithm 2: Bobs Algorithm
Initially average is 0 and count of integers read is 0 ;
while the input stream is not empty do
Read the integer i ;
Modify average as (average * count + i)/(count +1) ; Increase count by 1 ;
Report average; write a loop invariant proof

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

Database In Depth Relational Theory For Practitioners

Authors: C.J. Date

1st Edition

0596100124, 978-0596100124

More Books

Students also viewed these Databases questions

Question

3. Existing organizations and programs constrain behavior.

Answered: 1 week ago