THINK FIRST·CODE LATER

← All labs

Predicting CPU bursts with exponential averaging

Problem

SJF needs the length of the next CPU burst. Predict it with exponential averaging:

τₙ₊₁ = α · tₙ + (1 − α) · τₙ

Input: first line alpha tau0; second line: the actual burst lengths t₀, t₁, … (at least one).

Print a table line for each actual burst: n=k predicted=P actual=A error=E where P is the prediction before that burst, E = A − P (signed), both with two decimals. Then print Next prediction: X (two decimals) and Mean absolute error: M (two decimals).

Input:

0.5 10
6 4 6 4 13 13 13

Output:

n=0 predicted=10.00 actual=6.00 error=-4.00
n=1 predicted=8.00 actual=4.00 error=-4.00
n=2 predicted=6.00 actual=6.00 error=0.00
n=3 predicted=6.00 actual=4.00 error=-2.00
n=4 predicted=5.00 actual=13.00 error=8.00
n=5 predicted=9.00 actual=13.00 error=4.00
n=6 predicted=11.00 actual=13.00 error=2.00
Next prediction: 12.00
Mean absolute error: 3.43

Write it here or in your IDE, then paste it. Compile and test it yourself before comparing. Your code stays in your browser — it is never sent to or stored on the server.