cs 121 – quiz 3 2010 feb 17 th. question 2 we want to count how many days there were such that the...

8
CS 121 – Quiz 3 2010 Feb 17 th

Upload: hilary-hubbard

Post on 19-Jan-2016

213 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the

CS 121 – Quiz 3

2010 Feb 17th

Page 2: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the

Question 2• We want to count how many days there were

such that the temperature is at least 1.900 degrees lower than the previous day and the average temperatures over those days

• Need a for loop to traverse all the elements in the list and start from the 2nd day• Need nops() function to get the total number of

elements in the list• Need an if statement to set a condition to check if the

temperature is 1.900 degrees lower than the previous day

Page 3: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the

• Need a variable to count the days for which the condition is true• Need a variable to calculate the total temperature over

all the days for which the condition is true• The average temperature is the total/count

– Script outline• counter := 0; total := 0;• for i from 2 to nops(Ltemps) do

– If (Ltemps[i] - Ltemps[i-1] >= 1.900) then» counter := counter + 1;» total := total + Ltemps[i];

– end if;

• end do:

Page 4: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the

– A List is a container. Items can be accessed by subscript.

–When the loop is done, just print out the count variable and the average temperature is the total temperature divided by the count.

– The loop runs from 2 instead of 1, because the first execution of the loop will compare the ith and (i+1)th element. When i =1, the loop compares the 1st to 0th element, and 0th element doesn’t exist. When i=2, it compares the 2nd to the 1st element, which makes more sense.

Page 5: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the

Question 3

• Use the computational model and script outline used in CS 122 Lab 3 Problem 1

• You are asked to input a list of 2 numbers.– [minAngle,maxAngle]– To ensure Blammo lands within 1 meter of a

target positioned 110 meters away

Page 6: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the

• #Define gravitational constants, initial x,y position, initial velocity, target distance, tolerance, and other constants.

• #Define a table to contain all angles that allow Blammo to land within 1 meter of the target– angleTab := table();

• #Define a loop to test angles– for angle from 10 to 89 by .1 do

Page 7: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the
Page 8: CS 121 – Quiz 3 2010 Feb 17 th. Question 2 We want to count how many days there were such that the temperature is at least 1.900 degrees lower than the

• #convert the table to a list– angleList := convert(angleTab, list);

• #use min and max function to pick up the minimum and maximum angle– minAngle := min(angleList);– maxAngle := max(anlgeList);

• #Put the two angles into a list– [minAngle,maxAngle];