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

Data for this lab

Your program reads its data from standard input. Download the file, then give it to your program as shown below. Start with data set 1: its expected output is given.

os4-3-data-1.txt

0.5 10
6 4 6 4 13 13 13
What your program must print for os4-3-data-1.txt
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

Run your program first, then open this and compare line by line: your output must contain these lines exactly.

How to give the file to my program

Terminal (macOS, Linux, or Windows Command Prompt), after compiling:

java Main < os4-3-data-1.txt

Windows PowerShell does not understand <:

Get-Content os4-3-data-1.txt | java Main

IntelliJ IDEA: Run → Edit Configurations → Redirect input from. Or simply paste the content of the file into the console when the program waits for input.

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.