Professional Documents
Culture Documents
Here's a variation on a common schoolchild riddle. Use the numPeople box to remember the
number of people as you proceeded through each step.
PARTICIPATION
ACTIVITY 1.5.1: People on bus.
For each step, keep track of the current number of people by typing in the numPeople
box (it's editable).
Start
1 2 3 4 5
Check Next
Feedback?
By the way, the real riddle's ending question is actually "What is the bus driver's name?"— the
subject usually says "How should I know?". The riddler then says "I said, YOU are driving a bus."
A variable is a named place for a programmer to store data in memory. A programmer de nes a
new variable by writing a statement like age = 20; where age has not been previously de ned.
The interpreter negotiates with the computing system to allocate a memory location for the new
variable. In MATLAB®, the details of such negotiation are hidden from the programmer. However,
to illustrate what is happening behind the scenes, the below animation shows a small portion of
the computer system's memory, in particular memory locations with addresses 96-99.
bl
PARTICIPATION 1.5.2: Variables in memory.
ACTIVITY
Start 2x speed
Memory
age = 20;
96 ??
nextAge = age; 97 20 age
98 ??
99 20 nextAge
Captions
1. Interpreter obtains memory for age. Interpreter writes 20 into age.
2. Interpreter obtains memory for nextAge. Interpreter writes current value of age into
nextAge.
Feedback?
The MATLAB statement age = 20; causes the interpreter to rst nd space in memory to
store the variable named age. The interpreter happens to obtain memory location 97 for age, to
which the interpreter then writes the value 20. The second statement nextAge = age;
causes allocation of another memory location, in this case location 99, after which the
interpreter reads the current value of variable age (which is 20), and writes that value into
nextAge.
An assignment statement such as age = 20; or nextAge = age; upon being executed,
writes (i.e., assigns) the current value of the item on the ='s right side into the variable on the ='s
left side. The "=" is known as the assignment operator. An assignment statement has one of the
following two forms, where the optional semicolon suppresses printing of the result of the
assignment statement to the command window:
Expressions will be described later; for now, the expression may be a number like "20" or a
variable.
variable = expression;
variable = expression
Feedback?
PARTICIPATION
ACTIVITY 1.5.3: De ning numeric variables.
Feedback?
The following example uses three variables named numFriends, peopleIKnow, and
peopleTheyKnow. The animation shows how the variables are written and read by each
statement, while being maintained in the base workspace. The base workspace stores variables
created at the command line. The example illustrates how interactions between people grow in
a social network. Note how variables are initialized, their values are read, and how values are
written back into the variables.
PARTICIPATION
1.5.4: People-known example: Statements write and read variables'
ACTIVITY memory locations.
Start 2x speed
Captions
1. Interpreter allocates memory for variables when variables are rst assigned.
2. "peopleIKnow" is assigned the value of "friends", so "peopleIKnow" is assigned the
value 200.
3. "peopleTheyKnow" is assigned the value of "peopleIKnow" times "friends". So
"peopleTheyKnow" is assigned the value 40000.
4. Assignment reads current values of "peopleTheyKnow" and "friends", multiplies, and
writes the result back into "peopleTheyKnow".
Feedback?
PARTICIPATION
ACTIVITY 1.5.5: Variable assignments.
apples = apples + 1;
apples = oranges;
oranges = oranges + 1;
Feedback?
Swapping variables
A common error is to overwrite the value in a variable before that variable's value was used. The
animation below shows code that initially fails in swapping two variable values, because the rst
assignment overwrites the initial value, which is then lost and thus not used in the second
assignment. One way to avoid such an error is to assign a temporary variable, such as shown in
the animation. The code in the gure further below shows the failed swap and the successful
swap.
PARTICIPATION
ACTIVITY 1.5.6: Swapping using a temporary variable.
Start 2x speed
Captions
1. rstBid is assigned 85 and secondBid is assigned 119.
2. rstBid is assigned secondBid which makes rstBid equal to 119.
3. secondBid is assigned rstBid which equals 119. This swap fails.
4. Just like above, rstBid is assigned 85 and secondBid is assigned 119.
5. A variable called tempBid is created and tempBid is assigned rstBid.
6. rstBid is assigned secondBid which is assigned 119.
7. secondBid is assigned tempBid, which is equal to rstBid, which is equal to 85. This
swap succeeds.
Feedback?
PARTICIPATION
ACTIVITY 1.5.7: Swapping two variables.
Given x = 22 and y = 99. What are x and y after the given code?
1) >> x = y;
>> y = x; Correct
2) >> x = y;
>> y = x; Correct
>> x = y;
x = y assigns 99 to x.
x is 99 and y is 22. y = x then assign x's value, now 99, to y.
The last x = y assigns 99 to x.
x is 99 and y is 99.
x is 22 and y is 22.
3) >> tempVal = x;
>> x = y; Correct
>> y = x;
The last statement reads x's new value of 99 and assigns
x is 99 and y is 22. it to y. Instead, that statement should read tempVal.
x is 99 and y is 99.
4) >> tempVal = x;
>> x = y; Correct
>> y = tempVal;
The swap succeeds, because x's value is saved before
x is 99 and y is 22. being assigned 99, and then that saved value is assigned
to y.
x is 99 and y is 99.
Feedback?
CHALLENGE
ACTIVITY 1.5.1: Assigning a sum. 2/2
Assigning a sum
Write a statement that assigns numCoins with numNickels + numDimes.
1 numNickels = 5
2 numDimes = 10
3
4 % Write a statement that assigns numCoins with numNickels + numDimes
5 numCoins=numNickels+numDimes
Output
numNickels =
numDimes =
10
numCoins =
15
Feedback?
CHALLENGE
ACTIVITY 1.5.2: Fahrenheit to Celsius using multiple statements. 2/2
1 temperatureFahrenheit = 77
2
3 % Write a statement that assigns fractionalMultiplier with 5/9
4 fractionalMultiplier=5/9
5
6 % Modify the statement below to convert Fahrenheit to Celsius,
7 % assigning temperatureCelsius with the Celsius value
8 temperatureCelsius = fractionalMultiplier*(temperatureFahrenheit-32)
Previous Assessment: All Tests Passed
Output
temperatureFahrenheit =
77
fractionalMultiplier =
0.5556
temperatureCelsius =
25
Feedback?
CHALLENGE
ACTIVITY 1.5.3: Multiple variables: Curving an exam score. 2/2
Output
myExamScore =
82
myCurvedExamScore =
87
Feedback?