80 likes | 267 Views
CS123 Quiz1. Question 3. Script Outline restart: Procedure definitions from the question description NextFalconsAndRobins falcons robins Define 2 tables for the population of falcons and robins R:=table(): F:=table():.
E N D
Question 3 • Script Outline • restart: • Procedure definitions from the question description • NextFalconsAndRobins • falcons • robins • Define 2 tables for the population of falcons and robins • R:=table(): • F:=table():
Initialize the first elements in the tables, which are the original populations. • R[0] := 5027: • F[0] := 14: • Make a loop • for i from 1 to 500 do • #Formula for computation of robins/falcons population • #The output of procedure NextFalconsAndRobins is a list, so we need indexing to pick up the specific population • F[i] := NextFalconsAndRobins(F[i-1],R[i-1])[1]: • R[i] := NextFalconsAndRobins(F[i-1],R[i-1])[2]: • end do:
Now we have the populations of robins or falcons after 500 years. • R[500]; • F[500]; • Create a new table for the population of robins in each local peek year. • Peek:=table(): • Make a loop • for i from 1 to 500 do • Compare the population in each year with the previous year and the next year • if (R[i-1] < R[i] and R[i] > R[i+1]) then • Peek[i] := R[i]: • end if: • end do: • Now we have the populations of robins in all local peek years. • convert(Peek, list);
Question 4 • Define procedure acState • acState := proc(currentTemp, presentState) • local triggerTemp, shutoffTemp ; • triggerTemp := 87; • shutoffTemp := 81; • if presentState=low and currentTemp>=triggerTemp • then return high; • elif presentState=high and currentTemp<=shutoffTemp • then return low; • else return presentState; • end if; • end;
Declare a list of temperatures and states • TempState := [[85.1, high], [85.2, low], [86.0, low], [83.9, high], [78.7, low], [89.3, low], [83.0, high], [89.4, low], [84.5, low], [90.3, high], [85.2, low], [84.5, low], [82.8, low], [81.4, high], [84.6, high], [78.4, low], [88.1, low], [85.9, low], [80.7, low], [90.5, low], [84.8, low], [86.6, high], [87.0, low], [86.4, high], [85.5, high], [80.7, low], [82.8, low], [86.9, high], [90.4, low], [91.4, high], [83.0, low], [89.4, high], [92.0, high], [83.6, high], [88.0, low], [80.8, high], [86.4, low], [90.0, low], [79.7, low], [83.6, low], [82.1, high], [87.5, high], [86.6, low], [88.3, high], [86.0, high], [87.8, high], [78.7, high], [91.6, low], [83.3, high], [88.4, low]]:
Create a table for states: • states := table(): • Make a loop • for i from 1 to nops(TempState) do • #TempState is a list of lists, TempState[i] is a list of 2 elements: temperature and present state. • state[i] := acState(TempState[i][1],TempState[i][2]); • Or • #The last slide is about map and op functions • states[i] := map(acState,op(TempState[i])); • end do: • convert(states, list);
Appendix • map • applies a procedure to each operand of an expression • for example: • f := (a,b) -> a+b • map(f,1,2) applies 1 and 2 to function f and return 3 • op • extracts operands from an expression • Takes all elements from a list • For example: • myList := [1,2,3] • op(myList) returns 1,2,3 • Go to Maple help for details