Lecture #15
November
5, 1999
Bob
Pitha
lets talk about the quiz
Stats:
average
score: 15.5
each
question got answered correctly by someone
question 1

Remember
the rules for identifiers:
Letters,
digits, and underscore
Cant start
with a digit
lets talk a little more about identifiers
Identifiers
are just names you can use in your program
theyre kind
of a general-purpose set of rules for naming things
variables
functions
new data types
We havent covered these yet
|
|
data structures
symbolic constants
valid identifiers
Theres
a difference between a legal identifier and one that can be used in a
particular context
legal means
it just passes the simple rules for identifiers
useable
means the compiler will let you use it
The
main reason a legal identifier may not be used is that its already used in a
way that would conflict
conflicting names
For
example, main
is a perfectly valid identifier
its all
letters
it doesnt
begin with a digit
But
I cant name a variable main, because it would conflict with the function named
main.
Similarly,
when your book says you shouldnt name variables starting with an underscore,
its not because its not a legal identifier
its just
that sometimes youll find that the name has already been used
so back to question 1

so back to question 1
It
is perfectly legal to mix upper and lower case
case-sensitive
means that 2 identifiers are considered different even if they only differ by
the case of one or more letters (i.e., i vs. I)
so back to question 1
so back to question 1
There
is a data type named double, which means it is a keyword that cannot be used as
a variable
but its
still a valid identifier
anyway, this
is Double, not double.
so back to question 1
This
is a legal identifier
just as your
book says, its a good idea to avoid identifiers that begin with a single
underscore
question 2

There
are some special-purpose computers that have other byte sizes, but these are
rare. All common modern computers use an 8-bit byte.
question 3

question 4
|

|
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
|
|
|
1111
|
|
|
|
|
2c
|
|
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
|
|
|
1111
|
|
|
|
|
2c
|
Lets
look at these numbers that are already filled in
so first,
lets review what we know about binary

question 4 binary review
So
1010 in binary is:
1*23 + 0*22
+ 1*21 + 0*20
8 + 2
10
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
|
|
15
|
1111
|
|
|
|
|
2c
|
Similarly,
1111 in binary is:
1*23 + 1*22
+ 1*21 + 1*20
8 + 4 + 2 + 1
15
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
a
|
|
15
|
1111
|
|
|
|
|
2c
|
Now
for some hex:
remember the
hex digits are
1 2 3 4 5 6 7 8 9 a b c d e f
where a-f
represent 10-15
so this
first one is easy: 10 in hex is a
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
a
|
|
15
|
1111
|
f
|
|
|
|
2c
|
And
15 is just f
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
a
|
|
15
|
1111
|
f
|
|
44
|
|
2c
|
Now
going from hex to decimal:
2c is:
2*16 + c
32 + 12
44
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
a
|
|
15
|
1111
|
f
|
|
44
|
|
2c
|
Now
the hard one: convert 44 to binary
this
essentially means expressing 44 as a sum of powers of 2
so what
powers of 2 can we sum up to make 44?
Whats the
largest power of 2 that is less than 44?
32 (25)
in binary,
therefore, 32 is 100000
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
a
|
|
15
|
1111
|
f
|
|
44
|
|
2c
|
So
44 is 25 + 12
so we need
to express 12 as a sum of powers of 2
whats the
largest power of 2 that is less than 12?
8 (23)
So
44 is 25 + 23 + 4
question 4
|
decimal
|
binary
|
hexadecimal
|
|
10
|
1010
|
a
|
|
15
|
1111
|
f
|
|
44
|
101100
|
2c
|
So
44 is 25 + 23 + 4
so we need
to express 4 as a sum of powers of 2
4 is just 22
So 44 is 25 + 23
+ 22
which means
in binary, 44 is 101100
that is, 1*25 + 0*24
+ 1*23 + 1*22 + 0*21 +
0*20
a comment about conversions
Whats
the point of this?
these were
simple numbers.
it helps to
know simple hex/binary/decimal numbers
extra credit

It
turns out that when the order of one base is a power of the order of another
(i.e., 16 is 24) it is very convenient to convert between
them
you only
have to look at each group of 4 bits
4 bits
represents a number from 0 to 15
1 hex digit
represents a number from 0 to 15
question 5

This
is a perfectly legal expression
a
and b
are character constants
remember
that characters are stored as numbers
so a and b are simply
integers, and they can be added together
homework #3
Should
be on the web page sometime today
This
is the first real program you get to write
a program to
print out a Fibonacci series
0, 1, 1, 2,
3, 5, 8, 13, 21, 34,
Your program
should prompt the user for the number of elements to print
Ive given
you the code to use to read the number in from the keyboard